Explanation for creating a .jar needed for the ultra .jar noob

Discussion in 'Mac Programming' started by Jiddick ExRex, May 22, 2008.

  1. Jiddick ExRex macrumors 65816

    Jiddick ExRex

    Joined:
    May 14, 2006
    Location:
    Roskilde, DK
    #1
    And by noob, I mean myself.

    I have this project in Eclipse that is depending on the framework of another project. File structure is:
    ~Documents/Eclipse Workspace/
    ________
    MakiSearch/ (main project, contains a src, bin and images folder).
    piccolo/ (depending framework).

    I need to make the main project into an executable .jar file that can be run on other computers.
    Basically I have tried Eclipse's way of making jars, and also through the terminal by: jar cf MakiSearch.jar * in the MakiSearch folder. I do get a .jar out if but it cannot be executed, gives me an error:
    "5/22/08 2:53:16 PM [0x0-0x16c16c].com.apple.JarLauncher[35243] Failed to load Main-Class manifest attribute from ~/Documents/Eclipse workspace/MakiSearch/MakiSearch.jar "

    Do I need to somehow specify the .class file which contains the main method? How?
    Do I need to jar both the main project and the depending project?
     
  2. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #2
    Yes, and yes.

    Specifying the main class requires creating a small text file with this information, as well as an empty line at the end for it to be parsed properly. To add this to the JAR file you add the m option, and the name of the text file BEFORE the name of the JAR file - for example: jar cmf manifest.txt MakiSearch.jar *

    It's easiest for your users if you either JAR the dependency inside the main JAR or include it in a second library JAR distributed with your application.

    To specify the main class you need to fully qualify its name - this also won't work unless you're in the directory outside all the package directories. For example, if your project structure looks like this:
    ~/Documents/Projects/Test (main project folder)
    ~/Documents/Projects/Test/build/manifest.txt (file with main class info)
    ~/Documents/Projects/Test/build/mypackage (folder where all the .class files live)
    You would cd to the build folder (NOT the mypackage folder), then use this command to make the JAR file:
    jar cmf manifest.txt Test.jar mypackage/*
     
  3. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #3
  4. foidulus macrumors 6502a

    Joined:
    Jan 15, 2007
    #4
    You can create a Manifest file and run the jar command

    which works alright, but if you are going to be doing this task a number of times, I would investigate ant, more specifically ant jar tasks. Ant is an automated way to build your project and works great with eclipse. It will require a bit of an investment up front, but it will be well worth it in my opinion. Plus, it saves you from having to learn the syntax of manifest files.
     

Share This Page