rt.jar
and tools.jar
部分更详细地描述了这一点,但Risks and Assumptions包含了一个很好的总结:
因此,正如你所观察到的,这些文件已经不存在了。在下面还有更多内容:JDK和JRE映像将不再包含
lib/rt.jar
、lib/tools.jar
、lib/dt.jar
以及其他内部jar文件,如上所述。现有代码假定这些文件的存在可能无法正常工作。
结果表明,JDK 9 兼容的解决方案与原始技巧并没有太大的区别:
<profiles>
<profile>
<id>jigsaw</id>
<activation>
<jdk>[1.9,)</jdk>
</activation>
<!-- No dependencies needed by Jigsaw -->
<dependencies/>
</profile>
<profile>
<id>default-jdk</id>
<activation>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<scope>system</scope>
<version>1.6</version>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
<profile>
<id>osx-jdk</id>
<activation>
<file>
<exists>${java.home}/../Classes/classes.jar</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<scope>system</scope>
<version>1.6</version>
<systemPath>${java.home}/../Classes/classes.jar</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
如果整个依赖
声明都移到配置文件
中,就可以让不同的配置文件使用不同数量的依赖项。
我创建了一个可重用模块,以隐藏对依赖tools.jar的个别项目的复杂性:
<dependency>
<groupId>com.github.olivergondza</groupId>
<artifactId>maven-jdk-tools-wrapper</artifactId>
<version>0.1</version>
</dependency>
你的解决方案(仅仅是权宜之计)被认为已经失效,并且在Java 9中将无法使用。你唯一能做的就是运行jdeps
并将你的代码移动到公共API。
tools.jar
中的类提供,因此保持项目可构建需要解决这个问题,特别是对于旧版JDK。 - Oliver Gondža