问题
I've followed the simplest maven example and made the following pom.xml
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.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</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.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
Then I wrote the following code:
package com.mycompany.app;
import org.json.simple.parser.JSONParser;
import org.json.simple.JSONObject;
public class App
{
public static void main(String[] args)
throws Exception
{
JSONParser parser = new JSONParser();
JSONObject cmd = (JSONObject) parser.parse("{ \"hello\": \"world\" }");
System.out.println(cmd.toString());
}
}
After that, compiled and packaged the project:
$ mvn package
Finally I tried to run the jar file:
$ java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/JSONParser
at com.mycompany.app.App.main(App.java:11)
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.JSONParser
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
What am I doing wrong?
回答1:
The problem is, you are setting the classpath to a single .jar file, thus java command can not find the dependency. (json-simple-1.1.1.jar in your case..)
You can tell maven to include the dependencies in target folder that you compiling your class to.
This is done by the following change in the pom.xml:
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Now try mvn package (or mvn clean install, I am not sure..) and you will see in /target/lib the dependencies are copied.
Run your application like this:
java -cp target/my-app-1.0-SNAPSHOT.jar:target/lib/json-simple-1.1.1.jar com.mycompany.app.App
{"hello":"world"}
回答2:
As per exception ClassNotFoundException: org.json.simple.parser.JSONParser
the required jar
file is not present in classpath
.
In eclipse use below steps:
Right click on project --> properties --> deployment assembly --> add
--> (here select appropriate file system to add jar like)java build path entries
hope it help.
回答3:
By default, jar doesn't include dependencies within it. You either need to add all dependencies in classpath at run time or you can build jar with dependencies. For building jar with dependencies see link: http://maven.apache.org/plugins/maven-assembly-plugin/
来源:https://stackoverflow.com/questions/35884429/why-dependencies-do-not-accompany-the-made-package-by-maven