簡體   English   中英

Maven 構建失敗類NotFoundException

[英]Maven build failure classNotFoundException

這是我第一次嘗試 maven,我不明白為什么每次嘗試構建時我都會遇到 classnotfoundexception。 這是我收到的錯誤:

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------< owmapi:owmapi >----------------------------
[INFO] Building OwmAPICase 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ owmapi ---
[WARNING] 
java.lang.ClassNotFoundException: owmapi.Main
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:435)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:246)
    at java.base/java.lang.Thread.run(Thread.java:832)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.582 s
[INFO] Finished at: 2021-02-28T14:09:17+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:java (default-cli) on project owmapi: An exception occured while executing the Java class. owmapi.Main -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

這是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>owmapi</groupId>  <!-- case.se.ctk -->
    <artifactId>owmapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>OwmAPICase</name>

     <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties> 

    <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <mainClass>owmapi.Main</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.aksingh/owm-japis -->
        <dependency>
            <groupId>net.aksingh</groupId>
            <artifactId>owm-japis</artifactId>
            <version>2.5.3.0</version>
        </dependency>
    </dependencies>
</project>

如果我只是在 eclipse 中將項目作為 java 應用程序運行,那么一切正常,但由於某種原因,它不適用於 maven。 我想問題出在我的 pom 中的 mainClass 上,但我嘗試了幾種解決方案都無濟於事。

我認為您的主要 class 對 pom.xml 中提到的pom.xml有一些依賴關系。 您只是在創建一個不包含這些依賴項的目標 jar。 您需要創建包含所有相關依賴項的 uber/fat jar。 您可以使用以下插件maven-assembly-plugin來創建目標 jar。

假設: Main.java class 在package owmapi下。

<build>
    <finalName>owmapi</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>owmapi.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM