簡體   English   中英

如何在Maven目標目錄的JAR文件中運行Java類?

[英]How to run java class in JAR file in maven target directory?

我想在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”

馬丁

你在罐子里看了嗎? 可能是Maven沒有在jar中包含所需的類。

我認為您應該使用-classpathjava命令定義類路徑。 您需要構造一個類路徑,其中將包含您的主類com.izforge.izpack.installer.Installer及其所有依賴項。 它可以在一個jar中,也可以在一個類文件夾中,也可以在多個jar中。 有關如何為Java調用定義類路徑的信息,請參見Wikipedia

您可以使用類似的方法為該執行定義依賴項:

<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>

但是,還有一個用於izpackMaven插件

暫無
暫無

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

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