编译依赖于jar的打包项目
对我来说,这似乎是一个微不足道的问题,但我很难得到答案。
我在 eclipse 中开发了一个项目,该项目依赖于 jar 文件,该文件位于项目的根目录中。我的所有文件都位于 src 文件夹中的包“abc”中。它在 eclipse 中运行得很好。我现在想从命令行运行这个项目。我执行以下命令来编译项目:
javac -classpath dependency.jar -d ./bin/ ./src/a/b/c/*.java
所有内容都编译成类文件并放入 bin/a/b/c 文件夹中。然后我执行以下命令来运行该项目:
光盘
java -cp ../dependency.jar abcMain
现在我得到“java.lang.NoClassDefFoundError:a/b/c/Main”。
那么,如何运行一个包中依赖于 jar 文件的项目呢?
This seems to me to be a trivial question, but I've had a lot of trouble getting an answer.
I've developed a project in eclipse that is dependent on a jar file, which resides in the project's root directory. All my files are in a package "a.b.c" in a src folder. It runs just fine in eclipse. I now want to run this project from the command line. I do this command to compile the project:
javac -classpath dependency.jar -d ./bin/ ./src/a/b/c/*.java
Everything is compiled into class files and put into the bin/a/b/c folder. Then I do these commands to run the project:
cd bin
java -cp ../dependency.jar a.b.c.Main
Now I get "java.lang.NoClassDefFoundError: a/b/c/Main".
So, how do I run a project that is in a package and depends on a jar file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将当前目录也包含在类路径中 - 即 java -cp ../dependency.jar:。 abcMain
Just include the current dir in the classpath as well - i.e.
java -cp ../dependency.jar:. a.b.c.Main
您还需要在类路径上指定已编译的文件,这些文件将包含您的
abcMain
。在 *nix 风格的机器上,cp 的路径分隔符是冒号 (:
),在 Windows 上它是分号 (;
),因此在 *nix 上,您的运行命令应该是(因为你是从 bin 目录运行):You also need to specify your compiled files on the classpath, these will contain your
a.b.c.Main
. On *nix flavor machines the path separator for cp is the colon (:
), and on windows it's a semicolon (;
), so on *nix, your run command should be (because you're running from the bin directory):