Executable Jar File not Working...

Started by
7 comments, last by Glass_Knife 9 years, 3 months ago

Hello guys

I have just developed a hangman game using Swing for the GUI.

The game works 100% in Eclipse when I run it. The game has GUI so you are not "playing" on the console. It has panels,buttons and everything.

Now about the problem...

When I export the game to an executable jar, I double click and nothing happens...

Maybe I do something wrong? Maybe I need to add something extra on my code??

Thanks in advance ^^ and Happy New Year!!!

Failure is not an option...

Advertisement

Step one: Run the jar from the command line and post the error message:


C:\>java -jar yourjar.jar

I'm not sure what Eclipse is doing that's breaking it, but without an error message you'll just be guessing.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

It says: Error: Unable to access jarfile hang.jar

(the file name is: hang)

Failure is not an option...

That message appears to indicate that no file called "hang.jar" exists. Are you viewing with file extensions enabled? Is the file just called "hang", or does it have an extension?

You can use the "dir" command to see the contents of the directory you are in.

I had the file on the wrong place to get opened by cmd... So now it "opens"

It says:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive

And it says this problem is on this code block


public String randomWord(){
	Random r = new Random();
	return words.get(r.nextInt(words.size()));
}

Maybe Random() might give some negative numbers? which it should clearly not cause there no negative index for arraylist?

(I dont get any warning from eclipse in the console window)

24wc8l3.png

EDIT: Problem gets solved if I place the jar to the folder where I have my images and txt files. Is there a possible way for me to make the jar run on its own?

Failure is not an option...

That almost certainly means words.size() is zero, and the Random.nextInt documentation clearly states the bound you provide must be strictly positive. I'm guessing you are loading a file, and that file isn't found when you launch your program from the command-line, perhaps because the working directory is not the same. Can you show your project directory layout, and the code that populates the "words" list?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Well the jar only works if its in a folder where my txt file is. Also when I gave the jar and the needed files to my friend it did not work for him...

Neither on my other pc(I got Java RE installesd). It seems that the jar only works for my pc right now :S .

Failure is not an option...

Can you get any error messages from the other computers too?

It looks like you've hard-coded the save file location to be the same director as where the jar is launched. This won't work.

I could make a script (*.sh or *.bat) to launch the jar file from anywhere on my system. The current working directory will be the location of my script, not the location of the jar.

Check out the System.getProperty( "user.home" ); method to find and save the file in the user's home directory. You'll also need to check if the file is there, and if not, either save a default or at least log an error message that your game can't access the save file. Your game should still start up even if the file isn't there. Sometimes the user's home directory will be protected and you won't be able to write to it (read-only). You'll at least want to let the user know, not just shut down the game.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

This topic is closed to new replies.

Advertisement