Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mvn exec:java不';不能正确设置类路径_Java_Maven - Fatal编程技术网

mvn exec:java不';不能正确设置类路径

mvn exec:java不';不能正确设置类路径,java,maven,Java,Maven,我试图在命令行上使用Maven运行Java程序,但它没有将正确的条目放在类路径上。如果我在IntelliJ(支持Maven)中运行该程序,则类路径有80个左右的条目,包括我的项目的jar依赖项、编译程序类和src/main/resources中的资源。如果我用mvn exec:java运行程序,我只会得到一个apache-maven-3.0.4/boot/plexus-classworlds-2.4.jar。在我的整个项目树中没有对plexus的引用。这个条目来自哪里?为什么其他预期的类路径条目

我试图在命令行上使用Maven运行Java程序,但它没有将正确的条目放在类路径上。如果我在IntelliJ(支持Maven)中运行该程序,则类路径有80个左右的条目,包括我的项目的jar依赖项、编译程序类和src/main/resources中的资源。如果我用
mvn exec:java
运行程序,我只会得到一个
apache-maven-3.0.4/boot/plexus-classworlds-2.4.jar
。在我的整个项目树中没有对plexus的引用。这个条目来自哪里?为什么其他预期的类路径条目不在那里

Maven版本:
ApacheMaven 3.0.4(r1232337;2012-01-17 00:44:56-0800)

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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>MyProject</artifactId>
<version>SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <!-- lots of dependencies -->
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <showDeprecation>true</showDeprecation>
                <showWarnings>true</showWarnings>
                <executable>${env.JAVA_HOME}/bin/javac</executable>
                <fork>true</fork>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptors>
                    <descriptor>${basedir}/src/assembly/assembly.xml</descriptor>
                </descriptors>
            <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1</version>
            <configuration>
                <mainClass>com.example.MyApp</mainClass>
                <executable>${env.JAVA_HOME}/bin/java</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

4.0.0
com.example
我的项目
快照
罐子
UTF-8
org.apache.maven.plugins
maven编译器插件
2.5.1
1.7
1.7
真的
真的
${env.JAVA_HOME}/bin/javac
真的
maven汇编插件
${basedir}/src/assembly/assembly.xml
假的
org.codehaus.mojo
execmaven插件
1.1
com.example.MyApp
${env.JAVA_HOME}/bin/JAVA

默认情况下,exec:java使用“runtime”作用域,它不会将依赖项设置为“compile”作用域

您可以使用:

exec:java -Dexec.classpathScope="compile"
包括编译依赖项(不能100%确定-D语法,但变量肯定是exec.classpathScope)

这应该能奏效。 如果您需要更多信息/选项,插件页面会列出一些选项:

我不知道Plexus(我猜这是Exec Maven插件的一个依赖项?),但请尝试在启用调试的情况下运行:
mvn Exec:java-X
,应该更清楚的是,您的依赖项正在添加到类路径中:

....
[DEBUG] Invoking : com.example.MyApp.main()
[DEBUG] Plugin Dependencies will be excluded.
[DEBUG] Project Dependencies will be included.
[DEBUG] Collected project artifacts [log4j:log4j:jar:1.2.16:compile, commons-lang:commons-lang:jar:2.6:compile]
[DEBUG] Collected project classpath [C:\MyProject\target\classes]
[DEBUG] Adding to classpath : file:/C:/MyProject/target/classes/
[DEBUG] Adding project dependency artifact: log4j to classpath
[DEBUG] Adding project dependency artifact: commons-lang to classpath
....

您应该会看到很多“添加项目依赖项工件”的消息。

请显示您的完整pom,否则无法提供任何帮助。此外,您使用哪种maven版本会有帮助?哪个版本的exec maven插件?等等。我已经用pom和Maven版本更新了我的问题,谢谢。首先尝试更新。我尝试用
mvn exec:java-Dexec.classpathScope=“compile”
运行,但仍然存在相同的问题。我还尝试了
-Dexec.includeProjectDependencies=true
-Dexec.includePlugins Dependencies=true
,但都没有任何效果。includeProjectDependencies和includePlugins Dependencies不是必需的。不管classpathScope如何,您必须(至少)有一个其他问题。您是否尝试过“mvn依赖关系:树”来理解plexus依赖关系的来源?