Eclipse: export to .jar and include resource files (ANT)

Our eclipse project roughly shows the following folders:

application - src - JRE System Library [1.6] - Referenced Libraries - lib - rsc 

In our project, we would like to use File> Export ...> Executable JAR

Well, that works fine, with a few exceptions: if we want to run our application.jar, we still need to copy the rsc / folder in the same place as application.jar.

In rsc / we have other folders to separate the different parts. Basically, we load fragments using the code (well, a bit has changed, but the style of the path is correct)

 strUrl = "file:rsc/properties/Constants.properties"; url = new URL(strUrl); ImageIcon icon = new ImageIcon(url); 

I could right-click on rsc / "> Build Path> Use as Source Folder.

But this does not work, because eclipse does not automatically copy the rsc folder to the trash / folder. And then we can no longer run it from the terminal (not packaged in a jar)

EDIT : why can't eclipse handle this correctly? Why is he having problems with "nested" output folders? On the Source tab of the Build Path dialog box, I could easily add an rsc / panel with the output folder set to bin / rsc /, but it doesn't seem to be able to put subfolders ...


EDIT 2 . Now I managed to create an xml file for assembling the material using ant and somehow managed to include the rsc folder directly in the jar. Still not working due to the same errors. I really need to check resource paths, are they in the JAR or not? The contents of the JAR are now as follows:

 META-INF/ META-INF/MANIFEST.MF configurator/ controller/ editor/ gui/ logic/ networking/ rsc/ rsc/gamesettings/ rsc/levels/ rsc/pics/ rsc/properties/ util/ 

but Java still hates me and throws java.io.FileNotFoundException: rsc/properties/Constants.properties with stacktrace for about 100 lines.


Does anyone know how to do this? thanks and considering

+7
eclipse executable-jar ant java-6
source share
5 answers

If you mark rsc as the source folder, Eclipse will copy its contents to the output directory. Or you can just move rsc under src if you want to group resources on output.

Then you can use Class.getResource("/properties/Constants.properties") or Class.getResource("/rsc/properties/Constants.properties") , and it will work regardless of whether your application is packaged as a jar or launched from the source.

As a side signal, the file: protocol only works with regular files, to access files inside the bank you need to use something like new URL("jar:file:app.jar!/rsc/properties/Constants.properties") .

+15
source share

You can try: in the "Java Build Path" dialog box, go to the "Libraries" tab and select "Add Cool Folder ...". Select rsc in the Select Folder dialog box. The contents of the rsc directory rsc now be included in the jar executable.

(Note that rsc itself will not be included, so you may need to add a new subdirectory under rsc to make the directory structure in the jar to the right.)

+4
source share

For this use, I would recommend using Maven , which automatically does this when creating a jar. There's even an Eclipse plugin .

You can also use Ant .

+2
source share

I usually do this manually, in your case jar -uf foo.jar rsc which includes all your resources. You can write a script or do it in ant

+1
source share

As you say in Edit 2 , that you have everything in one bank, this should work:

 String path = "rsc/img/img.gif"; URL url = this.getClass().getResource(path); ImageIcon icon = new ImageIcon(url); 
+1
source share

All Articles