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

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

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
     
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    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
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    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
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Thank you again. This file is "written by programmers for programmers." So the number of files doesn't matter.
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    hi!!
    you should all use the "Fat Jar" plugin...it's automagical
    http://fjep.sourceforge.net/
     
    author
    Posts: 5856
    7
    Android Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Report post to moderator
    Myriam, please do not reply to 3-year-old posts.
     
    I promise I will be the best, most loyal friend ever! All for this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
      Bookmark Topic Watch Topic
    • New Topic