【问题标题】:How to run java class in JAR file in maven target directory?如何在 Maven 目标目录的 JAR 文件中运行 java 类?
【发布时间】:2011-11-25 19:49:29
【问题描述】:

我想在构建 maven 之后运行我的 Izpack 安装程序,但在执行“mvn test”后我得到以下输出:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building RS IzPack installer
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
[debug] execute contextualize
[INFO] [resources:copy-resources {execution: copy-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 109 resources
[INFO] Copying 4 resources
[INFO] Preparing exec:java
[WARNING] Removing: java from forked lifecycle, to prevent recursive invocation.
[debug] execute contextualize
[INFO] [resources:copy-resources {execution: copy-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 109 resources
[INFO] Copying 4 resources
[INFO] [exec:java {execution: default}]
[WARNING]
java.lang.ClassNotFoundException: com.izforge.izpack.installer.Installer
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
        at java.lang.Thread.run(Thread.java:595)
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. com.izforge.izpack.installer.Installer

看起来我必须以某种方式将生成的 jar 文件放入类路径中,有什么想法吗?

摘自我的 pom.xml:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
      <execution>
        <phase>test</phase>
        <goals>
          <goal>java</goal> <!-- "exec" also possible -->
        </goals>
        <configuration>
          <mainClass>com.izforge.izpack.installer.Installer</mainClass>
          <arguments>
            <argument>-console</argument>
            <!-- <argument>arg1</argument> -->
          </arguments>
        </configuration>
      </execution>
    </executions>
  </plugin>

Apache Maven 2.2.1 (r801777; 2009-08-06 21:16:01+0200) Java版本:1.6.0_20 Java 主页:C:\Java\jdk16\jre 默认语言环境:en_GB,平台编码:Cp1252 操作系统名称:“windows xp” 版本:“5.1” 架构:“x86” 系列:“windows”

马丁

【问题讨论】:

    标签: java maven-2 maven izpack exec-maven-plugin


    【解决方案1】:

    你看过罐子里面了吗?可能是 maven 没有在 jar 中包含需要的类。

    【讨论】:

    • 是的,我查看了我的 target\Installer-3.0.0-SNAPSHOT-standard.jar 并且类 com.izforge.izpack.installer.Installer 在 jar 中。通过运行“java -jar Installer-3.0.0-SNAPSHOT-standard.jar”从 cmd 行执行它没有问题。只是从 maven 做同样的事情是有问题的。
    【解决方案2】:

    我认为您应该使用-classpath 定义java 命令的类路径。您需要构建一个包含您的主类com.izforge.izpack.installer.Installer 及其所有依赖项的类路径。它可以在一个 jar 中,也可以是一个类文件夹或多个 jar。请参阅Wikipedia 了解如何为 java 调用定义类路径。

    【讨论】:

      【解决方案3】:

      你可以使用类似的东西来定义执行的依赖:

      <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.2.1</version>
      <executions>
        <execution>
          <phase>test</phase>
          <goals>
            <goal>java</goal> <!-- "exec" also possible -->
          </goals>
          <configuration>
            <mainClass>com.izforge.izpack.installer.Installer</mainClass>
            <arguments>
              <argument>-console</argument>
              <!-- <argument>arg1</argument> -->
            </arguments>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.codehaus.izpack</groupId>
          <artifactId>izpack-standalone-compiler</artifactId>
          <version>4.3.4</version>
          <scope>compile</scope>
        </dependency>
      </dependencies>
      

      不过也有Maven plugin for izpack

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-14
        • 2012-08-17
        • 2012-12-20
        • 1970-01-01
        • 2016-07-20
        • 2014-08-09
        • 2016-11-07
        • 1970-01-01
        相关资源
        最近更新 更多