执行javac失败:编译失败

问题描述 投票:0回答:6

在两台不同的笔记本电脑上使用 Maven 构建同一项目。一台运行良好,一台显示错误。

状态:两个系统的配置相同。

C:\Users\admin>mvn -version
Apache Maven 2.2.1 (r801777; 2009-08-07 00:46:01+0530)
Java version: 1.6.0_43
Java home: C:\Installers\Java\jdk1.6.0_43\jre
Default locale: en_IN, platform encoding: Cp1252
OS name: "windows 7" version: "6.1" arch: "amd64" Family: "windows"

使用命令:mvn clean install -DskipTests=true

错误:

[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 3 source files to C:\Users\admin\HeliosWorkspace\...\target\classes
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
Failure executing javac,  but could not parse the error:
The system cannot find the path specified.

[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.BuildFailureException: Compilation failure
Failure executing javac,  but could not parse the error:
The system cannot find the path specified.    

        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor
.java:715)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifec
ycleExecutor.java:556)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.
java:535)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultL
ifecycleExecutor.java:387)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleE
xecutor.java:348)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java
:180)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
Failure executing javac,  but could not parse the error:
The system cannot find the path specified.

我什至尝试删除所有内容,例如再次创建 .m2 文件夹。

maven
6个回答
5
投票

我遇到了这个问题,并通过一些故障排除解决了它。

当您尝试执行文件但输入了无效路径时,这实际上是一条 DOS 错误消息。

C:\Users\me>c:\asdf\foo.exe
The system cannot find the path specified.

Maven 正在尝试执行 javac 来编译您的代码,但没有正确的路径。对于许多设置,您需要在 pom.xml 中检查编译器的路径。转到 cmd 提示符并将其复制并粘贴到其中并确保其路径有效。

对于使用 settings.xml 配置文件在不同 JAVA_HOME 上定义一些全局配置的设置(例如下面的示例),请确保每个变量中的路径正确,包含这些变量的配置文件处于活动状态(activeProfile 标签可以确保这一点),并且是在您的 pom.xml 中正确引用

    <!-- settings.xml -->
  <profiles>
    <profile>
      <id>compiler-versions</id> 
        <properties>
            <JAVA_1_5_HOME>C:/java/jdk1.5.0_16</JAVA_1_5_HOME>
            <JAVA_1_6_HOME>C:/java/jdk1.6.0_43</JAVA_1_6_HOME>
            <JAVA_1_7_HOME>C:/java/jdk1.7.0_55</JAVA_1_7_HOME>
      </properties>
    </profile>
  </profiles>


     <activeProfiles>
        <!-- make the profile active all the time -->
        <activeProfile>compiler-versions</activeProfile>
     </activeProfiles>

pom.xml 片段:

    <!-- pom.xml -->
    <!-- ... -->
            <build>
                <configuration>
                    <verbose>false</verbose>
                    <fork>true</fork>
                    <executable>${JAVA_1_6_HOME}/bin/javac</executable>
                    <compilerVersion>1.6</compilerVersion>
                    <meminitial>256m</meminitial>
                    <source>1.6</source>
                    <target>1.6</target>
                    <!--encoding>UTF-8</encoding-->
                    <maxmem>512m</maxmem>
                </configuration>
            </build>

2
投票

谢谢大家,现在可以使用了:

mvn -X clean install -DskipTests=true

调试模式非常有帮助。


0
投票

我遇到了类似的问题,我发现 settings.xml @ Maven_home/conf 或 C:\Users\.m2\settings.xml 指向错误的 JDK 路径,更正它确实解决了问题。上面打印的错误消息实际上是“Failureexecutingjavac”,这意味着 Maven 没有找到 JDK。

谢谢 希斯


0
投票

我知道这是一个老话题,但我认为这个答案会对未来的人们有所帮助:

Java主目录:C:\Installers\Java\jdk1.6.0_43\jre

您正在使用 JRE 而不是 JDK。只需从您的 java 主目录中删除“\jre”即可


0
投票

你可以使用@jiman指示的示例,但是如果在不同的计算机上使用,JDK的安装路径不应该烧录在pom或配置文件中,因为它可能会有所不同。

为了解决此冲突,我所做的是从 git 控制台更改 JAVA_HOME 环境变量,将其分配给我的 JDK 安装路径,如下所示:

导出 JAVA_HOME='C:\Program Files\Java\jdk1.8.0_301'

请记住,它仅适用于每个会话,也就是说,每次打开新控制台时,都必须执行相同的操作。


-1
投票

检查您的 POM 文件并确保您拥有适用于 MVN 和 Java 的正确环境变量

© www.soinside.com 2019 - 2024. All rights reserved.