Question

I have been trying for several days now to create an executable jar file for my muli-module maven project. However, when I try to run this jar file I get "Could not find or load main class src.main.java.com.domain.Mainclass" (I have changed the name domain and MainClass for my company's privacy sake)

I have searched for days and none of the obvious tips seem to work.

My project has a main maven project that downloads all of the dependencies (packaging:POM) and several module projects (Packaging:Jar).

Everything seems to work fine, and all of the files are compiled into class files, but somehow the main class is not being added to the pom.

My Pom File Plugin:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-4</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.domain.project.MainClass</mainClass>
        </manifest>
      </archive>
      </configuration>
      <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
      </executions>
      </plugin>

The Commands I Use: mvn clean package, then I cd into the target folder and do: java -jar jarfilename.jar

Any tips or help would be most welcomed!

Edit:

My current configuration creates a 2 jar files for every module:

  • projectname-jar-with-dependencies.jar
  • projectname.jar

When I navigate to the target folder of the module with my main class, I can successfully run the jar file. However, when I try to run the jar file of my parent maven project (the one with packageing:pom) I still get the same error. Does anyone know why the main jar file cannot find the main class?

Thanks!

Was it helpful?

Solution

You should not have src.main.java as part of the package name of your main class. It's just part of the default maven project folder structure convention. Your configuration should probably read:

<archive>
    <manifest>
      <mainClass>com.domain.project.MainClass</mainClass>
    </manifest>
  </archive>

OTHER TIPS

I'm going to necro this post because it is important to have 3 different things set properly:

  1. MainClass.java needs to be in <project_root>/src/main/java/com/domain/project/
  2. Maven assembly plugin needs <mainClass>com.domain.project.MainClass</mainClass>
  3. Your package should be set to com.domain.project

When those three match, Maven should package an executable JAR file.

it seems like you have one of the plugins missing. add the bellow to pom.xml file

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>yourclassnameKt</mainClass>
                </manifest>
            </archive>
    </configuration>
</plugin>

The plugin will add MANIFEST.mf file, which will tell the Java runtime which class to execute.

There is another issue that has the same problem -> Kotlin + Maven assembling: no main manifest attribute

Reference: https://michaelrice.com/2016/08/hello-world-with-kotlin-and-maven/

I have a bit of a different but similar scenario so I thought I would share. I have a primary maven application packaged into a war through the build (pom.xml packaging configuration). I wanted to add a jar file that is created from one package within our source code, and added into the assembled output along with the webapp war. This allows us to deliver the web application along with a cli tool separately. I didn't want to reconfigure the pom.xml to be multi-module, but just to add a jar as a separate executable within our existing structure. I was banging my head against this for a while.

First, I ended up deleting my entire .m2 directory with maven repositories locally. It appears there may have been an issue for me here, because my ultimate code that worked seems to be the same as what wasn't working originally. I suspect the reasoning for this was I was trying different versions of libraries and was creating conflicts, but who knows. First suggestion I have if you are having issues is delete your .m2 folder and try from scratch.

Also reference @Adam Howell's answer because that is the fundamentals. I created a simplified example project to figure out why nothing was happening and in there I realized i forgot to prefix the folder structure as src/main/java... doh! Of course in my existing project, this was not the case.

And here is my plugin code i inserted in my pom.xml that worked:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>com.myCompany.app.cli.CLITool</mainClass>
            </manifest>
        </archive>
        <outputDirectory>${project.build.directory}</outputDirectory>
        <finalName>our-cli</finalName>
    </configuration>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

I am pretty sure you can change the phase you want this done in, I figured earlier on in compile made since to ensure it was available for packaging later. I'm not very experienced with Maven though, so note that this may not be semantically in-line with Maven conventions.

And it worked! Not sure why this took me hours to work out although it did. This I just updated my bin.xml to include the jar file in my assembled deliverable, and Voilah! I have a jar executable separate from my webapp that I can use as a command-line interface tool.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top