Noclassdeffounderror on Maven Dependency

NoClassDefFoundError on Maven dependency

when I try to run it, I get NoClassDefFoundError

Run it how? You're probably trying to run it with eclipse without having correctly imported your maven classpath. See the m2eclipse plugin for integrating maven with eclipse for that.

To verify that your maven config is correct, you could run your app with the exec plugin using:

mvn exec:java -D exec.mainClass=<your main class>

Update: First, regarding your error when running exec:java, your main class is tr.edu.hacettepe.cs.b21127113.bil138_4.App. When talking about class names, they're (almost) always dot-separated. The simple class name is just the last part: App in your case. The fully-qualified name is the full package plus the simple class name, and that's what you give to maven or java when you want to run something. What you were trying to use was a file system path to a source file. That's an entirely different beast. A class name generally translates directly to a class file that's found in the class path, as compared to a source file in the file system. In your specific case, the class file in question would probably be at target/classes/tr/edu/hacettepe/cs/b21127113/bil138_4/App.class because maven compiles to target/classes, and java traditionally creates a directory for each level of packaging.

Your original problem is simply that you haven't put the Jackson jars on your class path. When you run a java program from the command line, you have to set the class path to let it know where it can load classes from. You've added your own jar, but not the other required ones. Your comment makes me think you don't understand how to manually build a class path. In short, the class path can have two things: directories containing class files and jars containing class files. Directories containing jars won't work. For more details on building a class path, see "Setting the class path" and the java and javac tool documentation.

Your class path would need to be at least, and without the line feeds:

target/bil138_4-0.0.1-SNAPSHOT.jar:
/home/utdemir/.m2/repository/org/codehaus/jackson/jackson-core-asl/1.9.6/jackson-core-asl-1.9.6.jar:
/home/utdemir/.m2/repository/org/codehaus/jackson/jackson-mapper-asl/1.9.6/jackson-mapper-asl-1.9.6.jar

Note that the separator on Windows is a semicolon (;).

I apologize for not noticing it sooner. The problem was sitting there in your original post, but I missed it.

java NoClassDefFoundError with maven dependency (log4j)

What are you doing?

You are creating a JAR file containing your code (and a Manifest file that points to your main class).

What is the problem?

You are exporting your code to a JAR file but the dependencies are not exported. Obviously, your program needs the dependencies to run(logging libriaries are not essention but other libs are).

How to solve it?

You said, you tried the shadow and the assembly plugin. You can add the dependencies with both. I think you just configured it incorrectly.

With the maven-assembly-plugin, you need to create a jar-with-dependencies and you shouldn't forget about your main class.

For example, you could use the following configuration(replace fully-qualified-main-class with the package name of your main class followed by the name of the main class(seperated by dots):

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</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>

After that, you can create the JAR with the command mvn package.

Make sure to execute the jar-with-dependencies and not the JAR created by the maven-jar-plugin.

other possible problems

It is possible that your program declares log4j with the scope provided. In that case, log4j is not included in your jar-with-dependencies.

See https://stackoverflow.com/a/574650/10871900

NoClassDefFoundError in maven project with local dependency

The maven-install-plugin is used to install an artifact in your local maven repository. This is not what you want to do here. Your goal is to get a dependency in the classpath.

To be able to start your code with this command

java -cp target/my-app-1.0.jar com.mycompany.app.App

you have to ensure two things

  1. The application jar's manifest must contain the path of the dependency jar
  2. The dependency jar has to be at the specified location

For the first part, you can use the maven-jar-plugin:

<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>

Be aware that this will add all dependencies to the classpath, which might or might not be what you want.

For the second part, you can use the maven-dependency-plugin. This will copy all dependencies (or with appropriate configuration only selected dependencies) to target/lib

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</plugin>

With this configuration, the target directory should look somewhat like this, showing only relevant files/directories:

target
|- lib
| |- core-10.jar
|- my-app-1.0.jar

keep getting java.lang.NoClassDefFoundError with maven

ok, although I am not sure how this magic happens, I will try to explain myself.

The only think that I needed to is make a fat jar.

There are apparently a lot of ways to do that : look up here

So, here I just added another plugin:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>

ClassNotFoundException with Maven dependency system scoped

If you read the Maven documentation about this scope, it seems the expected behavior if your application server doesn't provide this library at runtime:

This scope is similar to provided except that you have to provide the
JAR which contains it explicitly.

The provided scope states :

This is much like compile, but indicates you expect the JDK or a
container to provide the dependency at runtime
.

Not advised solution : add this library in the lib folder of your application server.

Cleaner solution : add this maven dependency in your maven repositories manually or with mvn install:install-file.

And remove the system scope of this dependency. It will use the default scope.

Java: NoClassDefFoundError in maven project?

You can either use maven-assembly-plugin to make a fat jar that can be run by itself or you can run your program using maven so that the compile time jars will be added to your classpath.

To run using maven:

mvn exec:java -Dexec.mainClass="engineTester.MainGameLoop"

To use the assembly plugin:

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>engineTester.MainGameLoop</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</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>
</plugins>
</build>

This will create a jar with -jar-with-depencencies added to the name in the target directory. That can then just be run with java -jar jarfile.



Related Topics



Leave a reply



Submit