• 15
• 15
• 11
• 9
• 10

# Eclipse Build/Run issue

This topic is 2925 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites

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.

##### Share on other sites
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:

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""

##### Share on other sites
Quote:
 Original post by Ph4tM4NSorry 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 Ph4tM4Nyes, 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 Ph4tM4NEDIT: 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"

##### Share on other sites
Quote:
 Original post by Ph4tM4NURL 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.