Maven Error: Could not find or load main class - java

I'm using a Java Maven program and I don't know what to enter as the <mainClass>. I've tried all kinds of things based off of numerous stackoverflow questions, but they are not solving the error.
Each time it says:
Maven Error: Could not find or load main class ...
I have this written inside my pom.xml (minus the ???)
<build>
...
<plugins>
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/jar-with-dependencies.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass> ??? </mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
How do I fix these errors?

I got this error using Maven, and I discovered the solution.
Error: Could not find or load main class com.mycompany.testapifactory.Main
I'm using java JDK version 1.7 on Linux, my pom.xml file was the default generated by Netbeans and I was using these commands to compile, which do work fine with a normal hello-world java application:
mvn clean compile
java -jar target/TestAPIFactory-1.0-SNAPSHOT.jar com.mycompany.testapifactory.Main
What happened:
It turns out my problem was that my Main method was extending something Exotic like this:
public class Main extends SomeExoticLibraryClass{
public static void main(String[] args){
//...
}
}
It was this extending of the main class that caused the above error.
TLDR solution:
Make sure your main class isn't extending any 3rd party classes. Refactor those out and away into their own classes. That error message is awful, and requires process of elimination to find out what to do.

Unless you need the 'maven-assembly-plugin' for reasons other than setting the mainClass, you could use the 'maven-jar-plugin' plugin.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<mainClass>your.package.yourprogram.YourMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
You can see the plugin in practise in the ATLauncher.
The 'mainClass' element should be set to the class that you have the entry point to your program in eg:
package your.package.yourprogram;
public class YourMainClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

For me the problem was nothing to do with Maven but to do with how I was running the .jar. I wrote some code and packaged it as a .jar with Maven. I ran it with
java target/gs-maven-0.1.0.jar
and got the error in the OP. Actually you need the -jar option:
java -jar target/gs-maven-0.1.0.jar

Please follow the below snippet.. it works..
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestProject</name>
<description>Sample Project</description>
<dependencies>
<!-- mention your dependencies here -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<mainClass>com.xyz.ABC.</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Please note, you have to provide the full classified class name (class name including package name without .java or .class) of main class inside <mainClass></mainClass> tag.

I got it too, for me the problem got resolved after deleting the m2 folder (C:\Users\username.m2) and updating the maven project.

I got it too, the key was to change the output folder from bin to target\classes. It seems that in Eclipse, when converting a project to Maven project, this step is not done automatically, but Maven project will not look for main class based on bin, but will on target\classes.

specify the main class location in pom under plugins
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<mainClass>com.example.hadoop.wordCount.WordCountApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

add this to your pom.xml file:
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>sample.HelloWorldApplication</mainClass>
</transformer>
</transformers>
</configuration>
and add the class name of your project (full path) along with the package name like "com.packageName.className" which consists of the main method having "run" method in it.
And instead of your "???" write ${mainClass} which will automatically get the className which you have mentioned above.
Then try command mvn clean install and mvn -jar "jar_file_name.jar" server "yaml_file_name.yml"
I hope it will work normally and server will start at the specified port.

The first thing i would suggest is to use the correct configuration for predefined descriptors.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
[...]
</project>
To configure the main class you need to know the package and name of the class you would like to use which should be given into <mainClass>...</mainClass> parameter.
Furthermore i recommend to stop using Maven 2 and move to Maven 3 instead.

I got this error(classNotFoundException for main class), I actually changed pom version , so did maven install again and then error vanished.

TLDR : check if packaging element inside the pom.xml file is set to jar.
Like this - <packaging>jar</packaging>. If it set to pom your target folder will not be created even after you Clean and Build your project and Maven executable won't be able to find .class files (because they don't exist), after which you get Error: Could not find or load main class your.package.name.MainClass
After creating a Maven POM project in Netbeans 8.2, the content of the default pom.xml file are as follows -
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Here packaging element is set to pom. Hence the target directory is not created as we are not enabling maven to package our application as a jar file. Change it to jar then Clean and Build your project, you should see target directory created at root location. Now you should be able to run that java file with main method.
When no packaging is declared, Maven assumes the packaging as jar. Other core packaging values are pom, war, maven-plugin, ejb, ear, rar. These define the goals that execute on each corresponsding build life-cycle phase of that package. See more here

this worked for me....
I added the following line to properties in pom.xml
<properties>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>

For me, I added
<packaging>jar</packaging>
and removed the default spring-boot-maven-plugin
<!-- remove this plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> -->

Add the -Xdiag option at the execution. This is extra "diagnostic". This will not solve the issue but add more detailed error messages and root causes that help identifying the issue.

In a rare occasion, an emoji in the access path caused this type of error. Double check your directory names, maybe a non standard character makes all the fun!

I am late to the party in my case it was the image I was using that was causing the trouble
Working config
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-maven-plugin.version}</version>
<configuration>
<from>
<image>openjdk:11-jre-slim</image>
</from>
<to>
<image>${docker.image.prefix}/${project.artifactId}</image>
</to>
<container>
<mainClass>com.example.configserverApplication</mainClass>
<ports>
<port>8888</port>
</ports>
</container>
</configuration>
</plugin>
Failing one
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-maven-plugin.version}</version>
<configuration>
<from>
<image>openjdk:alpine</image>
</from>
<to>
<image>${docker.image.prefix}/${project.artifactId}</image>
</to>
<container>
<mainClass>com.example.configserverApplication</mainClass>
<ports>
<port>8888</port>
</ports>
</container>
</configuration>
</plugin>
My understanding is that
"The OpenJDK port for Alpine is not in a supported release by OpenJDK, since it is not in the mainline codebase. It is only available as early access builds of OpenJDK Project Portola. See also this comment. So this image follows what is available from the OpenJDK project's maintainers."
https://hub.docker.com/_/openjdk
it is weird that it fails with a one-line error and changing the image fixed it for me.

Related

Maven assembly jar execution Could not find or load main class Caused by: java.lang.ClassNotFoundException

I have been trying, for several days now, to create an executable jar file for my muli-module maven project, sadly I have had no luck on doing it.
I know that there are a lot of similar questions already, but, even by following the answers, i cannot manage to run the jar I make.
I am using the maven-assembly plugin, so that my jar contains all the required dependencies, here is the pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>tam-authentication</artifactId>
<name>tam-authentication</name>
<description>authentication tam project</description>
<parent>
<groupId>com.netcomgroup.eu</groupId>
<artifactId>TAM</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<java.version>11</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>"fully qualified class name"</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
...
dependencies from other modules
...
</dependencies>
</project>
futhermore i have another probably related problem regarding jar creation with a common library included:
using Eclipse as IDE, whenever I run as > maven install on the multimodule project, I often get jars failing over correct imports, that I need to delete and import again to complete the java building process correctly. Sometimes i simply must run maven install several times in a row to make the jar building process succeed.
I don't know if the second problem is related but i guess there is some mistake I cannot see in the multi module project jar building.
First of all, as per your pom.xml you did not provide the fully qualified Main class name in your dependency. Second if you provided the Main class name, then after "mvn clean install", it would create 2 jars - one with artifact name and one with "jar-with-dependencies" and you have to user "jar-with-dependencies".
Also i don't think you need to use pluginManagement tag as an extra step. I have just modified a bit your pom below, please try this and check.
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
<artifactId>tam-authentication</artifactId>
<name>tam-authentication</name>
<description>authentication tam project</description>
<parent>
<groupId>com.netcomgroup.eu</groupId>
<artifactId>TAM</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<java.version>11</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>{Your fully qualified Main class eg - abc.cde.Main}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
...
dependencies from other modules
...
</dependencies>

Maven clean install not including sqlite dependency for executable jar file

After using
mvn clean install
and then
java -jar executable.jar
I get this error:
java.lang.ClassNotFoundException: org.sqlite.JDBC
This is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>****</groupId>
<artifactId>****</artifactId>
<version>0.7-SNAPSHOT</version>
<packaging>jar</packaging>
<name>****</name>
<description>****</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.23.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-engine</artifactId>
<version>5.0.0-ALPHA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.tinyjee.jgraphx/jgraphx -->
<dependency>
<groupId>org.tinyjee.jgraphx</groupId>
<artifactId>jgraphx</artifactId>
<version>3.4.1.3</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>******</sourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>*****</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>******</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Running the program inside intelliJ works without problems. I included it there from the project structure.
I replaced names and directories with ****. This is a school project and I don't want my prof accusing me of providing solutions to other groups in case they find this stackoverflow entry.
Probably you are getting this only when you are running your jar because the dependencies are not available/packaged inside of it.
Try generating a "fat jar" (also known as uber-jar), it will package all your dependencies inside the jar:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>YOUR_JAR_FINAL_NAME</finalName>
</configuration>
</plugin>
</plugins>
</build>
Documentation related to the maven-shade-plugin can be found in here
Since you are using a runnable jar file, you can follow this section of the documentation related to Executable Jars
Some background.
Maven install never installs dependencies;
it only installs the project that is build via the POM.
The installation of dependencies are a task that you must also perform
if you don't use either a "fat jar" (which I can't recommend) or use
the new spring boot executable jar implementation.
The classic "fat jar" is an amazingly terrible
(but often the only option)
solution for a problem like this.
Consider using Spring-Boot;
they have developed a new,
sane,
version of an executable jar file that includes the dependencies within the executable jar and adds them to the classpath at startup.
This functionality is similar to the functionality of a "war" file when it is added to
a JEE container.
Caveat: I don't work for Pivotal,
I just like much of their work (the Spring Framework).
Edit:
Here is a link to the
Executable Jar Section in the Spring Boot Reference.
It contains some details.

Maven custom directory structure - no sources to compile despite specifying sourceDirectory

I am using a custom directory structure and have specified the directory in sourcedirectory tag. But still I get the message No sources to compile. Although the build is successful.
My directory structure:
So instead of src/main/java, I am using java. (And I have a reason to do that, so right now it's not possible to switch to src/main/java)
Here's my pom.xml:
<artifactId>application</artifactId>
<name>application</name>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.test.skip>true</maven.test.skip>
</properties>
<build>
<sourceDirectory>java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>**/old/**/*.java</exclude>
</excludes>
<includes>
<include>java/com/**/*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>**/old/**/*.java</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.start.Start</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
When I run command mvn clean package, I get following output:
The source is not compiled and resultant jar is empty. All the sources I have referred for using custom directory structure with maven say that using sourceDirectory should solve the problem. But in my case it doesn't solve
EDIT
I am using custom directory structure as using standar directory structure did not work for me. Hers' my question related to that:
Getting error in linking a source folder in eclipse maven
EDIT2:
I am linking source in java directory. That is, on the file system, application->java does not exist, but in eclipse through link source option, I have added the Java source folder from a different directory. Therefore it appears in eclipse. Also I have run maven commands with mvn command line as well as through eclipse.
If I understand your issue correctly, You have an application folder and the actual source (java) folder is from somewhere else in the file system.
And you linked the external folder as java source through eclipse for compilation.
By linking in Eclipse, maven will not automatically know where the source files are. Maven follows standard directory structure for looking up java and test files.
You can use this Build Helper plugin to customize the way maven looks up sources.
An example talked here
This is your problem: <include>java/com/**/*.java</include>
You should not include the sourcedirectory, just the paths relative to it.
Follow these steps of this working example and compare it step by step with your project to figure out what's wrong:
create a folder.
create inside the folder a pom.xml with the following content:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yourdomain.yourproject</groupId>
<artifactId>yourapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>yourapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.javadoc.skip>true</maven.javadoc.skip>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.yourdomain.yourproject.content.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<dependencies>
<dependency>
<groupId>org.jvnet.wagon-svn</groupId>
<artifactId>wagon-svn</artifactId>
<version>1.12</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
create this folder structure inside your root folder:
src/main/java/com/yourdomain/yourproject/content/
create a Main.java in content folder with the following content:
package com.yourdomain.yourproject.content;
public class Main
{
public static void main(String[] args)
{
System.out.println("HELLO");
}
}
go back to your root folder and execute mvn clean install
a target folder will be created with the jar in there.
you can execute it with java -jar target/yourapp-1.0-SNAPSHOT.jar

Eclipse / Maven: "Could not find the main class" Errors While Building Executable JAR

Updated: "Could not find the main class" turned out to be a red herring: In the line immediately before public static void main(String[] args) my class attempts to load a resource bundle from a file that was not included in the JAR. Failure to load this resource produced reporting that lead me in the wrong direction:
Caused by: java.util.MissingResourceException: Can't find bundle for base name propFile, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1427)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1250)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:705)
at com.foo.bar.MyApp(MyApp.java:103)
Could not find the main class: com.foo.bar.MyApp. Program will exit.
I'm developing on Win 7 64-bit with Eclipse Juno and JDK 1.6_45, both 32-bit. I'm generating an executable JAR with a mvn clean install invocation.
When I attempt to launch the app I've received "Could not find the main class" or "Failed to load Main-Class manifest attribute"
Similar ground has been covered here and here. I've read through these answers and the basic maven examples, but I'm still failing to end up
with an executable JAR.
I've attempted to execute the app with both java -jar MyApp-jar-with-dependencies and java -cp MyApp-jar-with-dependencies com.foo.bar.MyApp invocations
I find it particularly confusing that the MANIFEST.MF (contined within META-INF within the JAR) specifically lists my target main class, and the MyApp.class file is present in the directory tree at the correct location (com\foo\bar\MyApp.class).
I have refactored my package name and class name a few times over the course of development. Might this action have caused some referencing/classpath hiccup? Where else could the main class lookup failing?
My pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
[...]
</parent>
<groupId>com.foo.bar</groupId>
<artifactId>MyApp</artifactId>
<packaging>jar</packaging>
<version>2.0.0.0-SNAPSHOT</version>
<name>MY APP</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<issues-product>APP</issues-product>
<issues-component>MY_APP</issues-component>
</properties>
<dependencies>
[...]
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven.checkstyle.version}</version>
<configuration>
[...]
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${maven.pmd.version}</version>
<configuration>
[...]
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven.javadoc.version}</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.foo.bar.MyApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
[...]
</pluginManagement>
</build>
<reporting>
[...]
</reporting>
<scm>
[...]
</scm>
</project>
My MANIFEST.MF within the the output JAR:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: myname
Build-Jdk: 1.6.0_45
Main-Class: com.foo.bar.MyApp
I'm not sure why yours isn't working, here is a cut-n-paste from my working maven project. You should only have to replace com.pictureu.mains.MainGui with your main class to test
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.pictureu.mains.MainGui</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>

Playn mvn clean install error "NoClassDefFoundError: playn/core/game

so for a long time now i have been searching the web constantly looking at other peoples problems and fixes hoping they will be reverent to my issue, but i cant seem to find anyone with the same issue involving playn.
i can run "mvn test" the the cmd and mu little project runs perfectly, however when i come to finally exporting the project using "mvn clean install" and attemt to run the myApp-core-1.0-SNAPSHOT.jar i get an Exception:
Exception in thread "main" java.NoClassDefFoundError: playn/core/game
im just wondering if anone has come across this problem in the past and can point me in the right direction to overcoming it, since this app is my A-level coursework and is due to be handed in soon i am very worried i may not reach the deadline for handing in dates, i really hope this doesnt happen D:
this is my myApp-core pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-
v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ALevelWork</groupId>
<artifactId>zombiepanic</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>zombiepanic-core</artifactId>
<packaging>jar</packaging>
<name>ZombiePanic Core</name>
<dependencies>
<dependency>
<groupId>com.googlecode.playn</groupId>
<artifactId>playn-jbox2d</artifactId>
<version>${playn.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.playn</groupId>
<artifactId>playn-core</artifactId>
<version>${playn.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass> ZombiePanic.core.ZombiePanic</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
</plugins>
<resources>
<!-- include the source files in our main jar for use by GWT -->
<resource>
<directory>${project.build.sourceDirectory}</directory>
</resource>
<!-- and continue to include our standard resources -->
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
</build>
</project>
Thanks in advance,
Jacob
i have fixed this problem by following this tutorial (http://jameswilliams.be/blog/entry/255), i was not linking the zombiepanic-core to the java backend (zombiepanic-java) to do this you need to firstly add this plug-in into the java backend pom.xml:
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>jarjar-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jarjar</goal>
</goals>
<configuration>
<rules />
</configuration>
</execution>
</executions>
once you have done this, link it to the core class (zombiepanic-core) by adding this also into the java backend pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.YourPackage.java.JarJarDemoJava</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
remember to link the main class to your java backend version, for example ZombiePanicJava instead of the core version which is just ZombiePanic.

Categories