URI is not hierarchical

Hey,
I got a problem with runnable jars that I export with Eclipse.

VM Call:

[quote]java -Djava.library.path=D:\Users\Dario\Desktop\native\windows -jar gametest.jar
[/quote]
Console log:

http://img7.imagebanana.com/img/dyjrpr27/URInothierarchical.PNG

Exception points to:

public static void main(String[] args) {
		try {
			AppGameContainer app = new AppGameContainer(new Game());
			app.setIcon("i/default.png");
			app.setDisplayMode(1024, 768, false);
			app.setTargetFrameRate(30);
			app.setMouseGrabbed(true);
			app.start(); // LINE 31
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}

Everything is working fine in Eclipse. Any suggestions? =)

That’s not where the exception points to.

Cas :slight_smile:

Ah, thanks, the actual trigger is Game.java:47.

try {
			for (String n : new File(ClassLoader.getSystemResource("i").toURI()).list()) // LINE 47 // For every image file in src\i
				i.put(n.substring(0, n.indexOf('.')), new Image("i/" + n)); // load and put to ArrayList<Image> i
			i.put("clickable1", i.get("clickable0").getFlippedCopy(true, false));
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}

I’m still looking for help with bypass that exception.

How about you print out what those URIs look like after you’ve done all that monkeying with them? I have a feeling it will be quite obvious once you see them.

Cas :slight_smile:

Debug code:

	try {
			System.err.println(ClassLoader.getSystemResource("i").toURI().toString());
		} catch (URISyntaxException e1) {
			e1.printStackTrace();
		}

Result in Eclipse:
file:/D:/Users/Dario/Desktop/space/Devrays/bin/i

Result as runnable jar:
jar:file:/D:/Users/Dario/Desktop/gametest.jar!/i

You can’t create a File from a jar: URI. Check the JavaDoc for File - the URI must have a scheme equal to file (ie. begin file:)

Quick Googling found this http://stackoverflow.com/questions/1429172/list-files-inside-a-jar-file - might be useful.

Or just have a plain text file in the JAR with a list of all your images and parse that. Depends on where you’re running - the above requires a security permission.

Best wishes, Neil

Jar entry listing works in theory, but this would mean that I can’t execute my projects through Eclipse anymore.
Is there no way for both Eclipse debugging and exportet .jars? =/

Use the index file that nsigma mentioned, and open all your resources with getResourceAsStream(). Resource location problems pretty much melt away when you do that.

I prefer to not create a txt with an image index. This does not seem to be very productive and slows down the developement. I finally created a data initialization that automatically checks if it is a jar or not during the runtime and chooses the correct way to load files, works perfect for me:

try {
			if (Game.class.getResource("/" + Game.class.getName().replace('.', '/') + ".class").toString().startsWith("file"))
				loadFiles();
			else
				loadResources();
		} catch (Exception e) {
			e.printStackTrace();
		}
static void loadFiles() {

		try {
			for (String n : new File(ClassLoader.getSystemResource("i").toURI()).list())
				i.put(n.substring(0, n.indexOf('.')), new Image("i/" + n));
		} catch (URISyntaxException | SlickException e) {
			e.printStackTrace();
		}

		try {
			for (String n : new File(ClassLoader.getSystemResource("f").toURI()).list()) {
				if (n.endsWith("t")) {
					n = n.substring(0, n.indexOf('.'));
					f.put(n, new AngelCodeFont("f/" + n + ".fnt", "f/" + n + ".png"));
				}
			}
		} catch (URISyntaxException | SlickException e) {
			e.printStackTrace();
		}

	}
static void loadResources() {
		try {
			Enumeration<JarEntry> en = new JarFile("gametest.jar").entries();
			while (en.hasMoreElements()) {
				JarEntry n = (JarEntry) en.nextElement();
				String name = n.getName();
				if (name.startsWith("i") && name.indexOf('.') > -1)
					i.put(name.substring(2, name.indexOf('.')), new Image(name));
				if (name.endsWith("fnt"))
					f.put(name.substring(2, name.indexOf('.')), new AngelCodeFont(name, name.substring(0, name.indexOf('.')) + ".png"));
			}
		} catch (IOException | SlickException e) {
			e.printStackTrace();
		}
	}

“jar:” is not a recognized URI prefix, meaning you can not be able to create a File object of anything inside a zipped file (jar).

You can however use the ZipInputStream class and its related classes to read file entries inside a zipped file :slight_smile:

JarFile et al extends ZipFile et al!

btw, I mentioned File doesn’t support the jar scheme already. :wink:

Well excuuuuuse me :slight_smile: