Skip to content

Instantly share code, notes, and snippets.

@mdedetrich
Last active May 15, 2018 09:13
Show Gist options
  • Save mdedetrich/6432d085108774e4f117e246ccd23f96 to your computer and use it in GitHub Desktop.
Save mdedetrich/6432d085108774e4f117e246ccd23f96 to your computer and use it in GitHub Desktop.
Find tools.jar if it exists on your system, translated from https://stackoverflow.com/a/25163628
import java.util.*;
import java.io.File;
class FindToolsJar {
static ArrayList<String> paths = new ArrayList<String>();
static ArrayList<File> files = new ArrayList<File>();
static final String suffix = "/lib/tools.jar";
private static final void addStringToArray(String value) {
try {
if (value != null && !value.isEmpty())
paths.add(value);
} catch (Exception e) {
;
}
}
public static void main(String[] args) {
addStringToArray(System.getenv("JDK_HOME"));
addStringToArray(System.getenv("JAVA_HOME"));
addStringToArray(System.getProperty("java.home"));
try {
File file = new File(System.getProperty("java.home"));
addStringToArray(file.getParent());
} catch (Exception e) {
;
}
for (String path : paths) {
try {
File file = new File(path + suffix);
System.out.println(file.getAbsolutePath());
System.exit(0);
} catch (Exception e) {
System.out.println("Could not automatically find JDK/lib/tools.jar");
System.exit(1);
}
}
}
}
@mdedetrich
Copy link
Author

mdedetrich commented May 14, 2018

Compile with javac -target 1.5 -source 1.5 FindToolsJar.java which will produce a FindToolsJar.class file that will run with any Java version 1.5 or newer. You can then simply run with java FindToolsJar to find the location of tools.jar. You can also do java -classpath folder FindToolsJar if FindToolsJar.class is located in folder.

The idea behind this script is to quickly and easily locate the tools.jar which some Java instrumentation tools require to be on the classpath of your running application. Since Java is portable across different operating systems (and is always going to be located on a machine that runs JDK), the script was written itself in Java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment