Spark Java and the classpath - java

I'm trying to start up with http://www.sparkjava.com/, a small Java web framework. The instructions tell you to add it as a Maven dependency (done), but when I mvn package, I get a class def not found for spark/Route.
I assume this is from Spark not being in my classpath. How can I add it? Would it go in pom.xml?
EDIT: Sorry, here is 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bernsteinbear.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
EDIT: Trace
λ chaos myapp → java -cp target/myapp-1.0-SNAPSHOT.jar com.bernsteinbear.myapp.App
Exception in thread "main" java.lang.NoClassDefFoundError: spark/Route
Caused by: java.lang.ClassNotFoundException: spark.Route
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
aaaand the source (the example from the homepage):
λ chaos myapp → cat src/main/java/com/bernsteinbear/myapp/App.java
/**
* Hello world!
*
*/
package com.bernsteinbear.myapp;
import spark.*;
import static spark.Spark.*;
public class App {
public static void main(String[] args) {
get(new Route("/hello") {
#Override
public Object handle(Request request, Response response) {
return "Hello World!";
}
});
}
}

What works for me to make it run:
mvn package
mvn exec:java -Dexec.mainClass="com.your.class.with.main.method"

I was facing the same problem when trying to deploy the application to Heroku. I added the following to my POM.xml. This plugin ensures that the maven dependencies are copied over to your application.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals><goal>copy-dependencies</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
After this you can go ahead and run
java -cp target/classes:"target/dependency/*" com.bernsteinbear.myapp.App
to run your application

Ok, so the maven package itself did not throw the exception; it was execution. The package produced by Maven must not contain everything required to run the app. (You can unzip the jar if you're curious about exactly what it contains.) So now it's a matter of either including Maven dependencies to your packaged classpath (I wouldn't necessarily recommend bothering with that yet), or instead simply including additional JARs in your runtime classpath (for Unix looks like -cp a.jar:b.jar:...). I suspect the spark-dependencies module has all the missing dependencies. (Unfortunately the readme is not very clear on this.)
Assuming the spark-dependencies module is sufficient, you'd just do:
java -cp target/myapp-1.0-SNAPSHOT.jar:lib/jetty-webapp-7.3.0.v20110203.jar:lib/log4j-1.2.14.jar:lib/slf4j-api-1.6.1.jar:lib/servlet-ap‌​i-3.0.pre4.jar:lib/slf4j-log4j12-1.6.1.jar com.bernsteinbear.myapp.App
Note you have to get the paths right. This is assuming the spark-dependencies zip file is unzipped to a lib folder.
If that still doesn't do it, or for additional information or to give feedback, you might also ping the author directly.

Related

Maven from command line: dependencies don't work

I used the following basic Maven command to generate a project:
mvn archetype:generate -DgroupId=it.maven.project -DartifactId=MavenExample -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
The project was correctly created and I could test the automatically generated App class without any problem with this instruction:
java -cp target/MavenExample-1.0-SNAPSHOT.jar:lib/* it.maven.project.App
Later on, I added some dependencies to the POM, obtaining the following file:
<?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>it.maven.project</groupId>
<artifactId>MavenExample</artifactId>
<version>1.0-SNAPSHOT</version>
<name>MavenExample</name>
<!-- FIXME change it to the project's website -->
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Finally, for testing sake, I modified the App class previously generated by Maven itself:
package it.maven.project;
import org.apache.commons.lang3.RandomStringUtils;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
System.out.println("Stringa generata casualmente: " + RandomStringUtils.random(16, true, true).toUpperCase());
}
}
A series of strange things happen:
even though more recent versions of dependencies are specified in POM, in .m2 folder the downloaded versions seem to be older (for instance I get 2.1 and 2.5 for commons-lang3)
the project is correctly compiled by instruction
mvn clean install -U
when I run again command to execute App, I get the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/RandomStringUtils
at it.maven.project.App.main(App.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.RandomStringUtils
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Questions:
how is it possible that project compiles but then errors are returned at compiling time? Why the import in App is accepted but instruction:
System.out.println("Stringa generata casualmente: " + RandomStringUtils.random(16, true, true).toUpperCase());
generates an exception?
what should I correct/execute to allow my program to work and use correctly dependencies?
when you create a simple java project you define a set of dependencies in the pom but if you don't create a java archive file (ejb-jar or war) all the dependencies are not available at runtime if you run the jar compiled in the target directory.
There are two solutions:
Create a uber jar that include all your depenencies in your jar: use stackoverflow solution
when you run the jar from command line you have to add the dependencies to the classpath:
java -cp "/path/dependencies/dep1.jar;/path/dependencies/dep2.jar" -jar myApp.jar

Maven error "Unable to initialize main class" when I try to run a jar file I just created

I created a .jar file using Maven in the command line. It created the .jar file. When I tried to run it in the command line I got this error:
java -jar target\github-automation-1.0.0.jar
Error: Unable to initialize main class com.aking.app.Application
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
I am not sure how to fix this issue. This is the file structure showing where important files are located (package is combined for the sake of typing):
- githubautomation (root directory)
- pom.xml
- src
- main
- java
- com.aking.app
- Applicatoin.java
- target
- classes
- com.aking.app
- Application.class
- github-automation-1.0.0.jar
My pom.xml (Edit: added suggested code)
<?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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
<start-class>com.aking.app.Application</start-class>
</properties>
<groupId>com.aking.app</groupId>
<artifactId>github-automation</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.aking.app.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
I also read something that there needs to be a main manifest attribute, but I'm not sure. I want to be able to have people run my program through the jar without relying on them to have an IDE.
You have to specify your start class in pom.xml.
In the properties section, include the below line
<start-class>#path to your main class</start-class>
The path should be relative, i.e from the start of your package name.
This is the root cause of your problem:
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
The class loader cannot find the WebDriver class.
Why?
Probably because the JAR file containing the class is not on the runtime classpath.
So how do you get it on the classpath?
There are 3 ways:
Use java -cp ... com.aking.app.Application where the ... has all of the JAR files that you need. The Oracle documentation explains the syntax you need to use to express the classpath on the command line.
Note: on Linux and Mac OS, the classpath separator is : not ;.
Modify your JAR file to include a Class-Path attribute in its MANIFEST.MF file.
Create an "UberJAR" that contains an exploded copy of all dependent JARs.
Variations 2 and 3 can be done using Maven.

Issues with javac compilation errors and Maven dependency resolution

I'm a "noob" when it comes to Maven. I've used ANT in the past, but not enough Maven to make it "stick" with me.
Here is my problem. I'm attempting to write a java program that will parse JSON files for relevant information and then generate CSV files for upload using a proprietary tool that only uploads CSV files. I built the project using Maven. The program will use the Google Gson library, which I listed as a dependency in my POM file (included below). When I execute "mvn compile", Maven returns an error indicating that the package Maven should be resolving via its dependency management features doesn't exist.
Specific error:
"error: package com.google.code.gson does not exist"
Here is my POM file:
<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>
<groupId>com.acumen.app</groupId>
<artifactId>CatalogConverter</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>CatalogConverter</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
I have an import statement in my java application class which should be resolvable based upon Maven downloading my dependencies, if my understanding of Maven is correct. Here is the import statement.
import com.google.code.gson.Gson;
So my question is, why will Maven not compile my code? I don't think I have misconfigured the import nor the POM file. Though, that is always debatable! I can go to my local Maven "repo" and see that the jars were, in fact, downloaded. I'm fairly certain that the error is "user error", so where am I making the mistakes?
I don't believe "provided" is the correct dependency because the jar files are needed for successful compilation and must be downloaded (since no container server or other mechanism will resolve them for me). The application will execute as a jar itself.
in your version of gson it must be
import com.google.gson.Gson;

How do I import normal enum in maven project?

I'm porting a project to Maven. It seems that I'm almost done, though there's still a strange problem. I have an enum:
package cz.autoclient.settings;
public enum Setnames {
SETTING1("s1", false),
SETTING2("s2", 666),
;
public final String name;
public final Object def;
Setnames(String n, Object d) {
name = n;
def = d;
}
}
I use this particular enum to avoid re-creation of String whenever some setting is loaded from the database. And to store default values.
In my old project, this was valid:
import cz.autoclient.settings.Setnames;
In Maven, there's a problem:
Exception in thread "main" java.lang.NoClassDefFoundError: cz/autoclient/settings/Setnames
at cz.autoclient.Gui.createTabs(Gui.java:326)
at cz.autoclient.Gui.initComponents(Gui.java:165)
at cz.autoclient.Gui.<init>(Gui.java:58)
at cz.autoclient.Main.startGUI(Main.java:71)
at cz.autoclient.Main.<init>(Main.java:32)
at cz.autoclient.Main.main(Main.java:98)
Caused by: java.lang.ClassNotFoundException: cz.autoclient.settings.Setnames
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Gui.java:326 is the line where I first use Setnames. I've been googling and what I got that you've got to use $ when naming enums somehow.
But I've no idea where should I put that $ in my case - other people allways had this problem when enum was hidden within a class.
Here's what it looks like in my IDE:
Here's the project 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>
<groupId>cz.autoclient</groupId>
<artifactId>autoclient</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<src.dir>src/</src.dir>
<test.dir>test/</test.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<testSourceDirectory>${test.dir}</testSourceDirectory>
<sourceDirectory>${src.dir}</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<name>Auto Client</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
</project>
This is the command line NetBeans is using to compile the project:
cd C:\MYSELF\programing\java\AutoCall\AutoClient; "JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_31" cmd /c "\"\"C:\\Users\\Jakub\\AppData\\Roaming\\NetBeans\\7.4\\maven\\bin\\mvn.bat\" -Dexec.args=\"-classpath %classpath cz.autoclient.Main\" -Dexec.executable=\"C:\\Program Files\\Java\\jdk1.8.0_31\\bin\\java.exe\" -DnetbeansProjectMappings= -Dmaven.ext.class.path=C:\\Users\\Jakub\\AppData\\Roaming\\NetBeans\\7.4\\maven-nblib\\netbeans-eventspy.jar org.codehaus.mojo:exec-maven-plugin:1.2.1:exec\""
You created a maven project and moved your class from cz.autoclient.settings.Setnames to cz.autoclient.PVP_net.Setnames. Now you're getting an NoClassDefFoundError which means that java simply can't find your class. Why? Because it's moved to another place, but this has nothing to do with maven it has just happened after creating a maven project.
Please check your whole project for imports like:
import cz.autoclient.settings.Setnames;
an replace them by something like:
import cz.autoclient.PVP_net.Setnames;
I hope that helps you.
Turned out it was some kind of glitch in the project. I have no idea how it happened.
Refactoring the packages with problematic classes fixed the problem.

NoClassDefFoundError on Maven dependency

My first use of Maven and I'm stuck with dependencies.
I created a Maven project with Eclipse and added dependencies, and it was working without problems.
But when I try to run it via command line:
$ mvn package # successfully completes
$ java -cp target/bil138_4-0.0.1-SNAPSHOT.jar tr.edu.hacettepe.cs.b21127113.bil138_4.App # NoClassDefFoundError for dependencies
It downloads dependencies, successfully builds, but when I try to run it, I get NoClassDefFoundError:
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonParseException
at tr.edu.hacettepe.cs.b21127113.bil138_4.db.DatabaseManager.<init>(DatabaseManager.java:16)
at tr.edu.hacettepe.cs.b21127113.bil138_4.db.DatabaseManager.<init>(DatabaseManager.java:22)
at tr.edu.hacettepe.cs.b21127113.bil138_4.App.main(App.java:10)
Caused by: java.lang.ClassNotFoundException: org.codehaus.jackson.JsonParseException
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 3 more
My pom.xml is like this:
<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>tr.edu.hacettepe.cs.b21127113</groupId>
<artifactId>bil138_4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>bil138_4</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Can anyone help me?
By default, Maven doesn't bundle dependencies in the JAR file it builds, and you're not providing them on the classpath when you're trying to execute your JAR file at the command-line. This is why the Java VM can't find the library class files when trying to execute your code.
You could manually specify the libraries on the classpath with the -cp parameter, but that quickly becomes tiresome.
A better solution is to "shade" the library code into your output JAR file. There is a Maven plugin called the maven-shade-plugin to do this. You need to register it in your POM, and it will automatically build an "uber-JAR" containing your classes and the classes for your library code too when you run mvn package.
To simply bundle all required libraries, add the following to your POM:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
Once this is done, you can rerun the commands you used above:
$ mvn package
$ java -cp target/bil138_4-0.0.1-SNAPSHOT.jar tr.edu.hacettepe.cs.b21127113.bil138_4.App
If you want to do further configuration of the shade plugin in terms of what JARs should be included, specifying a Main-Class for an executable JAR file, and so on, see the "Examples" section on the maven-shade-plugin site.
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.
You have to make classpath in pom file for your dependency. Therefore you have to copy all the dependencies into one place.
Check my blog.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>$fullqualified path to your main Class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
This is due to Morphia jar not being part of your output war/jar. Eclipse or local build includes them as part of classpath, but remote builds or auto/scheduled build don't consider them part of classpath.
You can include dependent jars using plugin.
Add below snippet into your pom's plugins section
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
For some reason, the lib is present while compiling, but missing while running.
My situation is, two versions of one lib conflict.
For example, A depends on B and C, while B depends on D:1.0, C depends on D:1.1, maven may
just import D:1.0. If A uses one class which is in D:1.1 but not in D:1.0, a NoClassDefFoundError will be throwed.
If you are in this situation too, you need to resolve the dependency conflict.
I was able to work around it by running mvn install:install-file with -Dpackaging=class. Then adding entry to POM as described here:
Choosing to Project -> Clean should resolve this

Categories