매니페스트 파일의 메인 클래스가 아닌 Jar에서 클래스를 실행하는 방법
4 개의 클래스가있는 JAR이 있으며 각 클래스에는 Main 메서드가 있습니다. 필요에 따라 각각을 실행할 수 있기를 원합니다. Linux 상자의 명령 줄에서 실행하려고합니다.
E.g. The name of my JAR is MyJar.jar
기본 클래스의 디렉토리 구조는 다음과 같습니다.
com/mycomp/myproj/dir1/MainClass1.class
com/mycomp/myproj/dir2/MainClass2.class
com/mycomp/myproj/dir3/MainClass3.class
com/mycomp/myproj/dir4/MainClass4.class
내 매니페스트 파일에서 하나의 클래스를 기본으로 지정할 수 있다는 것을 알고 있습니다. 그러나 명령 행에서 인수를 지정하여 실행하려는 클래스를 실행할 수있는 방법이 있습니까?
나는 이것을 시도했다 :
jar cfe MyJar.jar com.mycomp.myproj.dir2.MainClass2 com/mycomp/myproj/dir2/MainClass2.class /home/myhome/datasource.properties /home/myhome/input.txt
그리고이 오류가 발생했습니다 :
com/mycomp/myproj/dir2/MainClass2.class : no such file or directory
위의 명령에서 '/home/myhome/datasource.properties'및 '/home/myhome/input.txt'는 명령 행 인수입니다.
매니페스트 파일에서 메인 클래스없이 항아리를 만들 수 있습니다. 그런 다음 :
java -cp MyJar.jar com.mycomp.myproj.dir2.MainClass2 /home/myhome/datasource.properties /home/myhome/input.txt
jar 파일에 정의 된 경우에도 JAR 파일 의 메소드 가있는 모든 클래스를 실행할 수 있습니다 .public final static main
Main-Class
메인 클래스 실행 :
java -jar MyJar.jar // will execute the Main-Class
public static void main
메소드를 사용하여 다른 클래스를 실행하십시오 .
java -cp MyJar.jar com.mycomp.myproj.AnotherClassWithMainMethod
참고 : 첫 번째는 -jar
, 두 번째는을 사용합니다 -cp
.
을 호출하는 것 외에도 java -jar myjar.jar com.mycompany.Myclass
매니페스트의 메인 클래스를 Dispatcher 클래스로 만들 수도 있습니다.
예:
public class Dispatcher{
private static final Map<String, Class<?>> ENTRY_POINTS =
new HashMap<String, Class<?>>();
static{
ENTRY_POINTS.put("foo", Foo.class);
ENTRY_POINTS.put("bar", Bar.class);
ENTRY_POINTS.put("baz", Baz.class);
}
public static void main(final String[] args) throws Exception{
if(args.length < 1){
// throw exception, not enough args
}
final Class<?> entryPoint = ENTRY_POINTS.get(args[0]);
if(entryPoint==null){
// throw exception, entry point doesn't exist
}
final String[] argsCopy =
args.length > 1
? Arrays.copyOfRange(args, 1, args.length)
: new String[0];
entryPoint.getMethod("main", String[].class).invoke(null,
(Object) argsCopy);
}
}
우선 jar
항아리를 만들고 실행하지 않습니다. java -jar
대신 시도하십시오 .
둘째, 왜 클래스를 FQCN ( com.mycomp.myproj.dir2.MainClass2
)과 파일 ( com/mycomp/myproj/dir2/MainClass2.class
) 로 두 번 전달 합니까?
편집하다:
java -jar
메인 클래스를 지정 해야하는 것처럼 보입니다 . java -cp your.jar com.mycomp.myproj.dir2.MainClass2 ...
대신 시도해보십시오 . -cp
jar를 클래스 경로에 설정하고 java가 기본 클래스를 조회 할 수있게합니다.
Another similar option that I think Nick briefly alluded to in the comments is to create multiple wrapper jars. I haven't tried it, but I think they could be completely empty other than the manifest file, which should specify the main class to load as well as the inclusion of the MyJar.jar to the classpath.
MyJar1.jar\META-INF\MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.mycomp.myproj.dir1.MainClass1
Class-Path: MyJar.jar
MyJar2.jar\META-INF\MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.mycomp.myproj.dir2.MainClass2
Class-Path: MyJar.jar
etc. Then just run it with java -jar MyJar2.jar
Add below plugin in your pom.xml file and specify the Main Class with fully qualified name in element.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.2.5.RELEASE</version>
<configuration>
<mainClass>**main Class name with full qualified name**</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
'Programing' 카테고리의 다른 글
PHP-객체 배열에서 객체 속성별로 항목 찾기 (0) | 2020.06.19 |
---|---|
file_get_contents ()에 시간 초과 설정이 있습니까? (0) | 2020.06.19 |
iPhone / iPad 장치에서 사용 가능한 총 여유 공간을 감지하는 방법은 무엇입니까? (0) | 2020.06.19 |
사용자 지정 지시문의 범위 바인딩에서 기호 '@', '&', '='및 '>'사용 : AngularJS (0) | 2020.06.19 |
여러 개의 제출 버튼 장고 양식을 어떻게 만들 수 있습니까? (0) | 2020.06.19 |