Maven编译报错'javac'未识别,JDK8能否兼容构建?
Maven编译报错'javac'未识别的解决及相关问题解答
报错原因分析
当前报错是因为maven-compiler-plugin配置了<fork>true</fork>和<executable>${websphere_jdk_5}</executable>,但${websphere_jdk_5}变量未被正确定义为有效的javac路径,导致Maven无法找到指定的编译器执行文件,从而抛出'javac'未识别的错误。
本地JDK8能否兼容完成构建?
完全可以。JDK8的javac编译器支持通过-source和-target参数生成兼容JDK1.5的字节码,完全满足你配置中<source>1.5</source>和<target>1.5</target>的要求,不需要额外安装JDK5。
是否必须安装WebSphere应用服务器才能构建?
不需要。配置中的<executable>${websphere_jdk_5}</executable>只是指定编译器路径,这个路径不一定是WebSphere自带的JDK,只要指向本地有效JDK(比如你的JDK8)的javac执行文件即可。
具体解决方法
你可以通过以下几种方式修改配置解决问题:
方法1:移除指定的executable和fork配置(推荐)
让Maven直接使用当前环境变量配置的JDK8编译,修改后的插件配置如下:
<plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> <meminitial>256m</meminitial> <maxmem>512m</maxmem> </configuration> </plugin> </plugins>
方法2:保留fork配置,指定本地JDK8的javac路径
将executable的值改为你本地JDK8中javac的实际路径:
- Windows系统示例:
<executable>C:\Program Files\Java\jdk1.8.0_381\bin\javac.exe</executable>
- Linux/macOS系统示例:
<executable>/usr/lib/jvm/java-8-openjdk-amd64/bin/javac</executable>
方法3:定义websphere_jdk_5变量
在Maven的settings.xml中添加变量定义,或者编译时通过命令行传入:
- 在
settings.xml的<profiles>中添加:
<profile> <id>jdk-profile</id> <properties> <websphere_jdk_5>C:\Program Files\Java\jdk1.8.0_381\bin\javac.exe</websphere_jdk_5> </properties> </profile>
激活该profile后执行编译;或者直接用命令:
mvn clean compile -Dwebsphere_jdk_5=你的javac路径
内容的提问来源于stack exchange,提问作者JSS
相关产品推荐
相关产品推荐

