views:

1226

answers:

5

Hi

Our project in eclipse approximately 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 some exception: If we want to run our application.jar, we still need to copy the folder rsc/ in the same location as application.jar.

In rsc/, we have other folders for separation of the different parts. Basically, we load the pieces using the code (well a bit altered, but the path style is correct)

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

I could rightclick on rsc/ > Build Path > Use as Source Folder.

But this doesn't work either because eclipse doesn't automatically copy the rsc folder into the bin/-folder. And then we cannot run it anymore from terminal (not packaged into jar)

EDIT: why is eclipse unable to handle this correctly? Why does it have problems with "nested" output folders? In the Source-tab of the Build Path dialog, I could easily add the rsc/-folder with the output folder set to bin/rsc/, but it doesn't seem to be able to put nested folders...


EDIT 2: Now I've been able to create an xml-file to build the stuff with ant, and somehow managed to include the rsc-folder right into the jar. Still doesn't run due to the same errors. Do I really need to check the resources paths whether they're in a JAR or not? The JAR's content is now the following:

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 a stacktrace of around 100 lines.


anybody an idea how to do it? thx & regards

A: 

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

You could also use Ant.

Valentin Rocher
thx, I know that this kind of tools exist. But I haven't got any experience with them and my opinion is that if eclipse provides a tool to create executable jar's, I guess it should be possible to simply add another folder to the jar without modifying it.
Atmocreations
+1  A: 

You could try: In the Java Build Path dialog, go to the Libraries tab and select "Add Class Folder...". Select rsc from the Class Folder Selection dialog. The contents of the rsc directory will now be included in the executable jar file.

(Note that rsc itself won't be included, so you may have to add a new subdirectory under rsc to make the directory structure in the jar right.)

Ash
thx for your response. Well yes, that would work. But the problem is that I cannot move these objects into rsc/rsc/ because the local run (without the creation of the jar-file) wouldn't work anymore...
Atmocreations
A: 

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

Frank
+1  A: 

As you say in Edit 2 that you have all in one jar, this should work:

String path = "rsc/img/img.gif";
URL url = this.getClass().getResource(path);
ImageIcon icon = new ImageIcon(url);
rodrigoap
Thanks for your response. I have to say that "this.getClass()" is located in src/util/Will try it
Atmocreations
+1  A: 

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

Then you can use Class.getResource("/properties/Constants.properties") or Class.getResource("/rsc/properties/Constants.properties") and it will work regardless if your app is packaged as a jar or running from the source.

As a sidenote, the file: protocol works only with normal files, to access files inside the jar you'd have to use something like new URL("jar:file:app.jar!/rsc/properties/Constants.properties").

Dan Berindei
Yes. This was the solution, but marking rsc/ as src-folder in eclipse doesn't do the job. Another student led me right to the same solution. The problem was that we had to use Class.getResource("..."), the path prepended with a `/`. thx and regards
Atmocreations