명령 프롬프트에서 여러 클래스 경로 라이브러리가있는 jar 파일 실행
jar 파일을 생성하고 모든 종속성을 target/lib
폴더에 복사하는 Maven 프로젝트가 있습니다. 이 프로젝트를 클라이언트 컴퓨터 (Windows)에서 실행하고 싶습니다. 그래서, 복사 myproject.jar
할 C:\xyz
폴더 및 모든 종속 C:\xyz\lib
폴더에 있습니다. 클라이언트의 명령 프롬프트에서이 프로젝트를 어떻게 실행합니까? 내가 사용하려고 java -cp lib\*.jar -jar myproject.jar
에서 C:\xyz
폴더하지만 오류 다음 발생합니다.
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.
클래스 경로에 모든 종속성을 지정하면 (예 java -cp lib\dep1.jar;dep2.jar
:) 문제가 제거되지만 이미 40 개의 라이브러리가 있고 향후 릴리스에서 확장 될 수 있으므로이 작업을 수행하고 싶지 않습니다. 이 작업을 수행하는 더 좋은 방법이 있습니까?
maven이 애플리케이션을 시작하기 위해 배치 파일을 생성하도록합니다. 이것이 가장 간단한 방법입니다.
이러한 목적으로 appassembler-maven-plugin 을 사용할 수 있습니다 .
-jar
및 -cp
명령 줄에서 둘 다 사용할 수 없습니다. 다음을 사용 하는 경우 Java 설명서 를 참조하십시오 -jar
.
JAR 파일은 모든 사용자 클래스의 소스이며 다른 사용자 클래스 경로 설정은 무시됩니다.
다음과 같이 할 수 있습니다.
java -cp lib\*.jar;. myproject.MainClass
을 주목 ;.
에 -cp
자바 명령 줄 버그를 해결하려면 인수입니다. 또한 이것은 Windows 버전의 명령입니다. Unix에서 경로 구분자는 :
입니다.
UNIX에서 Java 1.7 사용-
java -cp myjar.jar:lib/*:. mypackage.MyClass
Windows에서는 ';'를 사용해야합니다. 대신에 ':' -
java -cp myjar.jar;lib/*;. mypackage.MyClass
OS에 관계없이 아래 명령이 작동합니다.
java -cp "MyJar.jar;lib/*" com.mainClass
항상 따옴표를 사용하고 lib / *. jar 가 작동하지 않는다는 점에주의하십시오 .
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>
가능한 해결책은
배치 파일 생성
그 안에있는 모든 파일에 대해 lib 디렉토리에서 루프를 수행하고 클래스 경로에서 lib를 제외하고 각 파일을 설정합니다.
그런 다음 항아리를 실행하십시오.
몇 가지 옵션이 있습니다.
가장 쉬운 방법은 exec 플러그인 일 것 입니다.
어셈블리 플러그인을 사용하여 모든 종속성을 포함하는 jar를 생성 할 수도 있습니다 .
마지막으로 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
This will not work java -cp lib\*.jar -jar myproject.jar
. You have to put it jar by jar.
So in case of commons-codec-1.3.jar
.
java -cp lib/commons-codec-1.3.jar;lib/next_jar.jar
and so on.
The other solution might be putting all your jars to ext
directory of your JRE. This is ok if you are using a standalone JRE. If you are using the same JRE for running more than one application I do not recommend doing it.
ReferenceURL : https://stackoverflow.com/questions/13018100/execute-jar-file-with-multiple-classpath-libraries-from-command-prompt
'UFO ET IT' 카테고리의 다른 글
Eclipse javadoc 배경색이 검정색입니다. (0) | 2021.01.17 |
---|---|
xcode 4.5의 릴리스 빌드를 위해 armv6 및 armv7을 모두 지원하는 방법 (0) | 2021.01.17 |
avcapturesession으로 카메라 전환 (0) | 2021.01.17 |
Angular 2에서 양식 필드를 동적으로 추가하고 제거하는 방법 (0) | 2021.01.17 |
Apache 사용자의 umask 설정 (0) | 2021.01.17 |