我正在嘗試使用 Maven 從 Groovy 代碼生成 JAR 檔案。它運行良好,類在 jar 檔案中,但它給了我錯誤Error: Could not find or load main class me.strafe.hello.Main.
pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<configuration>
<tasks>
<mkdir dir="${basedir}/src/main/groovy"/>
<taskdef name="groovyc"
classname="org.codehaus.groovy.ant.Groovyc">
<classpath refid="maven.compile.classpath"/>
</taskdef>
<mkdir dir="${project.build.outputDirectory}"/>
<groovyc destdir="${project.build.outputDirectory}"
srcdir="${basedir}/src/main/groovy/"
listfiles="true">
<classpath refid="maven.compile.classpath"/>
</groovyc>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>me.strafe.hello.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
我從 Groovy 檔案中獲取了這個。
樹:
├── pom.xml
├── src
│ └── main
│ └── groovy
│ └── Main.groovy
Main.groovy:
package me.strafe.hello
class Main {
static void main(String[] args) {
println "Hello, World!"
}
}
我也嘗試過使用 gradle,但我對它不太熟悉,因為我以前使用過 maven。
uj5u.com熱心網友回復:
如果您像這樣運行程式,它將起作用:
java -cp my.jar me.strafe.hello.Main
確保將任何其他 jar(如 groovy jar)添加到類路徑,如下所示(檔案分隔符:在 Linux 上,;在 Windows 上):
java -cp libs/groovy-all.jar:my.jar me.strafe.hello.Main
您還可以配置 POM 以生成一個“胖 jar”,其中包含單個 jar 中的依賴項,以使其更容易。
如果你真的希望你的 jar 是可運行的,那么你應該像上面那樣做,但也要將Main-Class宣告添加到 jar 的清單中,這樣你就不需要在命令列中指定主類,如上所示。
一旦你完成了這兩件事(在清單中宣告的?? fat jar 和 Main-Class),這個命令也將起作用:
java -jar my.jar
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/341212.html
