how to create a runnable jar file from command line similar to eclipse

13,149

there's no way do this kind of thing from the jar command line utility. there are 3 main solutions to the dependencies problem:

  1. in your MANIFEST.MF you may reference other jar files as part of your classpath and then people could invoke your code using java -jar [just your jar] and your dependencies will be picked up
  2. you can package your code and dependencies into a fat jar (which is what youre describing). usually you just convert your project to use some build tool (more advanced than simple javac and jar) and configure the tool. for example with maven. see 2 different approaches here and here
  3. (again with a build tool) you can use something better than a fat jar - use one-jar. the difference is that with a fat jar you run the risk of files with the same name in different libraries "colliding"
Share:
13,149
Satya
Author by

Satya

Updated on June 04, 2022

Comments

  • Satya
    Satya almost 2 years

    I understood that eclipse generates a runnable jar file with all library .jar files extracted and included in the jar file. How do I do the similar this while creating a jar file manually from command prompt. Do I need to extract all the lib jars into classes folder?

    Currently I am using -

        jar cvfm example.jar manifest.txt -C  Classes/ .
    

    Which creates a jar file with all the class files and I need to have all the library jars in the same folder as this example.jar exists.