how to create a runnable jar file from command lin

2019-04-09 11:04发布

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.

1条回答
劳资没心,怎么记你
2楼-- · 2019-04-09 11:39

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"
查看更多
登录 后发表回答