[solved]Cannot access resources from exported jar

Hello,



I have a problem i really cant solve myself. For half a day now, i've been trying to export my jME project to a jar, so i can show it to others.

I've read the wiki articles about the thing and i managed to start the application, but the program does not see the resources the jar contains. In eclipse they work fine, when i export them they are screwed, however the path seems to be correct. I'd really appretiate some guiding words on this. Thanks.

Tell us how the path seems to be correct!

Show us an example.

I like the way you do business clovis 8)



This is the first resource it gets to:

URL terrain=State.class.getClassLoader().getResource("jmetest/data/texture/terrain/test/map.raw");



it whines:

com.jme.system.JmeException: height file not found: file:/D:/Prog/Projects/001.jar!/jmetest/data/texture/terrain/test/map.raw



if i play around with dirs:

URL terrain=State.class.getClassLoader().getResource("./jmetest/data/texture/terrain/test/map.raw");



then it throws the good old nullpointerexception.

try to make use of the ResourceLocator like here

Core-Dump said:

try to make use of the ResourceLocator like here


no luck:( still throws the same error. I double-checked the path it should be working.

well do you really want to access the resources in jmetest/data?

if so, did you also add the jmetest-data*.jar's to your project ?

The jar is a kind of fat jar(eclipse 3.4.0). It has almost everything exported. It has jmetest in it, and the path to map.raw too.

Have you opened the jar (with winrar for example) to make sure the file is really there?

Guys, i wouldnt be here if i hadnt double-checked everything. The path is valid. The resource is there, the problem is with accessing it within the jar.

Core-Dump, you've just hit the Jackpot :smiley:

Well actually just half of it…

You were right, the getfile() method was the problem, i turned off terrain, and got further.

The next load is however with resourcelocator, and it throws nullpointerexception. That's what made me think its a resource loading problem. If i turn it off too the application loads successfully. If you can solve that too, i will be disappearing from your life i promise:)


MultiFormatResourceLocator locator=new MultiFormatResourceLocator(Unit.class.getClassLoader()
                            .getResource("models/md5/textures/"),new String[]{".tga",".jpg"});
ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, locator);

Okay, made it. Let me explain for our future descendants who might read this:



The trick is that naked directory names are not valid URLs in jar files. If you try to make one using the classloader stuff you get null. However you can point to a file in that directory. If you do that you can get the desired directory name by doing some String wizardry:


URL path=Unit.class.getClassLoader().getResource("models/md5/textures/kecske.jpg");
         String str=path.toString();
         str=str.substring(0, str.length()-10);
         
         path=new URL(str);
         
         MultiFormatResourceLocator locator=new MultiFormatResourceLocator(path, new String[]{".tga",".jpg"});
         ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, locator);



Core-Dump, this is gonna remain at half of the jackpot.
The Jackpot is a Big thanks, so here is your share: THA

yay!

i didn't quite understand your problem with the  not valid URL's in jar files, i think as long as the directory is in the classpath it should find it.

but i'm struggling myself with those things often enough.

Right. I messed up a bit:) The URL is valid, but Classloader seems to be buggy, and cannot make URL from a directory path in jar files.

Perhaps a different thing, but I've seen a similar issue before with directories if you don't put a / at the end of the url.

bump, the getFile() issue is not fixed yet  :slight_smile:


Patched in svn.