使用maven构建的jar中缺少Java类

时间:2014-05-06 21:35:14

标签: java maven jar

当我尝试运行我的.jar时,无法找到课程?
它是一个基本的应用程序,包含2个自定义类和commons-lang
.jar包含commons-lang的类,但不包含我的java类。

rob@work:~/git/ProjectName/target$ java -jar uber-ProjectName-1.0-SNAPSHOT.jar 
Exception in thread "main" java.lang.NoClassDefFoundError: com/test/server/Main
Caused by: java.lang.ClassNotFoundException: com.test.server.Main
    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:323)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: com.test.server.Main. Program will exit.

这是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.test.server</groupId>
    <artifactId>ProjectName</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>thisisatest</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.3</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.test.server.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <finalName>uber-${artifactId}-${version}</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

构建输出..

rob@work:~/git/ProjectName$ mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building thisisatest
[INFO]    task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] Ignoring available plugin update: 2.3 as it requires Maven version 3.0
[INFO] Ignoring available plugin update: 2.2 as it requires Maven version 3.0
[INFO] Ignoring available plugin update: 2.1 as it requires Maven version 3.0
[INFO] Ignoring available plugin update: 2.0 as it requires Maven version 3.0
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/rob/git/ProjectName/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] No sources to compile
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/rob/git/ProjectName/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] No sources to compile
[INFO] [surefire:test {execution: default-test}]
[INFO] No tests to run.
[INFO] Surefire report directory: /home/rob/git/ProjectName/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] [jar:jar {execution: default-jar}]
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: /home/rob/git/ProjectName/target/ProjectName-1.0-SNAPSHOT.jar
[INFO] [shade:shade {execution: default}]
[INFO] Including commons-lang:commons-lang:jar:2.3 in the shaded jar.
[INFO] Replacing /home/rob/git/ProjectName/target/uber-ProjectName-1.0-SNAPSHOT.jar with /home/rob/git/ProjectName/target/ProjectName-1.0-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue May 06 22:23:33 IST 2014
[INFO] Final Memory: 17M/179M
[INFO] ------------------------------------------------------------------------

3 个答案:

答案 0 :(得分:5)

[INFO] [compiler:compile {execution: default-compile}]
[INFO] No sources to compile

Maven无法找到任何要编译的代码。这看起来非常可疑。也许您的maven项目没有像maven项目那样配置,也无法找到源。请参阅maven的project structure diagram

答案 1 :(得分:1)

[INFO] [compiler:compile {execution: default-compile}]
[INFO] No sources to compile

Maven没有找到您的来源,您可能没有正确的目录结构。阅读http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

答案 2 :(得分:0)

当我在“main”文件夹之前有一个额外的空间时,我遇到了这个问题。您可以检查您的文件夹结构,看看它是否正确。