使用 Java 运行项目时出现 java.lang.NoClassDefFoundError,但在使用 mvn exec:exec 运行时有效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20376057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:29:11  来源:igfitidea点击:

java.lang.NoClassDefFoundError on running project with Java, but works when running with mvn exec:exec

javamavenstructr

提问by Rob Hoff

I'm compiling and running some source code I got from a project called Structr (https://github.com/structr/structr). I'm able to run the program correctly with a maven mvn command but not using the JVM java command.

我正在编译并运行从名为 Structr ( https://github.com/structr/structr)的项目中获得的一些源代码。我可以使用 maven mvn 命令正确运行程序,但不能使用 JVM java 命令。

The compilation step goes well with maven clean install -DskipTests

编译步骤与 maven clean install -DskipTests

On running the front-end (in the structr-ui directory), it goes well if using maven exec:execbut fails on java -cp target/lib/*;target/structr-ui-0.8.2.jar org.structr.Ui. I have some stack trace indicating java.lang.NoClassDefFoundErroron org.structr.core.entity.AbstractNodeand org.structr.core.EntityContext. What I find strange about this is that the maven pom.xml file in the exec entry we have

在运行前端(在 structr-ui 目录中)时,如果使用运行良好maven exec:exec但在java -cp target/lib/*;target/structr-ui-0.8.2.jar org.structr.Ui. 我有一些指示堆栈跟踪java.lang.NoClassDefFoundErrororg.structr.core.entity.AbstractNodeorg.structr.core.EntityContext。我对此感到奇怪的是,我们拥有的 exec 条目中的 maven pom.xml 文件

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-server</argument>
                    <argument>-Dfile.encoding=utf-8</argument>
                    <argument>-XX:+UseNUMA</argument>
                    <argument>-Xms1g</argument>
                    <argument>-Xmx1g</argument>
                    <argument>-classpath</argument>
                    <argument>target/lib/*;target/structr-ui-0.8.2.jar</argument>
                    <argument>org.structr.Ui</argument>
                </arguments>
            </configuration>
        </plugin>

The entire pom.xml reads

整个 pom.xml 读取

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.structr</groupId>
        <artifactId>structr</artifactId>
        <version>0.8.2</version>
    </parent>

    <groupId>org.structr</groupId>
    <artifactId>structr-ui</artifactId>
    <packaging>jar</packaging>
    <version>0.8.2</version>
    <name>structr-ui</name>
    <description>Structr is an open source framework based on the popular Neo4j graph database.</description>
    <developers>
        <developer>
            <name>Axel Morgner</name>
            <email>[email protected]</email>
        </developer>
        <developer>
            <name>Christian Morgner</name>
            <email>[email protected]</email>
        </developer>
    </developers>
    <url>http://structr.org</url>

    <properties>
        <netbeans.hint.license>structr-agpl30</netbeans.hint.license>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <repositories>
        <repository>
            <id>neo4j-releases</id>
            <url>http://m2.neo4j.org/content/repositories/releases</url>
        </repository>
        <repository>
            <id>neo4j-snapshots</id>
            <url>http://m2.neo4j.org/content/repositories/snapshots</url>
        </repository>
        <repository>
            <id>google-diff-patch-match</id>
            <name>google-diff-patch-match</name>
            <url>http://google-diff-match-patch.googlecode.com/svn/trunk/maven/</url>
        </repository>
        <repository>
            <id>jodd</id>
            <url>http://repo1.maven.org/maven2/org/jodd/</url>
        </repository>
        <!--        <repository>
            <id>alfresco-releases</id>
            <url>https://maven.alfresco.com/nexus/content/repositories/releases</url>
        </repository>
        <repository>
            <id>alfresco-snapshots</id>
            <url>https://maven.alfresco.com/nexus/content/repositories/snapshots</url>
        </repository>-->
        <repository>
            <id>snapshots.maven.structr.org</id>
            <url>http://maven.structr.org/artifactory/snapshot</url>
        </repository>
        <repository>
            <id>releases.maven.structr.org</id>
            <url>http://maven.structr.org/artifactory/release</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>structr-server</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <type>jar</type>
            <scope>test</scope>
            <optional>false</optional>
        </dependency>
        <!--        <dependency>
                <artifactId>urlrewritefilter</artifactId>
                <groupId>org.tuckey</groupId>
                <type>jar</type>
                <version>4.0.4</version>
        </dependency>-->
        <dependency>
            <groupId>diff_match_patch</groupId>
            <artifactId>diff_match_patch</artifactId>
            <version>current</version>
        </dependency>
        <dependency>
            <groupId>org.pegdown</groupId>
            <artifactId>pegdown</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>net.java</groupId>
            <artifactId>textile-j</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0-m09</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.9-ea04</version>
        </dependency>
        <!--        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-rest-graphdb</artifactId>
            <version>1.9.5</version>
        </dependency>-->
        <dependency>
            <groupId>com.flagstone</groupId>
            <artifactId>transform</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-lagarto</artifactId>
            <version>3.4.5</version>
        </dependency>
        <!--        <dependency>
            <groupId>org.jodd</groupId>
            <artifactId>jodd-http</artifactId>
            <version>3.4.5</version>
        </dependency>-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.4.1</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.oltu.oauth2</groupId>
            <artifactId>org.apache.oltu.oauth2.client</artifactId>
            <version>0.31</version>
        </dependency>
        <dependency>
            <groupId>org.twitter4j</groupId>
            <artifactId>twitter4j-core</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ftpserver</groupId>
            <artifactId>ftpserver-core</artifactId>
            <version>1.0.6</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/main/resources</directory>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-server</argument>
                        <argument>-Dfile.encoding=utf-8</argument>
                        <argument>-XX:+UseNUMA</argument>
                        <argument>-Xms1g</argument>
                        <argument>-Xmx1g</argument>
                        <argument>-classpath</argument>
                        <argument>target/lib/*;target/structr-ui-0.8.2.jar</argument>
                        <argument>org.structr.Ui</argument>
                    </arguments>
                </configuration>
            </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>
                    <debug>true</debug>
                </configuration>
            </plugin>
            <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>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </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>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/resources/assemblies/dist.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.vafer</groupId>
                <artifactId>jdeb</artifactId>
                <version>1.0.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jdeb</goal>
                        </goals>
                        <configuration>
                            <timestamped>true</timestamped>
                            <controlDir>${basedir}/src/main/deb/control</controlDir>
                            <dataSet>
                                <data>
                                    <src>${project.build.directory}/${project.build.finalName}.jar</src>
                                    <type>file</type>
                                    <dst>structr-ui.jar</dst>
                                    <mapper>
                                        <type>perm</type>
                                        <prefix>/usr/lib/${project.artifactId}/</prefix>
                                    </mapper>
                                </data>

                                <data>
                                    <src>${project.build.directory}/lib</src>
                                    <type>directory</type>
                                    <mapper>
                                        <type>perm</type>
                                        <prefix>/usr/lib/${project.artifactId}/lib</prefix>
                                        <filemode>755</filemode>
                                    </mapper>
                                </data>

                                <data>
                                    <src>${basedir}/seed.zip</src>
                                    <type>file</type>
                                    <mapper>
                                        <type>perm</type>
                                        <prefix>/usr/lib/${project.artifactId}/</prefix>
                                        <filemode>755</filemode>
                                    </mapper>
                                </data>

                                <data>
                                    <src>${basedir}/src/main/deb/bin</src>
                                    <type>directory</type>
                                    <mapper>
                                        <type>perm</type>
                                        <prefix>/usr/lib/${project.artifactId}/bin</prefix>
                                        <filemode>755</filemode>
                                    </mapper>
                                </data>

                                <data>
                                    <src>${basedir}/src/main/deb/init.d</src>
                                    <type>directory</type>
                                    <mapper>
                                        <type>perm</type>
                                        <prefix>/etc/init.d</prefix>
                                        <filemode>755</filemode>
                                    </mapper>
                                </data>
                            </dataSet>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <!-- Added to make m2e happy, thanks to http://stackoverflow.com/questions/8706017/maven-dependency-plugin-goals-copy-dependencies-unpack-is-not-supported-b -->
        <pluginManagement>
            <plugins>
                <!-- Ignore/Execute plugin execution -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <!-- copy-dependency plugin -->
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-dependency-plugin</artifactId>
                                        <versionRange>[1.0.0,)</versionRange>
                                        <goals>
                                            <goal>copy-dependencies</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <profiles>
        <profile>
            <id>release-sign-artifacts</id>
            <activation>
                <property>
                    <name>performRelease</name>
                    <value>true</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.4</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <licenses>
        <license>
            <name>GNU General Public License, Version 3</name>
            <url>http://www.gnu.org/licenses/agpl-3.0-standalone.html</url>
            <comments>
                Copyright (C) 2010-2013 Axel Morgner, structr &lt;[email protected]&gt;

                This file is part of structr &lt;http://structr.org&gt;.

                structr is free software: you can redistribute it and/or modify
                it under the terms of the GNU Affero General Public License as
                published by the Free Software Foundation, either version 3 of the
                License, or (at your option) any later version.

                structr is distributed in the hope that it will be useful,
                but WITHOUT ANY WARRANTY; without even the implied warranty of
                MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                GNU General Public License for more details.

                You should have received a copy of the GNU Affero General Public License
                along with structr.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
            </comments>
        </license>
    </licenses>

    <scm>
        <url>https://github.com/structr/structr</url>
        <connection>scm:git:[email protected]:structr/structr.git</connection>
    </scm>

    <distributionManagement>
        <!--        <repository>
            <id>releases.maven.structr.org</id>
            <name>maven.structr.org-releases</name>
            <url>http://maven.structr.org/artifactory/release</url>
        </repository>
        <snapshotRepository>
            <id>snapshots.maven.structr.org</id>
            <name>maven.structr.org-snapshots</name>
            <url>http://maven.structr.org/artifactory/snapshot</url>
        </snapshotRepository>-->
        <repository>
            <id>sonatype-nexus-staging</id>
            <name>Maven Central Staging</name>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
        </repository>
        <snapshotRepository>
            <id>sonatype-nexus-snapshots</id>
            <name>Maven Central Snapshots</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>

</project>

The mvn exec:execstatement looks equivalent to the java JVM statement. This question may be basically; What is maven doing differently than java, that in the maven case making the program run correctly?

mvn exec:exec语句看起来等同于 java JVM 语句。这个问题可能基本上是;maven 与 java 有什么不同,在 maven 情况下使程序正确运行?

Edit - additional maven and java debugging information

编辑 - 额外的 maven 和 java 调试信息

On mvn -X exec:exec, The output is given here http://roberthoff.com/files/mvn_exec_trace.txt

mvn -X exec:exec,这里给出输出http://roberthoff.com/files/mvn_exec_trace.txt

And on java -server -Dfile.encoding=utf-8 -XX:+UseNUMA -Xms1g -Xmx1g -classpath target/lib/* ;target/structr-ui-0.8.2.jar org.structr.Uiwe have http://roberthoff.com/files/java_trace.txt

java -server -Dfile.encoding=utf-8 -XX:+UseNUMA -Xms1g -Xmx1g -classpath target/lib/* ;target/structr-ui-0.8.2.jar org.structr.Ui我们有http://roberthoff.com/files/java_trace.txt

回答by HDave

You have a valid question here, because according to the Maven documentation on the exec:execgoalyou would need to have the <\classpath>stanza in your configuration in order to include the entire project module classpath.

您在这里有一个有效的问题,因为根据有关exec:exec目标Maven 文档,您需要<\classpath>在配置中包含该节才能包含整个项目模块类路径。

However, it appears as though Maven is running the program with the fully (and properly) configured classpath for some reason.

但是,由于某种原因,似乎 Maven 正在使用完全(且正确)配置的类路径运行程序。

In order to get to the bottom of the problem, I'd run Maven with the -Xcommand line argument and carefully scan through the debug output until I found the exact command it runs. Then I'd try running it from the same directory, ensuring I am using the same Java installation and see if it works.

为了找到问题的根源,我将使用-X命令行参数运行 Maven并仔细扫描调试输出,直到找到它运行的确切命令。然后我会尝试从同一目录运行它,确保我使用相同的 Java 安装并查看它是否有效。

回答by Franti?ek Hartman

Notice the maven-dependency-plugin in the pom. It copies all dependencies during the package phase to target/lib folder and then they are on classpath. Following should work:

注意 pom 中的 maven-dependency-plugin。它将打包阶段的所有依赖项复制到 target/lib 文件夹,然后它们在类路径上。以下应该工作:

maven clean package -DskipTests
java -cp target/lib/*;target/structr-ui-0.8.2.jar org.structr.Ui