2016-08-07 13 views
1

이 문제와 관련된 다른 stackoverflow 질문을 살펴 보았습니다.이 질문과 대답은 pom.xml의 부적절한 기본 클래스 설정과 관련이 있습니다. 올바른 패키지 이름과 적절한 대소 문자를 구분하기 위해 주 클래스를 작성했지만 내 항아리를 실행할 때마다 Could not find or load main class의 오류가 발생합니다.Java/Maven - 메인 클래스를 찾을 수 없거나로드 할 수 없습니다.

package com.willisjtc.maze; 

import io.vertx.core.AbstractVerticle; 
import io.vertx.core.Vertx; 

import org.apache.logging.log4j.LogManager; 
import org.apache.logging.log4j.Logger; 

public class VertxStarter extends AbstractVerticle { 

    private static final Logger logger = LogManager.getLogger(VertxStarter.class); 

    public static void main(String... args) { 
     Vertx vertx = Vertx.vertx(); 
     vertx.deployVerticle("ruby/webVerticle.rb"); 
     vertx.deployVerticle("ruby/mazeVerticle.rb"); 
    } 
} 

이 나는 ​​경우 얻고 무엇을 :

나는 GitHub의에 프로젝트를 추가 : https://github.com/quicksilversly/maze 내 받는다는 항아리 플러그인의 구성은 다음과 같습니다

configuration> 
    <archive> 
     <manifest> 
      <addClasspath>true</addClasspath> 
      <mainClass>com.willisjtc.maze.VertxStarter</mainClass> 
     </manifest> 
    </archive> 
</configuration> 

을 그리고 여기 내 주요 클래스 나는 java tvf target/jar-to-run.jar 실행 : 그래서 내 주요 클래스 I

597 Sat Aug 06 21:59:54 MDT 2016 META-INF/MANIFEST.MF 
    0 Sat Aug 06 21:59:54 MDT 2016 META-INF/ 
    0 Sat Aug 06 21:43:12 MDT 2016 com/ 
    0 Sat Aug 06 21:43:12 MDT 2016 com/willisjtc/ 
    0 Sat Aug 06 21:43:12 MDT 2016 com/willisjtc/maze/ 
    0 Sat Aug 06 21:43:18 MDT 2016 META-INF/maven/ 
    0 Sat Aug 06 21:43:18 MDT 2016 META-INF/maven/com.willisjtc/ 
    0 Sat Aug 06 21:43:18 MDT 2016 META-INF/maven/com.willisjtc/maze/ 
    0 Sat Aug 06 21:43:10 MDT 2016 ruby/ 
907 Sat Aug 06 21:43:12 MDT 2016 com/willisjtc/maze/VertxStarter.class 
220 Sat Aug 06 21:56:14 MDT 2016 META-INF/maven/com.willisjtc/maze/pom.properties 
2105 Sat Aug 06 21:56:14 MDT 2016 META-INF/maven/com.willisjtc/maze/pom.xml 
137 Sat Aug 06 21:43:10 MDT 2016 ruby/mazeVerticle.rb 
221 Sat Aug 06 21:43:10 MDT 2016 ruby/webVerticle.rb 

예상대로 거기에.

내 jar 파일을 실행하려면 무엇을 변경해야합니까?

+0

가능한 복제 (HTTP [I 메이븐을 사용 종속성이 실행 JAR을 만들 수 있습니까?] : // 유래. com/questions/574594/how-can-i-create-a-executable-jar-with-dependencies-using-maven) –

+0

이 문제와 관련하여 도움을 드릴 수 있도록 답변을 수락하거나 의견을 제공하는 것을 고려해보십시오. – jlars62

답변

0

java -jar output-jar-file.jar을 실행하면 main()io.vertx.core.AbstractVerticle, io.vertx.core.Vertx과 같은 외부 패키지를 찾을 수 없으므로로드 할 수 없습니다.

<addClasspath>true</addClasspath>을 사용하면 java는 모든 종속성이 동일한 디렉토리에서 실행 가능한 jar와 함께 표시 될 것으로 기대합니다. 실제로 모든 의존 관계는 .m2 디렉토리 안에 있습니다.

모든 종속성을 target dir로 복사하고 output-jar 파일의 클래스 경로로 선언해야합니다. pom.xml을 다음과 같이 변경하십시오 :

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <version>2.10</version> 
    <executions> 
     <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>copy-dependencies</goal> 
      </goals> 
      <configuration> 
      <outputDirectory>${project.build.directory}/lib</outputDirectory> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 
<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
    <version>3.0.2</version> 
    <configuration> 
     <archive> 
      <manifest> 
       <addClasspath>true</addClasspath> 
       <classpathPrefix>lib/</classpathPrefix> 
       <mainClass>com.willisjtc.maze.VertxStarter</mainClass> 
      </manifest> 
     </archive> 
    </configuration> 
</plugin> 
1

이것은 완벽하게 작동했습니다.

pom.xml 이러한 플러그인을 추가

<plugins> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
     </configuration> 
    </plugin> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-shade-plugin</artifactId> 
     <executions> 
      <execution> 
       <phase>package</phase> 
       <goals> 
        <goal>shade</goal> 
       </goals> 
       <configuration> 
        <filters> 
         <filter> 
          <artifact>*:*</artifact> 
          <excludes> 
           <exclude>META-INF/*.SF</exclude> 
           <exclude>META-INF/*.DSA</exclude> 
           <exclude>META-INF/*.RSA</exclude> 
          </excludes> 
         </filter> 
        </filters> 
        <transformers> 
         <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 

          <!--enter the name of your class here--> 
          <mainClass>YourPackage.YourMainClass</mainClass> 
         </transformer> 
        </transformers> 
       </configuration> 
      </execution> 
     </executions> 
    </plugin> 
</plugins> 

올바른 패키지와 클래스 이름으로 mainClass 요소를 변경해야합니다.

그런 다음 mvn package을 실행하여 jar 실행 파일을 빌드하십시오.

jar 파일은 /target 디렉토리 안에 있습니다.

그런 다음이 명령을 사용하여 명령 행에서 항아리를 실행할 수의

java -jar <NameOfJarFile>.jar

관련 문제