Java Maven exec 插件 ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20705914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:42:39  来源:igfitidea点击:

Maven exec plugin ClassNotFoundException

javamaven

提问by Barry

I'm using the Maven exec plugin to run a java application from the command line with the command mvn exec:java. I have specified the main class in the pom.xml and the associated dependencies.

我正在使用 Maven exec 插件通过命令 mvn exec:java 从命令行运行 Java 应用程序。我已经在 pom.xml 中指定了主类和相关的依赖项。

<groupId>com.example.MyApp</groupId>
<artifactId>MyApp</artifactId>
<version>1.0.0</version>
<build>
  <plugins>
     <plugin>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>exec-maven-plugin</artifactId>
       <version>1.2.1</version>
       <executions>
         <execution>
           <goals>
             <goal>java</goal>
          </goals>
         </execution>
       </executions>
       <configuration>
          <mainClass>com.example.myclass</mainClass>
          <arguments>
            <argument>configFile</argument>
            <argument>properties</argument>
          </arguments>
       </configuration>
     </plugin>

I also specify a number of dependencies...

我还指定了许多依赖项...

<dependencies>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>com.example.MyLibrary</groupId>
    <artifactId>MyLibrary</artifactId>
    <version>1.0.0</version>
</dependency>

The MyAppprogram reads a config file which is passed in as a command line argument. The config file contains the name of a class which is located in MyLibrary. So the class could be com.mypackage.driver.MyClasswhich is located in MyLibrarywhich is a dependency of the MyAppjar listed above. However when I try to run this I get a ClassNotFoundException...

MyApp程序读取作为命令行参数传入的配置文件。配置文件包含位于MyLibrary. 因此,该类可能com.mypackage.driver.MyClass位于上面列出MyLibraryMyAppjar的依赖项中。但是,当我尝试运行它时,我得到了一个ClassNotFoundException......

Update---- I'm using the system classloader to load the classes which are passed in on the command line for the MyAppprogram

更新----我正在使用系统类加载器来加载在MyApp程序命令行中传入的类

ClassLoader loader = ClassLoader.getSystemClassLoader();

I'm thinking that this is causing the problem as it is looking for the classes on the default classpath which does not contain the dependencies.

我认为这是导致问题的原因,因为它正在寻找不包含依赖项的默认类路径上的类。

Any hints on what I'm doing wrong here?

关于我在这里做错了什么的任何提示?

采纳答案by Caleb

Are you still looking for an answer to this question? I had the exact same issue, and finally figured it out.

你还在寻找这个问题的答案吗?我遇到了完全相同的问题,终于弄清楚了。

You need to add includePluginDependenciesto your configuration to make the plugin search your dependencies for the main class:

您需要将includePluginDependencies添加到您的配置中,以使插件搜索主类的依赖项:

<configuration>
  <includePluginDependencies>true</includePluginDependencies>
  <mainClass>com.example.myclass</mainClass>
  <arguments>
    <argument>configFile</argument>
    <argument>properties</argument>
  </arguments>
</configuration>

See here: http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html#includePluginDependencies

请参阅此处:http: //mojo.codehaus.org/exec-maven-plugin/java-mojo.html#includePluginDependencies

回答by Moritz Petersen

You can let the classpath get generated like this:

您可以让类路径像这样生成:

    <configuration>
      <executable>java</executable>
      <arguments>
        <argument>-Dmyproperty=myvalue</argument>
        <argument>-classpath</argument>
        <!-- automatically creates the classpath using all project dependencies,
             also adding the project build directory -->
        <classpath/>
        <argument>com.example.Main</argument>
        ...
      </arguments>
    </configuration>

回答by jjmartinez

Before to execute from command line, you should import also in command line your library. You can use this command with your expecific name and info of your library:

在从命令行执行之前,您还应该在命令行中导入您的库。您可以将此命令与您的库的特定名称和信息一起使用:

mvn install:install-file -Dfile=MyLibrary.jar -DgroupId=com.example.MyLibrary -DartifactId=MyLibrary -Dversion=1.0.0 -Dpackaging=jar

回答by Jens Baitinger

You need to add the dependency as dependency of the execution plugin, so the execution plugin can load the class you configured com.example.myclass:

您需要将依赖添加为执行插件的依赖,以便执行插件可以加载您配置的类com.example.myclass

<plugins>
 <plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>exec-maven-plugin</artifactId>
   <version>1.2.1</version>
   [...]
   <dependencies>
     <dependency>
       <groupId>com.example.MyLibrary</groupId>
       <artifactId>MyLibrary</artifactId>
       <version>1.0.0</version>
       <type>jar</type>
     </dependency>

 </plugin>