View Full Version : How To Make Executable JAR Files?

Aug 7, 2006, 11:31 PM
Below is the contents of a folder. I'm pretty confusded on how to use the

jar cvf command.

Thanks for any help.

Aug 8, 2006, 09:40 AM
I see you are using BlueJ. If you click File, create jar then it allows you to specify your main class and make the jar file executable. If you specifically need to do it from the command line then you'll need to ask someone else.

Aug 8, 2006, 11:46 AM
jar [flags] files

you need a manifest text file with the following line:

Main-Class: YourMainClass

(There are other attributes you can set as well)

Create a Jar

jar cvmf yourmanifest.txt *

* adds all of the files in the current directory

expand a jar

jar xvf yourjar.jar

the v flag only causes the files found to be printed to stdout

Aug 8, 2006, 12:51 PM
I don't really understand your instructions... I'm sorry. Maybe something more specific?

I tried having BlueJ make the jar file but I keep getting that "MainCanvas" is not found errors.

Aug 8, 2006, 01:08 PM
jar cfm MyJar.jar Manifest.txt MyPackage/*.class

I would be able to do what that code is telling me to do, but I don't know what to substitute in for the MyPackage/*.class part. I don't have a package...

Help is apprecaited.

Aug 8, 2006, 02:17 PM
First, find out what your main class is, the one whose public static void main(String args[]) method is called. It doesn't neccesarily have to have a package name. It could be something like "com.blah.Game" or simply "Test"

Make a manifest file, which is a plain text file typically called "Manifest.mf". Add a single line to that file that looks like either of:

Main-Class: com.blah.Game


Main-Class: Test

(as was explained above for finding your main class' name.

Then just do like hsvmoon explained, and do the following:

1. Open a terminal and cd to wherever the .class files are, or the root of where they go. If you don't use package names, then cd to where all the .class files are. If you do use packages, then go to the directory where the packages begin, so if your package name is "com.blah", go to the parent directory of the "com" directory

2. Type "jar cvfm path-to-Manifest.mf *"

Aug 8, 2006, 02:43 PM
Still some trouble.
I've attached the manifest file and the whole java file.

I did what you said. Here's what is in my Terminal window right now.

209-181-58-139:~/JavaProjects/Klamath.net bobber205$ jar cvfm Manifest.mf *
java.io.FileNotFoundException: Classes (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at sun.tools.jar.Main.run(Main.java:122)
at sun.tools.jar.Main.main(Main.java:903)
209-181-58-139:~/JavaProjects/Klamath.net bobber205$ jar cvfm Manifest.mf *.class
java.io.IOException: invalid header field
at java.util.jar.Attributes.read(Attributes.java:389)
at java.util.jar.Manifest.read(Manifest.java:167)
at java.util.jar.Manifest.<init>(Manifest.java:52)
at sun.tools.jar.Main.run(Main.java:123)
at sun.tools.jar.Main.main(Main.java:903)
209-181-58-139:~/JavaProjects/Klamath.net bobber205$

Does that make any sense?

Aug 8, 2006, 03:06 PM
This is getting almost silly. On my terminal I have this input:
209-181-58-139:~/JavaProjects/Klamath.net bobber205$ jar cmfv Manifest.mf example.jar *.class
added manifest
adding: KlamathWebCam$mainCanvas.class(in = 8325) (out= 7458)(deflated 10%)
adding: KlamathWebCam$refreshAction.class(in = 1240) (out= 666)(deflated 46%)
adding: KlamathWebCam$startUpdating.class(in = 1382) (out= 814)(deflated 41%)
adding: KlamathWebCam$startUpdatingThread.class(in = 1337) (out= 782)(deflated 41%)
adding: KlamathWebCam$stopAction.class(in = 1373) (out= 746)(deflated 45%)
adding: KlamathWebCam.class(in = 3867) (out= 2017)(deflated 47%)
adding: KlamathWebCamOLD.class(in = 1834) (out= 947)(deflated 48%)
adding: MainCanvas.class(in = 702) (out= 462)(deflated 34%)
adding: refreshAction.class(in = 628) (out= 389)(deflated 38%)

In there I am told that $mainCanvas was added. Whee. But when I try to run my class
this comes up.

Exception in thread "main" java.lang.NoClassDefFoundError: KlamathWebCam$MainCanvas

I'm no longer getting a "failure to load manifest attribute" error anymore. I think I've done that part at least right. The main canvas is the first to be loaded in my class. That must be why it's the one that keeps coming up.

Thanks for any help! I am desperate! :D

Aug 8, 2006, 03:14 PM
I did do it right!
Look what is in that jar file I made in the terminal command above.

Aug 9, 2006, 03:05 PM
When you use inner classes, the filename might look like "Outer$Inner.class", but the classname is still "Outer.Inner", which is what should be in the Manifest.mf file.