Eclipse Build/Run issue

Started by
6 comments, last by Ph4tM4N 14 years, 1 month ago
Hello everyone! I'll cut right to the chase. I have a program which I have previously exported to an executable jar file, and had it run perfectly. I have since added files which are used to generate levels (*.wld) files, and code to generate these levels using these files. The files are properly included in the Jar file (I verified this), however, at runtime, I get this exception message: "file:C:\<unnecessary junk omitted>\MYJAR.jar!\worlds\world2.wld (The filename, directory name, or volume label syntax is incorrect)" The code runs just fine when I execute it within Eclipse, but the executable jar file consistently crashes. What could be causing this?
Advertisement
also, I'd settle for a link to somewhere that has instructions, or just instructions on how to go about manually building a jar (command line stuffs) so's I can see if it's eclipse's problem or my code's.
Quote:
The code runs just fine when I execute it within Eclipse, but the executable jar file consistently crashes. What could be causing this?


The path to the file. I'm not completely sure about this, but - you are using relative paths right? The path to your .wld when running in eclipse is not the same as when executing the .jar.

When running in eclipse the project executes from the root of your project folder:
MyProject/res/models/*.wld

You would then use the path "res/models/*.wld" in your code.

However when running the .jar, the program executes from the path where you run java -jar ... At the same time, your .jar i probably has MyProject as root folder, thus the path your should be using is "MyProject/res/models/*.wld".

As I said, I'm not completely sure about the last part, but the "can read the file in Eclipse, but not 'outside' Eclipse" is almost always a path problem.
you have to change your code were you are loading your wld files.

it should be like <Any classname in your jar>.class.getResourceAsStream("\worlds\world2.wld")

and set the jar in your java project's classpath, so that it will also work when launched from eclipse.
Sorry if this should have code tags or something, this post editor appears to be from the stone age.


Here is a snippet of the code that is throwing the exception:



URL url = this.getClass().getClassLoader().getResource(ref);
try {
Scanner scan = new Scanner(new File(url.getFile()));
width = scan.nextInt();
<code omitted>
} catch (Exception e) { // yes, i know, i catch all exceptions here. so sue me.
JOptionPane.showMessageDialog(game.getGameWindow(), e.getMessage(), "World Input Error",JOptionPane.ERROR_MESSAGE );
e.printStackTrace();
System.exit(-1);
}

EDIT: At failtime, the value of ref is a String with the value ""worlds/world2.wld""
Quote:Original post by Ph4tM4N
Sorry if this should have code tags or something, this post editor appears to be from the stone age.

There is a "source" tag, with square brackets.

Quote:Original post by Ph4tM4N
yes, i know, i catch all exceptions here. so sue me.

That's pretty much the only sensible way of dealing with exceptions in Java games without completely polluting your code with exception handlers, IMHO. I usually catch and rethrow as RuntimeException with custom message, then log it on the top level.

Edit: nevermind what I said about your problem, but please triple-check the paths (path to Jar, and path within Jar) against the stack trace. Also, please post the stack trace here.

Quote:Original post by Ph4tM4N
EDIT: At failtime, the value of ref is a String with the value ""worlds/world2.wld""

Also try if it works with an absolute path: "/worlds/world2.wld"
Quote:Original post by Ph4tM4N
URL url = this.getClass().getClassLoader().getResource(ref);
Scanner scan = new Scanner(new File(url.getFile()));

If you're loading resources from a jar on the classpath then it's generally a bad idea to try and access them via a File object, use the Url or getResourceAsStream directly:
Scanner scan = new Scanner( getClass().getClassLoader().getResourceAsStream(ref) );

This will work both unpacked via Eclipse and when packed in a jar file and should solve your issue.
Thank you OrangyTang! that worked marvelously. I figured that the file object was unnecessary, but I find it difficult to find good documentation online on the subject of file IO covering multiple aspects of it.

This topic is closed to new replies.

Advertisement