简体   繁体   English

从命令提示符执行具有多个类路径库的jar文件

[英]Execute jar file with multiple classpath libraries from command prompt

I have a Maven project which generates a jar file and copies all dependencies to target/lib folder. 我有一个Maven项目,该项目生成一个jar文件并将所有依赖项复制到target/lib文件夹。 I want to execute this project on client's machine (windows). 我想在客户端计算机(Windows)上执行该项目。 So, I copied myproject.jar to C:\\xyz folder and all dependencies to C:\\xyz\\lib folder. 因此,我将myproject.jar复制到C:\\xyz文件夹,并将所有依赖项复制到C:\\xyz\\lib文件夹。 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. 我试图从C:\\xyz文件夹中使用java -cp lib\\*.jar -jar myproject.jar ,但它引发以下错误。

Exception in thread "main" java.lang.NoClassDefFoundError: lib\commons-codec-1/3/jar
Caused by: java.lang.ClassNotFoundException: lib\commons-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: lib\commons-codec-1.3.jar.  Program will exit.

I think if I specify all dependencies in classpath (like java -cp lib\\dep1.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. 我想如果我在classpath中指定所有依赖项(例如java -cp lib\\dep1.jar;dep2.jar ),它将摆脱此问题,但由于已有40个库,所以我不想这样做在将来的版本中增长。 Is there a better way to do this? 有一个更好的方法吗?

You cannot use both -jar and -cp on the command line - see the java documentation that says that if you use -jar : 您不能在命令行上同时使用-jar-cp请参阅说明使用-jar 的Java文档

the JAR file is the source of all user classes, and other user class path settings are ignored. JAR文件是所有用户类的源,而其他用户类路径设置将被忽略。

You could do something like this: 您可以执行以下操作:

java -cp lib\\*.jar;. myproject.MainClass

Notice the ;. 注意;. in the -cp argument, to work around a Java command-line bug. -cp参数中,以解决Java命令行错误。 Also, please note that this is the Windows version of the command. 另外,请注意,这是命令的Windows版本。 The path separator on Unix is : . Unix上的路径分隔符是:

Using java 1.7, on UNIX - 在UNIX上使用Java 1.7-

java -cp myjar.jar:lib/*:. mypackage.MyClass

On Windows you need to use ';' 在Windows上,您需要使用';' instead of ':' - 代替 ':' -

java -cp myjar.jar;lib/*;. mypackage.MyClass

Let maven generate a batch file to start your application. 让maven生成一个批处理文件以启动您的应用程序。 This is the simplest way to this. 这是最简单的方法。

You can use the appassembler-maven-plugin for such purposes. 您可以将appassembler-maven-plugin用于此类目的。

Regardless of the OS the below command should work: 无论使用哪种操作系统,以下命令均应起作用:

java -cp "MyJar.jar;lib/*" com.mainClass

Always use quotes and please take attention that lib/*.jar will not work. 始终使用引号,并请注意lib / *。jar将不起作用。

You can use maven-assembly-plugin, Here is the example from the official site: https://maven.apache.org/plugins/maven-assembly-plugin/usage.html 您可以使用maven-assembly-plugin,这是来自官方网站的示例: https : //maven.apache.org/plugins/maven-assembly-plugin/usage.html

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5.1</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
                <manifest>
                    <mainClass>your main class</mainClass>
                </manifest>
            </archive>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id> <!-- this is used for inheritance merges -->
                <phase>package</phase> <!-- bind to the packaging phase -->
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

a possible solution could be 一个可能的解决方案可能是

create a batch file 创建一个批处理文件

there do a loop on lib directory for all files inside it and set each file unside lib on classpath 在lib目录中对其中的所有文件进行循环,并在classpath上将每个文件都设置在lib旁

then after that run the jar 然后运行罐子

source for loop in batch file for info on loops 批处理文件中循环的以获取循环信息

There are several options. 有几种选择。

The easiest is likely the exec plugin . 最简单的可能是exec插件

You can also generate a jar containing all the dependencies using the assembly plugin . 您还可以使用Assembly插件生成一个包含所有依赖项的jar。

Lastly, you can generate a file with the classpath in it using the dependency:classpath goal. 最后,您可以使用dependency:classpath目标生成一个包含类路径的文件。

I was running into the same issue but was able to package all dependencies into my jar file using the Maven Shade Plugin 我遇到了同样的问题,但是能够使用Maven Shade插件将所有依赖项打包到我的jar文件中

This will not work java -cp lib\\*.jar -jar myproject.jar . 这将无法使用java -cp lib\\*.jar -jar myproject.jar You have to put it jar by jar. 你必须把它一个罐一个罐地放。

So in case of commons-codec-1.3.jar . 因此,在commons-codec-1.3.jar情况下。

java -cp lib/commons-codec-1.3.jar;lib/next_jar.jar and so on. java -cp lib/commons-codec-1.3.jar;lib/next_jar.jar等。

The other solution might be putting all your jars to ext directory of your JRE. 另一个解决方案可能是将所有jar放入JRE的ext目录。 This is ok if you are using a standalone JRE. 如果您使用的是独立JRE,则可以。 If you are using the same JRE for running more than one application I do not recommend doing it. 如果您使用同一JRE来运行多个应用程序,则不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在命令提示符下运行jar文件。 Java无法在类路径中找到库 - Running jar file in command prompt. Java can't find libraries in classpath 从Java执行命令提示符的命令以创建jar文件 - Execute command prompt's command from java to create jar file 无法从命令提示符下引用类路径中的jar运行Java类 - unable to run java class from command prompt refering to a jar in classpath 如何从命令提示符制作jar文件 - How to make jar file from command prompt 从命令提示符加载外部文件,而不是从classpath使用 - load external file from command prompt instead of using from classpath 使用自定义清单创建jar,其中包含classpath中的多个库 - create jar with custom manifest with multiple libraries in classpath 通过命令提示符从具有多个JAR依赖项的JAR运行类 - Running class from JAR with multiple JAR dependencies via command prompt 从命令行运行 JAR 文件并指定类路径 - Run a JAR file from the command line and specify classpath (JAVA)使用命令提示符从多个.class文件创建.jar文件 - (JAVA) Use Command Prompt to create .jar file from multiple .class files 使用从java程序调用的Windows命令提示符,将带有多个连续空格的字符串作为参数传递给jar文件 - Pass a string with multiple contiguous spaces as a parameter to a jar file using Windows command prompt called from a java program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM