V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
asanelder
V2EX  ›  程序员

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

  •  
  •   asanelder · 2021-06-22 15:21:16 +08:00 · 1138 次点击
    这是一个创建于 1010 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码

    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

    有铁子知道原因么?

    4 条回复    2021-06-22 18:34:28 +08:00
    yzmm
        1
    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
        2
    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
        3
    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
        4
    asanelder  
    OP
       2021-06-22 18:34:28 +08:00
    @yzmm #1
    @ThreeBody #2

    感谢二位, 确实是这么做的, 这个 tools.jar 并不是在默认的 classpath 中, 得显式指定
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1553 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:13 · PVG 01:13 · LAX 10:13 · JFK 13:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.