How to run a Java jar file without a MS DOS batch file

Started by
15 comments, last by Glass_Knife 11 years, 2 months ago

So I have been working on my game for a little over a month. Even with college, I spend as much time as I can on the game to be bug-free as possible and make sure it is important it has the right mechanics of that particular genre.

Now I want to share it with people and make sure they can play it by clicking the jar file instead of the MS DOS batch file I created. This would mean the src folder will not be given along side with the Java jar file.

directory_zps0692df20.png

I know for certain it is possible seeing as I have downloaded and use other people jar files and their jar files do not come out a src folder or a batch file. How did they do it?

Advertisement

What does your batch file contain? I'd expect this to be out of your control, because for instance, WinRAR typically associates itself with .jar files by default, which means if the user has his system settings set to user WinRAR (or something else) in precedence to Java for .jar files, double-clicking the file will not run the game, and there's not much you can do about that. You could have an installer which reinstates proper file associations, but that is *very* unusual practice for a game.

In this case, though, it seems your file associations are correct, I believe you need to write an additional method - probably to set the entry point - to be able to run from the .jar without invoking java directly (which I guess is what your batch file is doing) but I don't know any further off the top of my head. Did you try reading this?

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

You could have an installer which reinstates proper file associations

angry.png

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

You could add/edit the jar manifest to include/specify which class is the startign point.

See http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html for more details.

If file associations are correctly set up I would recommend using OneJar (http://one-jar.sourceforge.net/). I've used this for prototypes. It can even load native libraries such as LWJGL.

Other solutions you could use are:

If you're using Eclipse then you can go to "File->export->Java->Runnable Jar file" to create an executable Jar. If you're not using Eclipse then look for something similar in your IDE. If you're not using an IDE then I highly recommend getting one tongue.png

As for shipping the source code - whether you make a runnable JAR or use a batch file you shouldn't need to include a copy the source code. When delivering a batch file all you need to ship with it are the compiled .class files and your batch script can invoke Java on those. My guess is that your current batch file is invoking javac (the java compiler) and compiling the source on the fly? Or perhaps you don't realise that once the .class files are generated Java doesn't require the .java source files to actually run the program.

One tool I would recommend for this purpose is Launch4j.

@warnexus It looks like you're targeting Windows, but if you target other platforms you could build one executabe wrapper for each platform.

I once had a problem where I created an executable JAR file that worked by double-clicking on my computer but it didn't work on another computer. It could have been an issue involving a JRE difference. The other advantages of using a tool such as Launch4j is that JRE differences are handled automatically (you either bundle your JRE version or you set a min and max version) and you don't have to worry about the user having to set the Java path.

Now I want to share it with people and make sure they can play it by clicking the jar file instead of the MS DOS batch file I created. This would mean the src folder will not be given along side with the Java jar file.

I'm just wondering if you are having a problem with the OS not executing the jar file when clicked, or are you not setting the Main-Class: in the manifest file of the class path. As pointed out above, if the user doesn't have the file associations configured, there is not much you can do. However, there is nothing wrong with having a batch file or shell script to launch your program. ANT does this, for example.

Using launch software to generate an executable for the target OS to launch the Java code behind the scenes seems like overkill for a small 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

What does your batch file contain? Did you try reading this?

I actually did come across the same link but my jar cannot execute unless it has the src folder that has the .class files. when I use other people's jar file. I do not even see the .class files only just one .jar file to execute the program.

My batch file is basically a .txt file with these commands: Here's a picture of what is inside the .txt

On a side note I do not understand what "-cp" means.

batch_zpsec4cb42f.png

I already written the manifest. then I tried this command java -jar NicholasGame.jar in the command prompt and it worked but for the average user this would be weird to do. If I execute my jar file without the src folder my application becomes like this.

blank_zps0222e1df.png

You could add/edit the jar manifest to include/specify which class is the startign point.

See http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html for more details.

I did add the Manifest. Here is what the Manifest looked like. Should it be in the src folder or outside of the src folder? But the manifest did not help me access the jar file. I even tried to java -jar JAR-name (ie java -jar NicholasGame.jar )on the command prompt and it worked but for the average user this is weird to perform just to open a game.

manifest_zps0c7b2f74.png

This topic is closed to new replies.

Advertisement