*
The moose likes IDEs, Version Control and other tools and the fly likes Creating .jar files (preferably in Eclipse) and adding existing .jar files to them Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Reply locked New topic
Author

Creating .jar files (preferably in Eclipse) and adding existing .jar files to them

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
I have an application I would like to export as a .jar. I know how to export a .jar file using
  • project->
  • export->
  • export as jar->
  • etc.
  • . . . but how do you add existing .jar files to it? I have imported them into the Eclipse classpath, and the app works on Eclipse. But when I try exporting it as a .jar it starts and doesn't finish. On the command line I get a NoClassDefFoundError.
    This might be because text file I need has got out of place, or it is more likely that I haven't got a jar (called java_cup) in my output jar.

    How can I add this pre-existing jar to my output jar? I would prefer to use Eclipse.

    CR
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    I expect the background behind this question is that you want to create an executable jar file. In that case the way to "add" another jar file to your executable jar file is not to put it inside the executable jar file, but to mention it in a Class-Path entry in your executable jar's manifest.

    I don't think Eclipse will generate the manifest for you, at least not to this level of detail (but I could be wrong). So it's up to you to create the manifest file yourself and identify the main class and the other jars you want in the class path. It's also up to you to put those other jars in the right place relative to your executable jar, before you run it at the command line.

    Here is a link to Sun's tutorial about jar files.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38519
        
      23
    Thank you.

    I shall try that, and report back whether I have had any luck.

    So far the manifest file reads something like
    Manifest-Version: 1.0

    Main-Class: uk.co.critchie.eiffel.test.EiffelFileReader


    possible with more returns after it.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38519
        
      23
    Thank you. It took me a bit of time to work it out . . .

    You have to write a manifest file, either using a text editor, or with the "new->untitled text file" option on Eclipse. This is what I wrote
    Manifest-Version: 1.0
    Class-Path: java-cup-11a.jar java-cup-11a-runtime.jar
    Main-Class: uk.co.critchie.eiffel.test.EiffelFileReader
    . . . remembering to put a "return" at the end of the last line.

    Then you go through the usual procedure for creating a .jar:
  • Right-click the project in "package" on the left.
  • Export
  • Java->Jar File
  • Click on name of project
  • After the page where it says "packaging"->next
  • Mark the radio button where it says "use existing manifest"
  • Navigate to the manifest file, then "finish"

  • You need to put all the required files into the directory where the .jar file is.
    Bingo. It is all working.



    Thank you
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38519
        
      23
    As I said yesterday, it works nicely. I can say
    java -jar EiffelParsing.jar
    and it runs. But I still have to put the other two .jars (java-cup and java-cup-runtime) in the same folder.

    Is it possible to add those .jars to my .jar file?

    CR
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    Putting a couple of jars in a folder isn't that onerous, is it? I've seen this question numerous times but I have never understood why it's asked. Maybe you could enlighten me.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38519
        
      23
    No, it isn't hard at all. In fact that is what I have got and it works nicely. But I can't double-click my jar to run it, and it would look nicer with a single file . . .

    If it's not possible, it's not possible . . .

    And thank you again for your help yesterday.
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    If double-clicking the jar file doesn't run it properly, then (assuming you're on Windows) your file associations for the .jar extension aren't set up to do "java -jar X.jar". Or if it is set up that way, then something else in your configuration isn't set up right. Distributing only one jar file isn't going to help that problem.

    Almost every piece of software I've installed for the last several years has come via an installer. Nowadays if you send out some files and say "Put these files in a directory" then you look like an amateur anyway; whether you send one file or five doesn't make much difference. The only exceptions are programs written by programmers for programmers, and there I don't think the number of files matters either.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38519
        
      23
    Thank you again. This file is "written by programmers for programmers." So the number of files doesn't matter.
    Myriam Leggieri
    Greenhorn

    Joined: Jul 11, 2009
    Posts: 1
    hi!!
    you should all use the "Fat Jar" plugin...it's automagical
    http://fjep.sourceforge.net/


    People waste health to make money and then waste money to recover health. (Dalai Lama)
    Peter Johnson
    author
    Bartender

    Joined: May 14, 2008
    Posts: 5823
        
        7

    Myriam, please do not reply to 3-year-old posts.


    JBoss In Action
     
     
    subject: Creating .jar files (preferably in Eclipse) and adding existing .jar files to them