Home >Java >javaTutorial >How Can I Access a Resource File (e.g., XML) Inside a Java JAR?
Accessing a Resource File from Within a Java JAR
You're encountering issues loading an XML file from a JAR file. Here's the appropriate solution:
Instead of using FileReader, which is not suitable for reading resources from a JAR, utilize java.lang.Class.getResourceAsStream(String). The correct way to read the XML file is as follows:
URL url = getClass().getResourceAsStream("/xxx/xxx/xxx/services.xml");
This method returns an InputStream which allows you to access the resource file's contents as a stream. You can then parse the XML file using an appropriate XML parsing library.
The above is the detailed content of How Can I Access a Resource File (e.g., XML) Inside a Java JAR?. For more information, please follow other related articles on the PHP Chinese website!