I'd like to know how it is started. What is the command to start this java process ? What I mean is I have one running java process, and I'd like to know the command to start it, such as what is the main class and what is the arguments, etc.
Any tool for that ? Thanks
There is a command line tool that comes with the JDK: jps, that will give you the list of java processes being run at the moment you execute the command, the arguments given to the method main and the parameters used for the JVM. Try this:
path\to\jdk\bin\jps -m -l -v
It won't give you the exact command used to start the process, but it will give you a hint of how to "rebuild" that command.
For more info, if you are on a decent distro of linux, try man jps or if you are on Windows, see the Oracle documentation about jps.
Your question wasn't clear. If you are looking to find the command that launched this process than you can look at the property sun.java.command. This will give you the main class name and arguments passed to it. java.class.path property gives you the class path. You can get the arguments passed to the java command itself by using ManagementFactory.getRuntimeMXBean().getInputArguments() method. Using all these you should be able to reconstruct the java command.
If you use Windows, you can use the Taskmanager, go to the Process/Details Tab, where you can see the PID for each Process. There you can add a column for the command line (e.g. in German its "Befehlszeile", i'm not sure how that column is labeled in English).
Then just look at the java.exe/javaw.exe Processes.
You could also use the alternative Taskmanager from Microsoft, Process Explorer, afaik there you can just click right on a process and select details.
Related
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.
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.
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...
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.
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.
There is a environment variable %~dp0 which returns the complete path
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.
Since Java 9 you may use ProcessHandle to get the command line of the process:
ProcessHandle.current().info().commandLine()
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.
If you are using solaris as the OS, take a look at "pargs" utility. Prints all the info required.
In my company environment, there's this script that runs on plenty computers and that I occasionally ask the users to kill because it's known to lock up from time to time.
I was thinking of a way to kill it myself. I've noticed I can remotely list using pslist and then killing it using pskill.
Now the problem is, when somebody's running multiple java applications (ex. Eclipse, this application, another java.exe app,...) it becomes tricky to kill the correct application in the pslist, that would look something like this:
javaw 4214 .. ...
javaw 5000 .. ...
And so on. These are different applications, but they all run from javaw.exe. Is there a way of finding out the name of the .jar they are running, so I can kill the process based on that?
You can list java processes with
jps
or
jps -v
Jps is a tool provided with JDK and JRE, you'll find it in JDK_HOME/bin.
Option -v shows additional info (JVM start parameters)
You can use jps. It alone doesn't provide much helpful information to identify the java application. You need little bit extra details.
You can use the following options with jps to print the complete command line arguments passed to the java appliation (JVM arguments and main method arguments),
jps -lvm
Reference: https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jps.html
I have a running java process, I want to find out with which parameters it was started, specifically, I want to know what debug port was specified (it was). Is there a way to do that in linux?
Update: Application was started through ant, so I can get ant command options, so now I know the task that was started, also I know ant process id.
jps -v -m might help also you can cat /proc/<pid>/cmdline
EDIT: jvisualvm provide also a bunch of infos about running java processes.
Why not just use ps -fe ?
From the ps man page
-f Do full-format listing. This option can be combined
with many other UNIX-style options to add additional columns. It also
causes the command arguments to be printed. When used with -L, the NLWP (number of threads) and LWP (thread ID)
columns
will be added. See the c option, the format keyword args, and the format keyword comm.
(my emphasis)
jps.exe which found on JDK 1.5 and later could monitor all Java process but is there a way to detect the specify command line and terminate the correct pid?
What if the user have JRE, is there a similar code allow us to terminate any process easily?
Prefer to keep the topic on Windows which I am working on.
The jps command supports a number of options that modify the output of the command. These options are subject to change or removal in the future.
-q Suppress the output of the class name, JAR file name, and arguments passed to the main method, producing only a list of local VM identifiers.
-m Output the arguments passed to the main method. The output may be null for embedded JVMs.
-l Output the full package name for the application's main class or the full path name to the application's JAR file.
-v Output the arguments passed to the JVM.
-V Output the arguments passed to the JVM through the flags file (the .hotspotrc file or the file specified by the -XX:Flags= argument).
Pipe the output of jps to grep or sed or awk or perl or even another Java program for further matching, parsing and action. On Windows, the easiest way to get those utilities is through Cygwin.
Here are some Microsoft downloadable command line utilities which are useful for working with processes on Windows:
pskill
pslist
and the rest of the Sysinternals Suite
If the user don't have jps, you can use ps. The command line options for ps differs between platforms, see man ps on you system. I use ps -C java -o pid,time,cmd to list java processes on a CentOS system. Then kill to terminate.
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.
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.
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...
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.
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.
There is a environment variable %~dp0 which returns the complete path
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.
Since Java 9 you may use ProcessHandle to get the command line of the process:
ProcessHandle.current().info().commandLine()
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.
If you are using solaris as the OS, take a look at "pargs" utility. Prints all the info required.