Skip to content

Commit

Permalink
added @fancellu's idea of using JAVA_HOME too to try discover tools.j…
Browse files Browse the repository at this point in the history
…ar for #718
  • Loading branch information
jstrachan committed Nov 4, 2013
1 parent 1bc611a commit 340de34
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions hawtio-app/src/main/java/io/hawt/app/App.java
Expand Up @@ -25,6 +25,10 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
*/
Expand All @@ -43,17 +47,29 @@ public static void main(String[] args) {
//System.out.println("Found " + aClass + " on the classpath!");
} catch (Exception e) {
// lets try find the tools.jar instead
String path = System.getProperty("java.home", ".");
String jreSuffix = File.separator + "jre";
if (path.endsWith(jreSuffix)) {
path = path.substring(0, path.length() - jreSuffix.length());
Set<String> paths = new HashSet<String>();
String javaHome = System.getProperty("java.home", ".");
addPath(paths, javaHome);

// now lets try the JAVA_HOME environment variable just in case
javaHome = System.getenv("JAVA_HOME");
if (javaHome != null && javaHome.length() > 0) {
addPath(paths, javaHome);
}

boolean found = false;
for (String path : paths) {
File file = new File(path, "lib/tools.jar");
if (file.exists()) {
found = true;
//System.out.println("Found tools.jar at " + file);
main.setExtraClassPath("file://" + file.getCanonicalPath());
break;
}
}
File file = new File(path, "lib/tools.jar");
if (file.exists()) {
//System.out.println("Found tools.jar at " + file);
main.setExtraClassPath("file://" + file.getCanonicalPath());
} else {
System.out.println("Failed to load class " + virtualMachineClass + " and find tools.jar at " + file + ". " + e);
if (!found) {
System.out.println("Failed to load class " + virtualMachineClass
+ " and find tools.jar in directories " + paths + ". " + e);
}
}

Expand Down Expand Up @@ -87,6 +103,15 @@ public static void main(String[] args) {
}
}

protected static void addPath(Set<String> paths, String path) {
paths.add(path);
String jreSuffix = File.separator + "jre";
if (path.endsWith(jreSuffix)) {
path = path.substring(0, path.length() - jreSuffix.length());
paths.add(path);
}
}

private static Class<?> loadClass(String name, ClassLoader... classLoaders) throws ClassNotFoundException {
for (ClassLoader classLoader : classLoaders) {
try {
Expand Down

0 comments on commit 340de34

Please sign in to comment.