Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You may be able to find the JDK path by looking to see where javac is installed. Assuming that "javac" is within the environment paths of the system then you can retrieve the path by passing "where javac" to code such as the following </p> <pre><code>public static String getCommandOutput(String command) { String output = null; //the string to return Process process = null; BufferedReader reader = null; InputStreamReader streamReader = null; InputStream stream = null; try { process = Runtime.getRuntime().exec(command); //Get stream of the console running the command stream = process.getInputStream(); streamReader = new InputStreamReader(stream); reader = new BufferedReader(streamReader); String currentLine = null; //store current line of output from the cmd StringBuilder commandOutput = new StringBuilder(); //build up the output from cmd while ((currentLine = reader.readLine()) != null) { commandOutput.append(currentLine); } int returnCode = process.waitFor(); if (returnCode == 0) { output = commandOutput.toString(); } } catch (IOException e) { System.err.println("Cannot retrieve output of command"); System.err.println(e); output = null; } catch (InterruptedException e) { System.err.println("Cannot retrieve output of command"); System.err.println(e); } finally { //Close all inputs / readers if (stream != null) { try { stream.close(); } catch (IOException e) { System.err.println("Cannot close stream input! " + e); } } if (streamReader != null) { try { streamReader.close(); } catch (IOException e) { System.err.println("Cannot close stream input reader! " + e); } } if (reader != null) { try { streamReader.close(); } catch (IOException e) { System.err.println("Cannot close stream input reader! " + e); } } } //Return the output from the command - may be null if an error occured return output; } </code></pre> <p>The string returned will be the exact location of javac so you may need extra processing to get the directory that javac resides in. You will need to distinguish between Windows and other OS</p> <pre><code>public static void main(String[] args) { //"where" on Windows and "whereis" on Linux/Mac if (System.getProperty("os.name").contains("win") || System.getProperty("os.name").contains("Win")) { String path = getCommandOutput("where javac"); if (path == null || path.isEmpty()) { System.err.println("There may have been an error processing the command or "); System.out.println("JAVAC may not set up to be used from the command line"); System.out.println("Unable to determine the location of the JDK using the command line"); } else { //Response will be the path including "javac.exe" so need to //Get the two directories above that File javacFile = new File(path); File jdkInstallationDir = javacFile.getParentFile().getParentFile(); System.out.println("jdk in use at command line is: " + jdkInstallationDir.getPath()); }//else: path can be found } else { String response = getCommandOutput("whereis javac"); if (response == null) { System.err.println("There may have been an error processing the command or "); System.out.println("JAVAC may not set up to be used from the command line"); System.out.println("Unable to determine the location of the JDK using the command line"); } else { //The response will be "javac: /usr ... " //so parse from the "/" - if no "/" then there was an error with the command int pathStartIndex = response.indexOf('/'); if (pathStartIndex == -1) { System.err.println("There may have been an error processing the command or "); System.out.println("JAVAC may not set up to be used from the command line"); System.out.println("Unable to determine the location of the JDK using the command line"); } else { //Else get the directory that is two above the javac.exe file String path = response.substring(pathStartIndex, response.length()); File javacFile = new File(path); File jdkInstallationDir = javacFile.getParentFile().getParentFile(); System.out.println("jdk in use at command line is: " + jdkInstallationDir.getPath()); }//else: path found }//else: response wasn't null }//else: OS is not windows }//end main method </code></pre> <p>Note: if the method returns null / error it doesn't mean that javac doesn't exist - it will most likely be that javac isn't within the PATH environment variable on windows and so cannot be found using this method. This isn't likely on Unix as Unix usually adds the jdk/bin directory to the PATH automatically. Also, this will return the javac version currently in use at command line, not necessarily the latest version installed. so if e.g. 7u12 and 7u13 are installed but command prompt is set to use 7u12 then that's the path that will be returned.</p> <p>Only tested on multiple Windows machines but works fine on them.</p> <p>Hope this is helpful. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload