aksamit
aksamit

Reputation: 2453

How do I get the commandline that started the process

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.

Upvotes: 27

Views: 27329

Answers (9)

Johannes Weiss
Johannes Weiss

Reputation: 54111

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...

Upvotes: 5

Nolequen
Nolequen

Reputation: 4337

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

ProcessHandle.current().info().commandLine()

Upvotes: 8

Keshav
Keshav

Reputation: 4478

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

Upvotes: 0

SamWest
SamWest

Reputation: 131

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.

Upvotes: 1

ggonsalv
ggonsalv

Reputation: 1262

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

Upvotes: 1

Raymond Kroeker
Raymond Kroeker

Reputation: 532

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.

Upvotes: 0

Stephen Denne
Stephen Denne

Reputation: 37057

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.

Upvotes: 21

Asgeir S. Nilsen
Asgeir S. Nilsen

Reputation: 1137

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.

Upvotes: 1

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102448

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.

Upvotes: 1

Related Questions