aspose file tools*
The moose likes Beginning Java and the fly likes Game Tutorials -->> Creating An Executable JAR File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Game Tutorials -->> Creating An Executable JAR File" Watch "Game Tutorials -->> Creating An Executable JAR File" New topic
Author

Game Tutorials -->> Creating An Executable JAR File

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
This is part of the package of Game Tutorials threads, for which a complete listing of threads can be found in The Table Of Contents.
Let's create a JAR file such that double clicking on the file will cause our desired Java application to run.
Where do we begin to learn about JAR files? The JAR Files trail of Sun's Java Tutorial.
What is a jar file? A file format that enables us to bundle multiple files into a single archive file and it can provide the function of running a specified application when the jar file is double clicked (assuming that Java 1.2 or newer is properly installed on the user's machine).
Sounds nice, let's make one already.
OK, we'll need an application to include. Here's one that displays a Frame when executed (and the Frame can be closed):
It compiles and runs just fine. Let's add it to a JAR file. How do we do this? The Lesson on Using JAR Files: The Basics tells just what to do:
jar cf jar-file input-file(s)
So, let's name our JAR file FooJar.jar and add our single class file:
jar cf FooJar.jar Foo.class
No error messages, it must have done something successfully. Indeed, a new JAR file named FooJar.jar was created and using my favorite zip tool, I see the contents include two files - our class file and the expected manifest file. If you weren't expecting that manifest file, then read up on JAR files in the aforementioned trail of Sun's Java Tutorial.
So, does it work yet? I mean, can we just double click on it and our application executes? Try it. Did a Frame appear? The answer is no. Then let's try it at the command line to see what error messages might be generated:
java -jar FooJar.jar
What happened? Don't take my word for it. Try it and see what error message is generated. Now you know what that error message might mean.
What's next then? After reading up a bit on Creating a JAR File it would seem that we need to learn a bit more on just what that manifest file does and how we can tell it to do what we want.
The Understanding the Manifest Lesson indicates that we need to specify the Main-Class header (as did our previous exploration into error messages).
That's fine and dandy, so how do we edit the Manifest File? With a zip tool such as PowerArchiver or WinZip, it's pretty easy. But let's do it the Java way - from the command line. The Modifying a Manifest File Lesson gives us some good ideas (such as exactly what to do).
Here are the contents of our text file that we want included in the Manifest File:
FooFest.txt

That's it. Now let's create our new JAR file (after deleting our previously created jar file):
jar cmf FooFest.txt FooJar.jar Foo.class
No error messages and a new JAR file was created. But it still doesn't work. Where did we go wrong. Examining the Manifest File in our new JAR file reveals that it does not include the contents of the text file we specified. Why not? Ah, further down the page on Modifying a Manifest File is a red warning: "The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return."
In that case let's add a carriage return to our text file (two of them, even, so as to emphasize the point):
FooFest.txt

[Note that the two blank lines might not display properly on this Bulletin Board]
Delete the old JAR file to make room for the new. Then:
jar cmf FooFest.txt FooJar.jar Foo.class
Survey says... double click and... a Frame appears! And running the JAR file from the command line produces no error messages. Success!
For further reading, check out The Documentation on the Manifest Format.
Any questions?
[ June 05, 2003: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
So now you want to declare a package in your main class file. Sure, all your friends are doing it, why not?
Suppose, the main class definition is now:Compile the Foo.java file and place the class file in a subdirectory named foobar (just like the package declaration). Adjust the Main-Class setting in Text.txt to be foobar.Foo and create the JAR file like this:
jar cmf FooFest.txt FooJar.jar foobar/Foo.class
FooJar.jar should have been succesfully created and you should now be able to double-click it or type java -jar FooJar.jar to run it.
[ February 24, 2003: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Use of multiple class files in one JAR file is possible.
Compilation of this codeshould result in two class files: Foo.class and Foo$1.class
Use of the same Manifest file
FooFest.txtand the following command
jar cmf FooFest.txt FooJar.jar Foo.class Foo$1.class
should result in the creation of the executable JAR file FooJar.jar that executes successfully when double-clicked.
What should we explore next?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
What's next?

Loading an image from a JAR file is the winner.

Note that loading an image from a JAR file is a little bit different than loading an image from an unjarred file.

Save the moose head image at the top left of this page in a directory name "images".

In a different directory named "fubar", compile the following source files.

Foo.javaImagePanel.java

Create and save a file named manifest.txt with the following contents. (Don't forget to include at least one blank line at the end of the file.)



Now, your project directory structure should be as follows.



From the directory that holds fubar, images, and manifest.txt, run the following command.

jar cmf manifest.txt FooJar.jar fubar/* images/*

This should have created a file name FooJar.jar with the same contents as the above directory structure, plus a directory named "META-INF" with a file name "MANIFEST.MF" inside of it.

Delete everything but this FooJar.jar file.

You should be able to execute the JAR with the following command.

java -jar FooJar.jar

A frame should appear, likely without any image displayed. Just resize the frame to trigger a repaint, and the moose head image should appear.

For a description of the java.net.URLClassLoader used to load the image, take a look at the "Using JAR-related APIs Introduced in 1.2" lesson of Sun's Java Tutorial.

What's next?
[ August 20, 2004: Message edited by: Dirk Schreckmann ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If that's what the word "jar" does to you, Dirk, what do we get when we say "pot" ?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
My grandfather used to keep a jar under his bed so he wouldn't have to make the trip to the outhouse on those cold winter nights.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Can you run two applications from the same jar file?

Main-Class: Service1

Main-Class: Service2

Main-Class: Service3

or must I make a fourth app to contain the other three?

What if I want to run more than one instance of Service2?
(for example in Linux I would just say
java Service1&
java Service2&
java Service2&
java Service3& )


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923



After reading the first post, I thought: Yes!
And what's about packages?
And the next post was the answer.
And I thougth: Aha!
But what's about nested packages?
And the next post was the answer.
And I thougth: Well done!
But what's about images in jars?
And the next post...
Good job!

* The pitfall of the trailing blank:

Main-Class: Foo_<-here

* The directory switch:
jar ... -d ...

* Pitfalls: Invoking jar from wrong directory:


* jar and ant
* jar and eclipse
* Double-click jar in KDE
* Double-click jar in GNOME
* Double-click jar in Win95,98,00,ME,NT,XP,03,LongHorn

btw: what does it have to do with games?
[ June 10, 2004: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Also be sure not to put any blank lines between
Main-Class: Foo
and
Class-Path: FooFrameJar.jar
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Marilyn de Queiroz:
Can you run two applications from the same jar file?

Main-Class: Service1

Main-Class: Service2

Main-Class: Service3

or must I make a fourth app to contain the other three?

What if I want to run more than one instance of Service2?
(for example in Linux I would just say
java Service1&
java Service2&
java Service2&
java Service3& )


To be clear, are you trying to figure out whether you can specify something in the manifest of the JAR that would invoke multiple applications, or even duplicate instances of an application, when executing the JAR?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Originally posted by Dirk Schreckmann:

To be clear, are you trying to figure out whether you can specify something in the manifest of the JAR that would invoke multiple applications, or even duplicate instances of an application, when executing the JAR?

Yes, exactly.
Razvan MIHAIU
Ranch Hand

Joined: Nov 12, 2004
Posts: 43
Yes, exactly.



Yes, exactly *what* ???!!!


Regards,<br /><a href="http://www.mihaiu.name" target="_blank" rel="nofollow">Razvan</a><br /> <br /> <br />SCJP PREPARATION MATERIAL:<br /><a href="http://www.mihaiu.name/2004/sun_java_scjp_310_035/index.html" target="_blank" rel="nofollow">SCJP 310-035</a><br /><a href="http://www.mihaiu.name/2004/sun_java_scjp_310_035_test1/index.html" target="_blank" rel="nofollow">SCJP 310-035 T1</a><br /><a href="http://www.mihaiu.name/2004/sun_java_scjp_310_035_test2/index.html" target="_blank" rel="nofollow">SCJP 310-035 T2</a><br /><a href="http://www.mihaiu.name/2004/sun_java_scjp_310_035_test3/index.html" target="_blank" rel="nofollow">SCJP 310-035 T3</a><br /><a href="http://www.mihaiu.name/2004/sun_java_scjp_310_035_test4/index.html" target="_blank" rel="nofollow">SCJP 310-035 T4</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Game Tutorials -->> Creating An Executable JAR File