在jar的类路径中添加com.sun.tools.jar [英] Adding com.sun.tools.jar in classpath of jar

查看:720
本文介绍了在jar的类路径中添加com.sun.tools.jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用jdk1.8.0_121/lib/tools.jar中存在的tools.jar.

I'm having trouble using tools.jar present in jdk1.8.0_121/lib/tools.jar.

我的$JAVA_HOME设置为:

# echo $JAVA_HOME
/usr/local/java/jdk1.8.0_121

tools.jar的路径是:

# ls /usr/local/java/jdk1.8.0_121/lib/tools.jar
/usr/local/java/jdk1.8.0_121/lib/tools.jar

然后我使用以下java可执行文件来运行代码:

And I use the following java executable to run the code:

/usr/local/java/jdk1.8.0_161/bin/java

但是,当我访问 VirtualMachine 类,它会引发

But, when I access the VirtualMachine class, it throws

Caused by: java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_161]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_161]
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) ~[na:1.8.0_161]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_161]
        ... 72 common frames omitted

有人可以解释为什么Java在其类路径&中找不到lib/tools.jar的原因.我该怎么做才能纠正这种行为?

Can someone explain why Java is not able to find lib/tools.jar in its classpath & What can I do to correct this behaviour?

要在本地计算机上运行,​​我在pom中添加了以下依赖项:

To run on my local machine, I've added the following dependency in my pom:

<dependency>
    <groupId>com.sun</groupId>
    <artifactId>tools</artifactId>
    <version>1.8</version>
    <scope>system</scope>
    <systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>

但是,当我将其部署到服务器上时,由于system作用域&在服务器的jdk路径上也找不到jar.

But, when I deploy it on the server, this jar is not packaged due to system scope & neither does it find the jar on the server's jdk path.

不是应该自动找到所有的jdk jar吗?

Isn't it supposed to find all the jdk jars automatically?

我还试图在jar的MANIFEST文件的类路径条目中添加env变量$JAVA_HOME,如下所示:

I've also tried to add env variable $JAVA_HOME in the class-path entry of jar's MANIFEST file as follows:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: pankajsinghal
Class-Path: $JAVA_HOME/lib/
Created-By: Apache Maven 3.5.4
Build-Jdk: 1.8.0_181

但是,这也不起作用.另外,我不想在我的代码中显式添加此lib的jar,因为它是JDK lib,我想访问它的正确方法是从系统的JDK路径本身.因此,在这个方向上寻找解决方案.

But, this is also not working. Also, I don't want to add this lib's jar in my code explicitly as it's a JDK lib and I guess the proper way to access this would be from the system's JDK path itself. So, looking for a solution in this direction itself.

我们非常感谢您的帮助.

Any help is really appreciated.

推荐答案

您可以尝试如下操作:

java -cp "/path/your.jar:/usr/local/java/jdk1.8.0_121/lib/tools.jar" your.MainClass

或参考以下内容:

类型com.sun .tools.javac.util.Assert".无法访问

希望它对您有所帮助.

这篇关于在jar的类路径中添加com.sun.tools.jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆