引用 tools.jar 下的类, 编译不过?

2021-06-22 15:21:16 +08:00
 asanelder

代码

import com.sun.tools.javac.util.Abort;   // 来自 tools.jar 中的类

public class Test {
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}

使用 javac 来编译, 报如下错误

程序包 com.sun.tools.javac.util 不存在

使用的是 jdk1.8

有铁子知道原因么?

1180 次点击
所在节点    程序员
4 条回复
yzmm
2021-06-22 15:49:38 +08:00
Linux:
javac -cp $JAVA_HOME/lib/tools.jar Test.java

Windows:
javac -classpath %JAVA_HOME%/lib/tools.jar Test.java
ThreeBody
2021-06-22 15:54:12 +08:00
如果你的是 maven 项目的话,可以直接引入依赖
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
uselessVisitor
2021-06-22 17:02:19 +08:00
This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notic
asanelder
2021-06-22 18:34:28 +08:00
@yzmm #1
@ThreeBody #2

感谢二位, 确实是这么做的, 这个 tools.jar 并不是在默认的 classpath 中, 得显式指定

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/785082

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX