Programing

매니페스트 파일의 메인 클래스가 아닌 Jar에서 클래스를 실행하는 방법

lottogame 2020. 6. 19. 19:22
반응형

매니페스트 파일의 메인 클래스가 아닌 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 mainMain-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 ...대신 시도해보십시오 . -cpjar를 클래스 경로에 설정하고 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>

참고URL : https://stackoverflow.com/questions/5474666/how-to-run-a-class-from-jar-which-is-not-the-main-class-in-its-manifest-file

반응형