Maven Jar Builder: Could not find or load main cla

2019-02-24 05:55发布

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!

2条回答
聊天终结者
2楼-- · 2019-02-24 06:21

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>
查看更多
▲ chillily
3楼-- · 2019-02-24 06:25

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/

查看更多
登录 后发表回答