26

From Java, is it possible to get the complete commandline with all arguments that started the application?

System.getEnv() and System.getProperties() do not appear to contain the values.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
aksamit
  • 2,325
  • 8
  • 28
  • 40
  • 3
    do you mean the arguments provided to the jvm also ? there's a question related to that http://stackoverflow.com/questions/1490869/how-to-get-vm-arguments-from-inside-of-java-application – LB40 Mar 29 '10 at 22:55

9 Answers9

20

Some of it is available from the RuntimeMXBean, obtained by calling ManagementFactory.getRuntimeMXBean()

You can then, for example call getInputArguments()

The javadocs for which say:

Returns the input arguments passed to the Java virtual machine which does not include the arguments to the main method. This method returns an empty list if there is no input argument to the Java virtual machine.

Some Java virtual machine implementations may take input arguments from multiple different sources: for examples, arguments passed from the application that launches the Java virtual machine such as the 'java' command, environment variables, configuration files, etc.

Typically, not all command-line options to the 'java' command are passed to the Java virtual machine. Thus, the returned input arguments may not include all command-line options.

Community
  • 1
  • 1
Stephen Denne
  • 36,219
  • 10
  • 45
  • 60
  • 2
    I think this is about the best you can get using pure Java. – clstrfsck Mar 29 '10 at 23:05
  • 1
    @Stephen Denne ... Not a fan of this one. My understanding of the question was to get the command line (say what shows in `ps -f` with the command string. As the manual page says, "_does not include the arguments to the main method._" Right now, I'm trying to determine what some other program is passing to a `main` that is not visible to my class. – will Feb 07 '18 at 03:38
5

In Linux that should be possible when you get the output of that command (run in a shell)

cat /proc/$PPID/cmdline

But that is not portable at all and should therefore not be used in Java...

Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136
  • Ok, thanks. It is ok if it is not portable, however I need it to work on Windows as well. – aksamit Mar 29 '10 at 22:20
  • 2
    @aksamit - in other words, you need it to be portable between (at least) Linux and Windows ... duh! @Johannes - "therefore should not be used in Java" is too strong. It is not totally wrong to write non-portable code in Java ... if circumstances require it. – Stephen C Mar 29 '10 at 23:30
  • @Johannes Weiß: so Sun where just out of their mind when they decided to put *Runtime.exec* in right? I mean, it obviously serves no need at all and should be removed from the language to please the "pure Java gods"? Or ? – SyntaxT3rr0r Mar 30 '10 at 00:37
5

Since Java 9 you may use ProcessHandle to get the command line of the process:

ProcessHandle.current().info().commandLine()
Nolequen
  • 3,032
  • 6
  • 36
  • 55
1

The following links may help you get there:

How to get command line arguments for a running process

get command-line of running processes

How to get a list of current open windows/process with Java?

Just as a note:

In Windows you have Process Explorer by Sysinternals that shows you the command line used to open the process. Right click the process and select Properties... You'll see Command Line in the window that is opened.

Community
  • 1
  • 1
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
1

You might want to look into how jps does this. It's a Java program that is able to get the full command line for all Java processes, including full class name of main class and JVM options.

Asgeir S. Nilsen
  • 1,137
  • 9
  • 13
  • The answer might be right, but it would be much more useful if extended: where can one inspect jps sources? Or ideally include "how `jps` does this` directly in the answer. – Suma Dec 30 '22 at 09:54
1

There is a environment variable %~dp0 which returns the complete path

ggonsalv
  • 1,264
  • 8
  • 18
  • 1
    Oops.. I forgot it is in windows. Very useful to run switch to UNC path for a batch file. For Example UNC path \\Server\share\folder1\runit.bat if the first line of the batch is pushd %~dp0 then your current path will be \\Server\share\folder1\ – ggonsalv Apr 17 '10 at 03:49
  • That answer is not relevant to why I found this question, but useful to know. – Justin Dearing Oct 23 '11 at 16:32
  • System.getenv("%~dp0") returns null also on Windows. – Horcrux7 May 04 '15 at 07:38
1

Have a look at YAJSW (Yet Another Java Service Wrapper) - it has JNA-based implementations for various OSes (including win32 and linux) that do exactly this so it can grab the commandline for a running process and create a config that wraps it in a service. A bit more info here.

Community
  • 1
  • 1
SamWest
  • 132
  • 1
  • 8
0

One option I've used in the past to maintain the cross-platform-shine is to set the command line as an environment variable prior to issuing the command.

Raymond Kroeker
  • 525
  • 1
  • 5
  • 13
0

If you are using solaris as the OS, take a look at "pargs" utility. Prints all the info required.

Keshav
  • 4,408
  • 8
  • 31
  • 50