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

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.

exporterrors_zpsf8567826.png

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

Advertisement

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.

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 Twitter: https://twitter.com/indieprogram

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

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

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 Twitter: https://twitter.com/indieprogram

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

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.

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

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

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

This topic is closed to new replies.

Advertisement