Last active
May 15, 2018 09:13
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile with
javac -target 1.5 -source 1.5 FindToolsJar.javawhich will produce aFindToolsJar.classfile that will run with any Java version 1.5 or newer. You can then simply run withjava FindToolsJarto find the location oftools.jar. You can also dojava -classpath folder FindToolsJarifFindToolsJar.classis located infolder.The idea behind this script is to quickly and easily locate the
tools.jarwhich 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.