Search code examples
javaeclipsejarpackaging

Packaged assets not available in Jar


My bundling looks like this and I call the resources like this. All works well when run from Eclipse, though when exported as a runnable Jar. The program appears to not be able to reference the resources
Question
Why are my assets not accessible when packaged?


Solution

  • File doesn't work well with jars, because the file system isn't set up to see into jars. If you use URL's to address your resources you should be fine.

    EDIT: adding example.

    Assuming your linked address phrase is correct (since the code works in Eclipse, that seems likely), the following should get you a working URL.

    URL url = MapLoader.class.getResource("assets/xml/maps.xml");
    

    What you do next depends on the class and method where you use this as an argument. If you check the API for the class & method where you are using the File as an argument, chances are, there will be a similar method that uses a URL.