Adding com.sun.tools.jar in classpath of jar

混江龙づ霸主 提交于 2020-01-06 05:25:14

问题


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

My $JAVA_HOME is set to:

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

The path to tools.jar is :

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

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

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

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

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


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>

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.

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

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

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.


回答1:


You have to add that jar in project properties. In eclipse, To Add this Jar to your build path Right click the Project > Build Path > Configure build path> Select Libraries tab > Click Add External Libraries > Select the Jar file.



来源:https://stackoverflow.com/questions/51707812/adding-com-sun-tools-jar-in-classpath-of-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!