1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

How To Make Executable JAR Files?

Discussion in 'Mac Programming' started by bobber205, Aug 7, 2006.

  1. macrumors 68020

    bobber205

    #1
    Below is the contents of a folder. I'm pretty confusded on how to use the

    Code:
    jar cvf 
    command.

    Thanks for any help.
     

    Attached Files:

    • pic.jpg
      pic.jpg
      File size:
      148.6 KB
      Views:
      731
  2. macrumors regular

    #2
    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.
     
  3. macrumors newbie

    #3
    jar

    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
     
  4. macrumors 68020

    bobber205

    #4
    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.
     
  5. macrumors 68020

    bobber205

    #5
    Code:
    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.
     
  6. macrumors 65816

    MarkCollette

    #6
    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

    or

    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 *"
     
  7. macrumors 68020

    bobber205

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

    Code:
    [I]209-181-58-139:~/JavaProjects/Klamath.net bobber205$ jar cvfm Manifest.mf *[/I]
    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)
    [I]209-181-58-139:~/JavaProjects/Klamath.net bobber205$ jar cvfm Manifest.mf *.class[/I]
    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?
     

    Attached Files:

  8. macrumors 68020

    bobber205

    #8
    This is getting almost silly. On my terminal I have this input:
    Code:
    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.

    Code:
    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
     
  9. macrumors 68020

    bobber205

    #9
    Aha!
    I did do it right!
    Look what is in that jar file I made in the terminal command above.
     

    Attached Files:

  10. macrumors 65816

    MarkCollette

    #10
    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.
     

Share This Page