从命令提示符执行带有多个类路径库的 jar 文件 [英] Execute jar file with multiple classpath libraries from command prompt
问题描述
我有一个 Maven 项目,它生成一个 jar 文件并将所有依赖项复制到 target/lib
文件夹.我想在客户端的机器(windows)上执行这个项目.因此,我将 myproject.jar
复制到 C:xyz
文件夹并将所有依赖项复制到 C:xyzlib
文件夹.如何从客户端的命令提示符执行此项目?我尝试使用 C:xyz
文件夹中的 java -cp lib*.jar -jar myproject.jar
,但它抛出以下错误.
I have a Maven project which generates a jar file and copies all dependencies to target/lib
folder. I want to execute this project on client's machine (windows). So, I copied myproject.jar
to C:xyz
folder and all dependencies to C:xyzlib
folder. How do I execute this project from client's command prompt?
I tried to use java -cp lib*.jar -jar myproject.jar
from C:xyz
folder but it throws following error.
Exception in thread "main" java.lang.NoClassDefFoundError: libcommons-codec-1/3/jar
Caused by: java.lang.ClassNotFoundException: libcommons-codec-1.3.jar
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: libcommons-codec-1.3.jar. Program will exit.
我想如果我在类路径中指定所有依赖项(如 java -cp libdep1.jar;dep2.jar
),它会解决这个问题,但我不想这样做这是因为我已经有 40 个库并且它可能会在未来的版本中增长.有没有更好的方法来做到这一点?
I think if I specify all dependencies in classpath (like java -cp libdep1.jar;dep2.jar
), it will get rid of the problem but I don't want to do this as I have 40 libraries already and it might grow in future releases. Is there a better way to do this?
推荐答案
让 maven 生成一个批处理文件来启动您的应用程序.这是最简单的方法.
Let maven generate a batch file to start your application. This is the simplest way to this.
您可以使用 appassembler-maven-plugin这样的目的.
You can use the appassembler-maven-plugin for such purposes.
这篇关于从命令提示符执行带有多个类路径库的 jar 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!