java 在 jar 中包含库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16764834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Including libraries in jar
提问by dwjohnston
Ok, so here's what we've got:
好的,这就是我们所拥有的:
We've got two library packages, which we've compiled into jars.
我们有两个库包,我们已经将它们编译成 jar。
package starwars; public class JarJar { public void JarSayHello() { System.out.println("Jaaaaar!!"); } }
package barwars; public class BarBar { public void BarSayHello() { System.out.println("Baaaaa!!"); } }
We compile these with
我们编译这些
javac -d bin -sourcepath src src/barwars/BarBar.java jar cvf barwars.jar -C bin .
and
和
javac -d bin -sourcepath src src/starwars/JarJar.java jar cvf starwars.jar -C bin .
All nicely into jars for us.
都很好地装进了罐子里。
Now we want to include these two jars into another java project.
现在我们想将这两个 jars 包含到另一个 java 项目中。
so we've got
所以我们有
- /project/src/a_pack/HelloWorld.java
- /project/libs/starwars.jar
- /project/libs/barwars.jar
- /project/manifest.txt
- /project/src/a_pack/HelloWorld.java
- /project/libs/starwars.jar
- /project/libs/barwars.jar
- /项目/清单.txt
package a_pack; import starwars.JarJar; import barwars.BarBar; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); JarJar myJar = new JarJar(); myJar.JarSayHello(); BarBar myBar = new BarBar(); myBar.BarSayHello(); } }
Manifest.txt
清单.txt
Main-Class: a_pack.HelloWorld Class-Path: libs/starwars.jar libs/barwars.jar
Now we compile this with:
现在我们编译它:
javac -d bin -sourcepath src -cp "libs/starwars.jar;libs/*" src/a_pack/HelloWorld.java jar cvfm helloworld.jar manifest.txt -C bin .
And this compiles and runs fine.
这编译并运行良好。
Now I have two problems.
现在我有两个问题。
Firstly - if I move this jar file to somewhere else, and try run it, then I'll get:
首先 - 如果我将这个 jar 文件移动到其他地方,并尝试运行它,那么我会得到:
Exception in thread "main" java.lang.NoClassDefFoundError: starwars/JarJar
Exception in thread "main" java.lang.NoClassDefFoundError: starwars/JarJar
Now I can fix this by moving the libs folder to wherever I move the jar. But this strikes me as messy (what if there is already a libs folder in that location?).
现在我可以通过将 libs 文件夹移动到我移动 jar 的任何位置来解决这个问题。但这让我觉得很乱(如果那个位置已经有一个 libs 文件夹怎么办?)。
Ideally what I'd like to do, is include the referenced jars inside the jar, so there's one jar that contains everything that's required to run inside itself.
理想情况下,我想做的是将引用的 jar 包含在 jar 中,因此有一个 jar 包含在其内部运行所需的所有内容。
Is this possible? (And is it good practise?)
这可能吗?(这是好的做法吗?)
采纳答案by ccleve
Possible, yes. Good practice, no.
可能,是的。好的做法,不。
Jars are just zip files, so you can unzip and rezip to your heart's content. The bigger problem is managing all of these separate jars as your project gets larger.
罐子只是 zip 文件,因此您可以解压缩并重新压缩到您想要的内容。随着项目变大,更大的问题是管理所有这些单独的 jar。
Most projects do not compile using the command line. Instead, an IDE keeps your jars up to date. And most modern Java projects use Maven or Ivy to place jars in a repository and fish them out as needed.
大多数项目不使用命令行编译。相反,IDE 使您的 jar 保持最新。大多数现代 Java 项目使用 Maven 或 Ivy 将 jar 放入存储库并根据需要将它们取出。
Look at Eclipse, Netbeans, or Intellij for IDEs. And look into Maven for structuring your project.
查看 Eclipse、Netbeans 或 Intellij for IDE。并查看 Maven 以构建您的项目。