在Maven中,我的依赖项已经设置,但是当我运行生成的jar时,我得到了一个ClassNotFoundException [英] In Maven, my dependencies are set up but I get a ClassNotFoundException when I run generated jar

查看:106
本文介绍了在Maven中,我的依赖项已经设置,但是当我运行生成的jar时,我得到了一个ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 http://www.youtube.com/watch上的maven教程?v = IRKu8_l5YiQ

我使用默认原型创建了一个maven项目。然后我将以下依赖项添加到我的pom.xml中:

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>

然后我尝试将Logger放入我的App.java文件中。此文件内置了我使用的默认maven原型。代码如下:

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!");
    }

}

然后我跑'mvn package'它不会产生任何错误。但是,当我尝试使用以下命令运行输出的jar时:

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

我收到以下错误消息:

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

似乎即使我公认通过不在依赖关系块中指定范围来编译的默认范围,我仍然无法在运行时访问下载的jar。在设置了依赖项后,如何在运行时找到jar?他们为什么不在我的案子中工作?

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?

谢谢!

推荐答案

<当我第一次开始使用Maven时,我必须提供一些额外的构建插件/配置,以便使用正确的清单文件构建Jar,包括 class-path 条目以及jar中包含的依赖jar ...

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...

这篇关于在Maven中,我的依赖项已经设置,但是当我运行生成的jar时,我得到了一个ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆