- List of Java processes
- 18 Answers 18
- Find all the Java processes running on your machine
- Troubleshooting a Java Application with jps
- jps command
- Java program we will be debugging in this post
- Print Java process ids
- Print process id along with the class name
- Print Java process ids along with full package name
- Print process ids along with class name and arguments passed to the main method.
- Print JVM arguments passed to Java process
- jcmd to list Java processes
- Conclusion
List of Java processes
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.
please 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.
18 Answers 18
Recent Java comes with Java Virtual Machine Process Status Tool «jps»
[nsushkin@fulton support]$ jps -m 2120 Main --userdir /home/nsushkin/.netbeans/7.0 --branding nb 26546 charles.jar 17600 Jps -m
Just a note: jps only ships with the JDK, not the JRE. Machines with plain ol’ Java runtimes on them won’t have this tool.
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.
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
Note that usually -l is enough. According to the docs, 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.
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.
> jcmd 5485 sun.tools.jcmd.JCmd 2125 MyProgram
jcmd allows to send diagnostic command requests to a running Java Virtual Machine (JVM).
@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 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 .
@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.
You can use single command pgrep as well (doesn’t require you to use pipes and multiple commands):
For better output format check this command:
This will return all the running java processes in linux environment. Then you can kill the process using the process ID.
pgrep -l java ps -ef | grep java
Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve 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?
If I want simply list java processes, use:
ps axuwww | grep java | grep -v grep
- 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
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.
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.
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/\(5\+ *[^ ]*\) *[^ ]* *\([^$]*\)/\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.
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 . 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?! :).
Find all the Java processes running on your machine
When your application has some problem, the first thing to check is running processes on the machine. For Linux OS we generally use ps -ef . ps is one of the most used Linux troubleshooting commands. JDK provides similar functionality for Java processes through jps . The jps command-line utility provides a list of all running Java processes on a machine for which the user has access rights. The access rights are determined by access-control mechanisms specific to the operating system. jps utility can also provide information on arguments passed to the main method, arguments passed to JVM, etc. In this post, we will see the functionalities provided by jps .
Troubleshooting a Java Application with jps
In this section, we will see how to use jps with a running Java process.
jps command
jps [options] pid options: This represents the jps command-line options. pid: The process ID for which the information specified by the options is to be printed.
Java program we will be debugging in this post
Following is the sample class we are going to debug and try to understand the different features available.
public class Test public static void main(String[] args) while(true) > > >
For all our examples we will be using Java 17, as of writing this post it is built using JDK master branch. This post explains how to build JDK from the source.
java -version openjdk version "17-internal" 2021-09-14 OpenJDK Runtime Environment (build 17-internal+0-adhoc.vipin.jdk) OpenJDK 64-Bit Server VM (build 17-internal+0-adhoc.vipin.jdk, mixed mode)
We are running the Java process using the following command. For rest of the blog post we will use jps on this process.
java -XX:ConcGCThreads=6 -Xmx256m -Xms8m -Xss256k Test argument1 argument2
Print Java process ids
Following command shows process ids.
2468 10660 7067 7470 10366
Print process id along with the class name
This is the command to list Java processes with main class names, it is same as command jps -V .
2468 10694 Jps 7067 Main 7470 Launcher 10366 Test
Print Java process ids along with full package name
jps -l displays the full package name for the application’s main class or the full pathname to the application’s JAR file.
2468 10554 jdk.jcmd/sun.tools.jps.Jps 7067 com.intellij.idea.Main 7470 org.jetbrains.jps.cmdline.Launcher 10366 Test
Print process ids along with class name and arguments passed to the main method.
jps -m displays the arguments passed to the main method
2468 10726 Jps -m 7067 Main 7470 Launcher /home/vipin/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57/lib/commons-lang3-3.10.jar:/home/vipin/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57/lib/httpclient-4.5.12.jar:/home/vipin/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57/lib/annotations.jar:/home/vipin/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57/lib/netty-buffer-4.1.52.Final.jar:/home/vipin/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57/plugins/java/lib/jps-javac-extension-1.jar:/home/vipin/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57/lib/jdom.jar:/home/vipin/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57/lib/netty-resolver-4.1.52.Final.jar:/home/vipin/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57/lib/maven-resolver-api-1.3.3.jar:/home/vipin/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57/plugins/java/lib/maven-resolver-connector-basic-1.3.3.jar:/home/vipin/.local/share/JetBrains/Toolbox/apps/IDE 10366 Test argument1 argument2
Print JVM arguments passed to Java process
jps -v displays the arguments passed to the JVM.
2468 -Djava.library.path=/tmp/.mount_jetbraH5N0hQ -Xmx256m -Xms8m -Xss256k -XX:+UseStringDeduplication -XX:+UseCompressedOops -XX:+UseSerialGC -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Djdk.lang.processReaperUseDefaultStackSize=true vfprintf exit abort -DTOOLBOX_VERSION=1.20.7940 10501 Jps -Dapplication.home=/home/vipin/githubprojects/jdk/build/linux-x86_64-server-release/jdk -Xms8m -Djdk.module.main=jdk.jcmd 7067 Main -Xms128m -Xmx2048m -XX:ReservedCodeCacheSize=512m -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=50 -XX:CICompilerCount=2 -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -ea -Dsun.io.useCanonCaches=false -Djdk.http.auth.tunneling.disabledSchemes="" -Djdk.attach.allowAttachSelf=true -Djdk.module.illegalAccess.silent=true -Dkotlinx.coroutines.debug=off -Dsun.tools.attach.tmp.only=true -Dide.no.platform.update=true -XX:ErrorFile=/home/vipin/java_error_in_idea_%p.log -XX:HeapDumpPath=/home/vipin/java_error_in_idea_.hprof -Didea.vendor.name=JetBrains -Didea.paths.selector=IdeaIC2020.3 -Djb.vmOptionsFile=/home/vipin/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57.vmoptions -Didea.platform.prefix=Idea -Didea.jre.check=true 7470 Launcher -Xmx700m -Djava.awt.headless=true -Djdt.compiler.useSingleThread=true -Dpreload.project.path=/home/vipin/githubprojects/jdk -Dpreload.config.path=/home/vipin/.config/JetBrains/IdeaIC2020.3/options -Dcompile.parallel=false -Drebuild.on.dependency.change=true -Dio.netty.initialSeedUniquifier=4046065713679813272 -Dfile.encoding=UTF-8 -Duser.language=en -Duser.country=IN -Didea.paths.selector=IdeaIC2020.3 -Didea.home.path=/home/vipin/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57 -Didea.config.path=/home/vipin/.config/JetBrains/IdeaIC2020.3 -Didea.plugins.path=/home/vipin/.local/share/JetBrains/IdeaIC2020.3 -Djps.log.dir=/home/vipin/.cache/JetBrains/IdeaIC2020.3/log/build-log -Djps.fallback.jdk.home=/home/vipin/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/203.7148.57/jbr -Djps.fallback.jdk.version=11.0.9.1 -Dio.netty.noUnsafe=true -Djava.io.tmpdir=/home/vipin/.cache/JetBrains/IdeaIC2020.3/compile-server/jdk_5c2ba8e3/_temp_ -Djps.backward.ref.index.builder=true -Dkotlin.incremental.compilation=true 10366 Test -XX:ConcGCThreads=6 -Xmx256m -Xms8m -Xss256k
jcmd to list Java processes
jcmd or jcmd -l both provides similar information as jps -l . You can read more about jcmd on post.
Conclusion
jps is a simple tool with few options that make it easy to master, and when in need it can be a quick and great help you wanted. Utilities like this are very useful in a situation when we need to analyze and resolve problems in production Java application quickly. jps is part of OpenJDK, no need to install any third party software.
If you want to get amazing Java jobs, I wrote an ebook 5 steps to Best Java Jobs. You can download this step-by-step guide for free!