0

I'm trying to generate a JAR file from Groovy code with Maven. It works well, the classes are in the jar file, but it gives me the error 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>

I took this from Groovy docs.

Tree:

├── pom.xml
├── src
│   └── main
│       └── groovy
│           └── Main.groovy

Main.groovy:

package me.strafe.hello

class Main {
  static void main(String[] args) {
    println "Hello, World!"
  }
}

I've tried with gradle too, but i wasn't so familiar with it since i've used maven before.

123strafe
  • 13
  • 3
  • Who gives you an error? – daggett Oct 24 '21 at 14:10
  • @dagget running the JAR file with java -jar – 123strafe Oct 24 '21 at 14:12
  • Have you checked the following: "[If `-jar` is specified, then its argument is the name of the JAR file containing class and resource files for the application. The startup class must be indicated by the `Main-Class` manifest header in its manifest file.](https://docs.oracle.com/en/java/javase/17/docs/specs/man/java.html#description)"? – Gerold Broser Oct 24 '21 at 17:49
  • You have to specify groovy libraries in classpath – daggett Oct 24 '21 at 20:06

1 Answers1

0

If you run the program like this, it will work:

java -cp my.jar me.strafe.hello.Main

Make sure to add any other jars (like the groovy jars) to the classpath, something like this (the file separator is : on Linux, ; on Windows):

java -cp libs/groovy-all.jar:my.jar me.strafe.hello.Main

You can also configure the POM to generate a "fat jar" that includes the dependencies inside a single jar, to make this easier.

If you really want your jar to be runnable, then you should do as above, but also add the Main-Class declaration to the jar's manifest so that you don't need to specify the main class in the command line as shown above.

Once you do both of those things (fat jar and Main-Class declared in the Manifest) this command will also work:

java -jar my.jar
Renato
  • 12,940
  • 3
  • 54
  • 85