• FEATURED

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

16 replies to this topic

### #1warnexus  Prime Members

Posted 12 February 2013 - 10:32 PM

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.

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?

Edited by warnexus, 12 February 2013 - 10:40 PM.

### #2Bacterius  Members

Posted 13 February 2013 - 12:56 AM

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.”

### #3Khatharr  Members

Posted 13 February 2013 - 02:47 AM

You could have an installer which reinstates proper file associations

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.

### #4angelic999  Members

Posted 13 February 2013 - 03:42 AM

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.

### #5PurpleAmethyst  Members

Posted 13 February 2013 - 06:17 AM

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:

### #6dmatter  Members

Posted 13 February 2013 - 07:58 AM

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

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.

### #7shacktar  Members

Posted 13 February 2013 - 09:23 AM

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.

Edited by shacktar, 13 February 2013 - 09:40 AM.

### #8Glass_Knife  Moderators

Posted 13 February 2013 - 10:09 AM

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.

Edited by Glass_Knife, 13 February 2013 - 10:09 AM.

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

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

### #9warnexus  Prime Members

Posted 13 February 2013 - 10:38 AM

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.

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.

Edited by warnexus, 13 February 2013 - 11:24 AM.

### #10warnexus  Prime Members

Posted 13 February 2013 - 10:44 AM

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.

Edited by warnexus, 13 February 2013 - 10:55 AM.

### #11warnexus  Prime Members

Posted 13 February 2013 - 11:07 AM

If you're using Eclipse then you can go to "File->export->Java->Runnable Jar file" to create an executable Jar.

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.

I followed your instructions but I got export errors which is odd because my game runs fine without errors on Eclipse.

Here's what export errors showed.

I'm not providing the source code to the user. All source code been compiled into .class files. I actually provided what my batch file is doing above. The batch file is basically a .txt that says "java -cp  NicholasGame.jar Game". what exactly does "-cp" mean? I know the batch file is one way which I already performed before making this thread but I have used other people jar files and all I needed to do was click the jar file and run the application and they did not have any .class files with the jar file

Edited by warnexus, 13 February 2013 - 11:07 AM.

### #12warnexus  Prime Members

Posted 13 February 2013 - 11:23 AM

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.

Now it seems I cannot execute the jar unless it has the src file(which has the .class files) along side with it. I think when I use other people jar files, the programmers already put the src file inside the jar file. I want to know how I can do that.

If there is nothing wrong for the user to click the batch file then I will just let them do that. The reason why I did not want to do it that way was because I did not need to run a batch file when I used other people's jar files.

### #13Glass_Knife  Moderators

Posted 13 February 2013 - 11:48 AM

what exactly does "-cp" mean?

The -cp flag is short for -classpath, which tells the java command where to look for the class files. If you didn't put *.jar file in the classpath, then Java would not be able to find the code.

The source files do not have anything to do with the jar file working. A jar file is just a *.zip file renamed to *.jar. You could create the jar file as a zip file by hand, including the source code, and then change the extension to *.jar.

However, most people, for creating jar files like this, either do it from the command line or use http://ant.apache.org/

Something looks weird with your class files. They need to be in the package folders in the jar, and you shouldn't be using classes without at least one package. In the past, I have had trouble getting code without any packages to work like this. It seems like something is wrong, because I see your game is called Game, but I also see a Game folder in the jar screenshot.

One thing, is that the main class needs the fully qualified java name. For example, a typical manifest for my games looks like this:

Main-Class: tim.game.app.ReallyAwesomeGame

// ReallyAwesomeGame.java
package tim.game.app; // VERY IMPORTANT!!!

When compiled there with be a folder for each package level, and the Really AwesomeGame file must be in the package structure inside the jar file.

tim
- game
- app
ReallyAwesomeGame

If this stuff isn't exactly right, it won't work.

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

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

### #14warnexus  Prime Members

Posted 13 February 2013 - 12:54 PM

Something looks weird with your class files. They need to be in the package folders in the jar, and you shouldn't be using classes without at least one package. In the past, I have had trouble getting code without any packages to work like this. It seems like something is wrong, because I see your game is called Game, but I also see a Game folder in the jar screenshot.

Game.class is the entry point. I have another folder called "Game" and that contains an image for the gameover. I guess I should change the name of the folder to avoid confusion

Edited by warnexus, 13 February 2013 - 01:20 PM.

### #15Glass_Knife  Moderators

Posted 13 February 2013 - 01:40 PM

The last time I tried to create an executable jar with classes that we not in any packages, I couldn't get it to work. It seemed like a bug, but putting them in at least one package can't hurt.

Try it and see.

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

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

### #16warnexus  Prime Members

Posted 14 February 2013 - 10:10 AM

The last time I tried to create an executable jar with classes that we not in any packages, I couldn't get it to work. It seemed like a bug, but putting them in at least one package can't hurt.

Try it and see.

when you're talking about one package, can I use the default package or should I create my own package? I will try this.

### #17Glass_Knife  Moderators

Posted 14 February 2013 - 10:33 AM

when you're talking about one package, can I use the default package or should I create my own package? I will try this.

create at least one package. The name doesn't really matter. Long ago, in the Java 4.1 days, I tried created a Jar with files that had no package (because I was being lazy and didn't want to create a folder) but it didn't work. That may have been fixed. I've always used at least one package since, and have never tried it without one.

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