问题
I was following along with the maven tutorial at http://www.youtube.com/watch?v=IRKu8_l5YiQ.
I created a maven project with the default archetype. Then I added the following dependencies to my pom.xml:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
Then I tried to put a Logger into my App.java file. This file comes built in with the default maven archetype that I used. The code for that is below:
package GroupID;
import org.slf4j.*;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
//System.out.println( "Hello World!" );
Logger logger = LoggerFactory.getLogger(App.class);
logger.info("Hello, World!");
}
}
Then I ran 'mvn package' and it doesn't produce any errors. However, when I try to run the outputted jar using:
java -cp /Users/stephenmorse/Desktop/myapp/ArtifactID/target/ArtifactID-1.jar GroupID.App
I get the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory
at GroupID.App.main(App.java:12)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
...1 more
It seems that even though I accepted the default scope of 'compile' by not specifying a scope in the dependency block, I still don't have access to the downloaded jars at runtime. How is finding the jars at runtime after the dependencies have been set up supposed to work? Why aren't they working in my case?
Thanks!
回答1:
When I first started using Maven, I had to provide some additional build "plugin"/"configuration" in order for the Jar to be built with the correct manifest file, including the class-path
entry and the dependent jars included with the jar...
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- This is the prefix for the class path -->
<!-- The dependent jars will copied to the lib directory -->
<!-- under the main jar -->
<classpathPrefix>lib/</classpathPrefix>
<!--mainClass>com.acme.MainClass</mainClass-->
</manifest>
</archive>
</configuration>
</plugin>
<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-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<!--source>1.7</source-->
<!--target>1.7</target-->
</configuration>
</plugin>
</plugins>
</build>
You might want to do some additional reading on some the flags/plugins. This is nearly a year old, so some may have been deprecated...
回答2:
Consider using maven shade plugin to create an Uber Jar.
When using slf4j-api you need to add an API implementation dependency at runtime, e.g. slf4j-simple or logback:
...
<properties>
<slf4j.version>1.7.7</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependencies>
来源:https://stackoverflow.com/questions/24028921/in-maven-my-dependencies-are-set-up-but-i-get-a-classnotfoundexception-when-i-r