• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Game Tutorials -->> Creating An Executable JAR File

 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that's what the word "jar" does to you, Dirk, what do we get when we say "pot" ?
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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& )
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also be sure not to put any blank lines between
Main-Class: Foo
and
Class-Path: FooFrameJar.jar
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes, exactly.




Yes, exactly *what* ???!!!
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic