How can I list all Java processes in bash?
I need an command line. I know there is command ps
but I don't know what parameters I need to use.

- 1,755
- 2
- 12
- 9
-
1please show a bit more effort in researching this and describing your problem, there are a lot of possibilities depending on what exactly you're after. – Mat Jun 08 '11 at 18:03
-
3If you know there is `ps` then first try `man ps`,`info ps` and learn about it in the Internet. – ssapkota Jun 08 '11 at 23:10
18 Answers
Recent Java comes with Java Virtual Machine Process Status Tool "jps"
http://download.oracle.com/javase/1.5.0/docs/tooldocs/share/jps.html
For example,
[nsushkin@fulton support]$ jps -m
2120 Main --userdir /home/nsushkin/.netbeans/7.0 --branding nb
26546 charles.jar
17600 Jps -m

- 13,050
- 3
- 30
- 20
-
2Just a note: jps only ships with the JDK, not the JRE. Machines with plain ol' Java runtimes on them won't have this tool. – jake Jun 17 '15 at 17:43
-
-
I've been utilizing this for years, however it only shows current user's processes. Not all processes on the machine. Admin user and normal user processes might be different. – Davut Gürbüz Dec 15 '20 at 15:08
jps -lV
is most useful. Prints just pid and qualified main class name:
2472 com.intellij.idea.Main
11111 sun.tools.jps.Jps
9030 play.server.Server
2752 org.jetbrains.idea.maven.server.RemoteMavenServer

- 941
- 6
- 6
-
2That works great in a container with limited packages. For example if `ps` is missing. – Aleksandar Aug 31 '21 at 08:02
-
1Note that usually `-l` is enough. According to the [docs](https://docs.oracle.com/javase/7/docs/technotes/tools/share/jps.html), the `-V` flag outputs _"the arguments passed to the JVM through the flags file (the .hotspotrc file or the file specified by the -XX:Flags=
argument)."_ which won't matter for most use cases. – zb226 Aug 16 '22 at 10:38
Starting from Java 7, the simplest way and less error prone is to simply use the command jcmd
that is part of the JDK such that it will work the same way on all OS.
Example:
> jcmd
5485 sun.tools.jcmd.JCmd
2125 MyProgram
jcmd
allows to send diagnostic command requests to a running Java Virtual Machine (JVM).
More details about how to use jcmd
.
See also the jcmd
Utility

- 43,537
- 11
- 94
- 122
-
1
-
@SridharSarnobat: When it comes to listing java processes as per the question, I can't see a reason why `jcmd` does a better job than `jps`. I think the latter is slightly more flexible. – zb226 Aug 18 '22 at 07:21
-
@zb226 Please note that the OP wants also the command line which you cannot get with `jps` without the right flag. With `jcmd`, you don't need additional flags and/or arguments to get what is expected here. But anyway, what people like the most, is purely opinion based so please don't start a useless debate on that. You are free to prefer `jps` like others are free to prefer `jcmd`. – Nicolas Filotto Aug 18 '22 at 07:41
-
@NicolasFilotto Easy there, I was just interested why he thinks that it's better - maybe I'm overlooking something which is not purely opinion based. A "this is better" comment without any explanation does not help anybody. – zb226 Aug 18 '22 at 08:27
This will return all the running java processes in linux environment. Then you can kill the process using the process ID.
ps -e|grep java

- 4,280
- 8
- 40
- 62
ps aux | grep java
or
$ ps -fea|grep -i java

- 3,209
- 4
- 33
- 44

- 449
- 6
- 14
pgrep -l java
ps -ef | grep java

- 10,864
- 5
- 72
- 96
-
5Thank you for this code snippet, which may provide some immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its educational value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, why doesn't the second of those show the `grep` process? – Toby Speight Aug 01 '17 at 11:33
When I want to know if a certain Java class is getting executed, I use the following command line:
ps ww -f -C java | grep "fully.qualified.name.of.class"
From the OS side view, the process's command name is "java". The "ww" option widens the colum's maximum characters, so it's possible to grep the FQN of the related class.

- 41
- 5
-
This is what I was looking for - `$ top` just gives "java" as the command which isn't all that helpful when trying to figure out which process is hogging the cpu. `$ ps ww -fC java` provides the missing pieces of the puzzle. – David Clarke Jul 16 '19 at 21:59
ps axuwww | grep java | grep -v grep
The above will
- show you all processes with long lines (arg: www)
- filter (grep) only lines what contain the word java, and
- filter out the line "grep java" :)
(btw, this example is not the effective one, but simple to remember) ;)
you can pipe the above to another commands, for example:
ps axuwww | grep java | grep -v grep | sed '.....' | while read something
do
something_another $something
done
etc...

- 62,119
- 17
- 107
- 194
jps & jcmd wasn't showing me any results when I tried it using using openjdk-1.8 on redhat linux. But even if it did it only shows processes under the current user which doesn't work in my case. Using the ps|grep is what I ended up doing but the class path for some java apps can be extremely long which makes results illegible so I used sed to remove it. This is a bit rough still but removes everything except: PID, User, java-class/jar, args.
ps -o pid,user,cmd -C java | sed -e 's/\([0-9]\+ *[^ ]*\) *[^ ]* *\([^$]*\)/\1 \2/' -e 's/-c[^ ]* [^ ]* \|-[^ ]* //g'
Results look something like:
PID USER CMD
11251 userb org.apache.zookeeper.server.quorum.QuorumPeerMain ../config/zookeeper.properties
19574 userb com.intellij.idea.Main
28807 root org.apache.nifi.bootstrap.RunNiFi run
28829 root org.apache.nifi.NiFi
An alternative on windows to list all processes is:
WMIC path win32_process where "Caption='java.exe'" get ProcessId,Commandline
But that is going to need some parsing to make it more legible.

- 791
- 8
- 18
There's a lot of ways of doing this. You can use java.lang.ProcessBuilder
and "pgrep" to get the process id (PID) with something like: pgrep -fl java | awk {'print $1'}
. Or, if you are running under Linux, you can query the /proc
directory.
I know, this seems horrible, and non portable, and even poorly implemented, I agree. But because Java actually runs in a VM, for some absurd reason that I can't really figure out after more then 15 years working the JDK, is why it isn't possible to see things outside the JVM space, it's really ridiculous with you think about it. You can do everything else, even fork
and join
child processes (those were an horrible way of multitasking when the world didn't know about threads or pthreads, what a hell! what's going in on with Java?! :).
This will give an immense discussion I know, but anyways, there's a very good API that I already used in my projects and it's stable enough (it's OSS so you still need to stress test every version you use before really trusting the API): https://github.com/jezhumble/javasysmon
JavaDoc: http://jezhumble.github.io/javasysmon/, search for the class com.jezhumble.javasysmon.OsProcess
, she will do the trick. Hope it helped, best of luck.

- 191
- 3
- 9
ps -eaf | grep [j]ava
It's better since it will only show you the active processes not including this command that also got java string the []
does the trick

- 31,250
- 24
- 137
- 216
-
-
1@vaquarkhan dude just try to run both yours and mine suggestions and see whats the difference, they both will work – Ilya Gazman Jul 31 '17 at 19:49
-
I use this (good on Debian 8):
alias psj='ps --no-headers -ww -C java -o pid,user,start_time,command'

- 1,281
- 2
- 14
- 20
The following commands will return only Java ProcessIDs. These commands are very useful especially whenever you want to feed another process by these return values (java PIDs).
sudo netstat -nlpt | awk '/java/ {print $7}' | tr '/java' ' '
sudo netstat -nlpt | awk '/java/ {print $7}' | sed 's/\/java/ /g'
But if you remove the latest pipe, you will be noticed these are java process
sudo netstat -nlpt | awk '/java/ {print $7}'
sudo netstat -nlpt | awk '/java/ {print $7}'

- 57
- 7