Asked  6 Months ago    Answers:  5   Viewed   25 times

I'm trying to access an XML file within a jar file, from a separate jar that's running as a desktop application. I can get the URL to the file I need, but when I pass that to a FileReader (as a String) I get a FileNotFoundException saying "The file name, directory name, or volume label syntax is incorrect."

As a point of reference, I have no trouble reading image resources from the same jar, passing the URL to an ImageIcon constructor. This seems to indicate that the method I'm using to get the URL is correct.

URL url = getClass().getResource("/xxx/xxx/xxx/services.xml");
ServicesLoader jsl = new ServicesLoader( url.toString() );

Inside the ServicesLoader class I have

XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setContentHandler( this );
xr.setErrorHandler( this );
xr.parse( new InputSource( new FileReader( filename )));

What's wrong with using this technique to read the XML file?



The problem was that I was going a step too far in calling the parse method of XMLReader. The parse method accepts an InputSource, so there was no reason to even use a FileReader. Changing the last line of the code above to

xr.parse( new InputSource( filename ));

works just fine.

Tuesday, June 1, 2021
answered 6 Months ago

Finally, I found the solution:

final String path = "sample/folder";
final File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());

if(jarFile.isFile()) {  // Run with JAR file
    final JarFile jar = new JarFile(jarFile);
    final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
    while(entries.hasMoreElements()) {
        final String name = entries.nextElement().getName();
        if (name.startsWith(path + "/")) { //filter according to the path
} else { // Run with IDE
    final URL url = Launcher.class.getResource("/" + path);
    if (url != null) {
        try {
            final File apps = new File(url.toURI());
            for (File app : apps.listFiles()) {
        } catch (URISyntaxException ex) {
            // never happens

The second block just work when you run the application on IDE (not with jar file), You can remove it if you don't like that.

Tuesday, June 1, 2021
answered 6 Months ago

The default Java class loader cannot find an embedded jar inside a jar file. You will need to set your Java program to use a class loader which can deal with embedded jar files. As fas as I know, I have not seen one in any opensource libraries so you may need to write one yourself.

Alternatively, you can extract all the jar files and combine the the content into a single jar file.

Clean way to combine multiple jars? Preferably using Ant

Wednesday, July 7, 2021
answered 5 Months ago
Wednesday, July 7, 2021
answered 5 Months ago

text_file.readlines() returns a list of strings containing the lines in the file. If you want only a string, not a list of the lines, use instead.

You also have another problem in your code, you are trying to open unknown.txt, but you should be trying to open 'unknown.txt' (a string with the file name).

Thursday, October 14, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :