My Problem (with MAVEN) just exist on Windows (not Linux) and not Inside the IDE (Eclipse)
Class:
...
System.out.println("Location of Tools --> " + VirtualMachine.class.getProtectionDomain().getCodeSource().getLocation());
...
Exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/attach/VirtualMachine
at my.package.bootstrap(Controller.java:37)
at my.package.main(Main.java:35)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
My Environment:
Windows: Windows 10 (Here are the Problems!!!)
Linux: Xubuntu 16.04 (Works fine)
Java: 8 oracle (not openjdk!)
Maven (**using the maven-assembly-plugin for installing**)
My Project Architecture:
My Project which using the com.sun.tools is a library: lib.jar
which gets used by Main Project: Main.jar.
I'm using maven to compile all of that into Main.jar
What works:
Linux Eclipse IDE: found
Linux - Shell: java -jar myjar.jar: found
Windows Eclipse IDE: found
--> Windows - Bash: /java_jdk_path/java -jar myjar.jar: _NOT_FOUND_
My not-working-solutions to make it find the lib on windows in main.jar:
set %JAVA_HOME% ... to jdk
set %JRE_HOME% ... to jre
I found a lot of workarounds like i.e. maven profiles and so on, I tested all of them but nothing worked. One time I got it worked, when I copyed the com.sun.tools via maven plugin (mvn install...) into the JAR. Then it worked.
But I need it to find the com.sun.tools dynamically - depending on the operating system linux and windows.
Any suggestions for my next steps? I don't know what I could do that it works. I always get this exception when using this tools.jar on windows after executing it with ...java -jar Main.jar
Goal: I want to start Main.jar (java -jar Main.jar) on any folder I want.
UPDATE: I added a sample Project, which fails for me exactly with this error.
Just create a new Maven Project and add this to Main.java and to POM. Install it with: mvn install and it fails. Use the eclipse jar builder and it works!??? WTF Why?
Code:
...
System.out.println("Location of Tools --> " + VirtualMachine.class.getProtectionDomain().getCodeSource().getLocation());
...
POM:
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${env.JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<outputDirectory>C:\Output\</outputDirectory>
<archive>
<manifest>
<mainClass>com.test.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I know its not the solution for the current problem, but I am using a workaround and I can recommend it to any other. I am using https://github.com/oshi/oshi now, because it has all the features what I need and wanted to get with that com.sun.tools.
But Anyway it would be nice to hear if anyone has a working solution for this maven install (maybe maven {java.home} bug?) problem.