在较早的Java版本中(特别是Java 8及之前版本),tools.jar包含了JDK的一些核心工具,比如编译器(javac)等。在使用Maven构建的项目中,如果需要依赖tools.jar中的类或功能,可以通过特定的配置来实现。然而,从Java 9开始,JDK的模块化改动导致了tools.jar不再存在,其内容被整合到了JDK的其它部分。因此,这里主要适用于Java 8及之前的版本。
参考文档:Java中将JDK中tools.jar配置在maven中-CJavaPy
maven文档:Maven Settings - Settings
maven配置
<profiles> <profile> <id>default-profile</id> <activation> <activeByDefault>true</activeByDefault> <file> <exists>${java.home}/../lib/tools.jar</exists> </file> </activation> <properties> <toolsjar>${java.home}/../lib/tools.jar</toolsjar> </properties> </profile> <profile> <id>mac-profile</id> <activation> <activeByDefault>false</activeByDefault> <file> <exists>${java.home}/../Classes/classes.jar</exists> </file> </activation> <properties> <toolsjar>${java.home}/../Classes/classes.jar</toolsjar> </properties> </profile> </profiles>
将上面配置添加Pom.xml中。
然后在Eclipse中Windows > Preferecnes > Installed JREs,配置一下正确的JRE的安装路径。