How to get the path to the current run of the Java command line path? (to symbolic links)

I wrote a program that displays a usage hint. It currently returns the path to the main jar file that was originally entered on the command line.

 Usage: java -jar path/to/MyJar.jar <params> ... 

For completeness, I would like to make sure that the java bit also responded back, since there are various ways to access java (except for the word java and shorter than the canonical path to /us/opt/java-1.8.0-u123/bin/java )

 Usage: /us/opt/java7/bin/java -jar MyJar.jar <params> ... Usage: ./bin/java -jar MyJar.jar <params> ... Usage: java -jar MyJar.jar <params> ... # whatever the user typed in 

How to determine which command was used to invoke the JVM?
Before evaluating symbolic links, I need the original command line value.

I do not use System.getProperty("java.home") because it does not respect the original command line value, but only the final "canonical" JVM location. (Using a usage note, for example Usage: /us/opt/java-1.8.0-u123/jre/bin/java -jar ... , would be pretty verbose,
especially when using simple java on the command line.)

Is java command line location determination possible using pure java code?
(i.e. do not use script wrapper in bash )

+5
source share
2 answers

Use the following command

 jps -mlvV 

This should print everything about running java processes.

Where ps -e should indicate the executable path.

The following is a pure Java solution, it does not output the actual command used to execute the application, but creates a command that will have the same effect.

 import java.io.File; import java.lang.management.ManagementFactory; import java.util.List; import java.util.Map.Entry; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { System.out.println("Printing command"); createCommand(); } public static void createCommand() { try { String jvm = getJvmExecutable(); String mainClassName = findMainClass(); String processDir = System.getProperty("user.dir"); String arguments = getArguments(); String classpath = ManagementFactory.getRuntimeMXBean().getClassPath(); String command = String.format("cd %s & %s %s -classpath %s %s",processDir, jvm, arguments, classpath, mainClassName); System.out.println(command); } catch (ClassNotFoundException e) { e.printStackTrace(); } } private static String getJvmExecutable() { String jvm = ""; jvm = System.getProperty("java.home"); jvm += File.separator + "bin" + File.separator + "java"; jvm = '"' + jvm + '"'; return jvm; } private static String getArguments() { List<String> argsList = ManagementFactory.getRuntimeMXBean().getInputArguments(); String args = argsList.stream().collect(Collectors.joining(" ")); return args; } public static String findMainClass() throws ClassNotFoundException{ for (Entry<Thread, StackTraceElement[]> entry : Thread.getAllStackTraces().entrySet()) { Thread thread = entry.getKey(); if (thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals("main")) { for (StackTraceElement stackTraceElement : entry.getValue()) { if (stackTraceElement.getMethodName().equals("main")) { try { Class<?> c = Class.forName(stackTraceElement.getClassName()); Class[] argTypes = new Class[] { String[].class }; //This will throw NoSuchMethodException in case of fake main methods c.getDeclaredMethod("main", argTypes); return stackTraceElement.getClassName(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } } } } return null; } } 

Note. . It will work with regular java projects or regular JARS, however it will not work with special class loads, such as WAR, OSGI or Spring Load class loading.

I used the findMainClass() method from this answer fooobar.com/questions/1253684 / ...

0
source

Use this System property in code

 System.getProperty("java.home"); 

This will return the installation directory for the Java Runtime Environment (JRE)

More here

0
source

All Articles