Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Jar Builder: Could not find or load main class

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!

like image 647
Jonstewart Avatar asked Apr 01 '13 20:04

Jonstewart


1 Answers

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>
like image 189
NilsH Avatar answered Oct 15 '22 18:10

NilsH