How to know what program a java process is running [duplicate] - java

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
When a java based application starts to misbehave on a windows machine, you want to be able to kill the process in the task manager if you can't quit the application normally. Most of the time, there's more than one java based application running on my machine. Is there a better way than just randomly killing java.exe processes in hope that you'll hit the correct application eventually?
EDIT: Thank you to all the people who pointed me to Sysinternal's Process Explorer - Exactly what I'm looking for!

Run jps -lv which shows PIDs and command lines of all running Java processes.
Determine PID of the task you want to kill. Then use command:
taskkill /PID <pid>
to kill the misbehaving process.

Download Sysinternal's Process Explorer. It's a task manager much more powerfull than Windows's own manager.
One of it's features is that you can see all the resources that each process is using (like registry keys, hard disk directories, named pipes, etc). So, browsing the resources that each java.exe process holds might help you determine wich one you want to kill. I usually find out by looking for the one that's using a certain log file directory.

If you can't run a GUI application like ProcessExplorer and you're looking for the "Command Line" arguments of the processes then you can use "wmic" via the command line. For example:
wmic PROCESS get Processid,Caption,Commandline
If you want to look for a specific process you can do this:
wmic PROCESS where "name like '%java%'" get Processid,Caption,Commandline
The output from this will show you all of the command line arguments of processes like "java."

Using jps in the JDK will give you more information. More information is display with the -m, -l and -v options.

Have you tried using Process Explorer from SysInternals? It gives a much better idea of what is running within the process. Available free online here: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

If you're using Java 6, try jvisualvm from the JDK bin directory.

You could try opening Windows Task Manager, going to the Applications tab, right clicking the application and then selecting "Go To Process". This will automatically highlight the appropriate process in the Processes tab.

In case you're developing software: use a java-launcher. I used for a few of my application [Exe4j][http://www.ej-technologies.com/products/exe4j/overview.html] and it worked very well. When the application is started, it's listed as for example "myserverapp.exe" or "myapp" in the windows tasks manager.
There are other lauchers too (don't known them by heart), few of them might be for free too.

I'd suggest downloading Process Explorer from Sysinternals and looking at the different java.exe processes more closesly, that way you can get a better idea of which one to kill.
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
It's very intuitive and you can find the java.exe processes and right click and goto their properties, from there you can see their command line, time of creation, etc which can help you find the process you want to kill.
Hope it helps.

Using ProcessExplorer and hovering over the Java process will show the command line.

If the application is not responding at all, then Process Explorer is a good option.
If it's sort of responding, but not dying, sometimes bringing up task manager, and then moving another dialog over the java process will give you a clue. The java process that's taking up cpu cycles to redraw is the one you're looking for.

Rather than using a third party tool, you can also make a pretty good guess by looking at all the columns in task manager if you know roughly what the various java processes on your system are. From the Processes tab, use View-> Select Columns and add PID, CPU Time, VM Size, and Thread count. Knowing roughly what the process is doing should help narrow it down.
For example, in a client-server app, the server will likely use more memory, have more threads, and have used more CPU time because it has been running longer. If you're killing a process because it's stuck, it might simply be using more CPU right now.
MAX java heap memory is usually directly reflected in VM Size. So if you're using -Xmx flags, the process with the larger setting will have a larger VM Size.

Related

Is it better to launch a Java app once and sleep or repeat launching and killing?

I have a Java application that needs to run several times. Every time it runs, it checks if there's data to process and if so, it processes the data.
I'm trying to figure out what's the best approach (performance, resource consumption, etc.) to do this:
1.- Launch it once, and if there's nothing to process make it sleep (All Java).
2.- Using a bash script to launch the Java app, and when it finishes, sleep (the script) and then relaunch the java app.
I was wondering if it is best to keep the Java app alive (sleeping) or relaunching every time.
It's hard to answer your question without the specific context. On the face of it, your questions sounds like it could be a premature optimization.
Generally, I suggest you do what's easier for you to do (and to maintain), unless you have good reasons not to. Here are some possible good reasons, pick the ones appropriate to your situation:
For sleeping in Java:
The check of whether there's new data is easier in Java
Starting the Java program takes time or other resources, for example if on startup, your program needs to load a bunch of data
Starting the Java process from bash is complex for some reason - maybe it requires you to fiddle with a bunch of environment variables, files or something else.
For re-launching the Java program from bash:
The check of whether there's new data is easier in bash
Getting the Java process to sleep is complex - maybe your Java process is a complex multi-threaded beast, and stopping, and then re-starting the various threads is complicated.
You need the memory in between Java jobs - killing the Java process entirely would free all of its memory.
I would not keep it alive.
Instead of it you can use some Job which runs at defined intervals you can use jenkins or you can use Windows scheduler and configure it to run every 5 minutes (as you wish).
Run a batch file with Windows task scheduler
And from your batch file you can do following:
javac JavaFileName.java // To Compile
java JavaFileName // to execute file
See here how to execute java file from cmd :
How do I run a Java program from the command line on Windows?
I personally would determine it, by the place where the application is working.
if it would be my personal computer, I would use second option with bash script (as resources on my local machine might change a lot, due to extensive use of some other programs and it can happen that at some point I might be running out of memory for example)
if it goes to cloud (amazon, google, whatever) I know exactly what kind of processes are running there (it should not change so dynamically comparing to my local PC) and long running java with some scheduler would be fine for me

How to automatically kill orphaned Java processes

I've posted this question before, but didn't get the answer I wanted. The problem I have right now is that there are a number of Java processes getting orphaned. This is both on Linux and Windows. I need a way to FIND which Java processes are the ones that are orphaned and kill them.
NOTE: I CANNOT make changes to the Java code as I have no access to it on any level. I am simply running some tests on my machine. I am aware of solutions like this one
Killing a process using Java
but that is not what I am looking for.
On Linux an orphaned process becomes the child of init, which always has pid 1. To kill java processes that are children of init you can use pkill:
pkill --parent 1 java
To make this automatic you can add this command to cron, for example.

How to check whether a Javaprocess is still running or not?

I am working on a plugin for the Serversoftware Bukkit which should restart it. So I have added the main Function which checks for the right arguments and then starts Bukkit. But before it starts Bukkit it should wait for Bukkit to shutdown before I start it again. I know I could add a timeout but I hope there is a better way. So does anybody know how to do that?
One way would be to check the os with System.getProperty("os.name"); and run the ad hoc command for that specific platform (for example: Linux and Windows).
When you start multiple java processes they all seem to have the same identifiers, which seems to be your problem. "differentiating" the processes is your problem, not killing them.
So either you could create the process yourself programatically, and thus be able to note the PID that is used for the process. Then killing it would be VERY simple:
http://blog.igorminar.com/2007/03/how-java-application-can-discover-its.html
Another idea would be to use JPS to get information on all the java processes. A simple sudo algorithm is discussed here:
http://bluepedia.blogspot.com/2008/11/jps-differentiate-between-multiple-java.html
Hope I could be of some help..

How To Start Jetty Properly

this really silly question probably, as no one else seems to be having this problem. In the Jetty documentation it says jar -jar start.jar starts Jetty, and it does. But when I close my SSH console, obviously it dies.
How do I run it PROPERLY?
Is this for running on a production machine that will actually serve up an application running under Jetty? I assume this is the case, since you're asking about starting it properly.
If so, you need a proper process supervision system, such as runit, daemontools, monit, upstart, systemd, or good ol' SysV init.d (as mentioned w/ a gist). Which to use depends on your preferences, business needs, and often, your underlying operating system.
I use and prefer runit. It is built on solid principles (daemontools), and for my preferred distribution (Debian and Ubuntu) it is nicely packaged by the author himself.
Despite being recommended in other answers, and mentioned in comments, starting a long running process in screen/tmux, or via nohup is sub-optimal. You don't have any real control over the process. It won't be restarted if it dies. You have to manually find its PID and otherwise manually manage the service. You have to do more manual work to get the log output (redirection, sending to some random file, etc). You cannot reliably make it depend on other processes, or have other processes depend on it. Decent process supervision systems provide all this functionality for you by default.
If your goal is something else entirely, then please update the question to be more specific about your use case.
java -jar start.jar &
(to run in the background) should also work, though logging won't be transmuted as nice as w/nohup.
This is because killing the shell that started a process (e.g. by logging out) will kill process to unless they're background processes. Screen works since as well since it runs in the background, and screen effectively keeps your session running while you attach/detach.
One way is to use nohup
nohup java -jar start.jar
This has the advantage of writing stdout and stderr to a file
Another way would be to use screen
If you're on a *nix system, the best solution is may be using a script in /etc/init.d (or whatever your system's equivalent is). There's one at https://gist.github.com/404672.
Otherwise, using nohup or screen from the command-line will at least have the process not die when you log out. So will putting the process in the background with &.

How can I give my Java application a unique process name?

I've noticed that when I start Netbeans it shows up in the task manager as netbeans.exe as all my own Java applications show up as java.exe or javaw.exe.
How can I change that so my process names shows up as myapp.exe?
The process name is the name of the JVM. So if you rename the jvm you have an other process name. There are some tools which can do that for you. For example Launch4J
IMO the best option is to choose one of the many open source launchers. They all provide a nicer deployment container than java.exe and a batch file.
I've compiled a list of them after a brief search on google (in no particular order and may not be exhaustive):
NSIS
Janel (dead link)
JSmooth
Launch4J
WinRun4J
(full disclosure: i work on winrun4j)
Not easily. The easiest way (but not nice!) would be to simply copy the java.exe (only 68k on my system, so perhaps practical!)
If you're worried about identifying which java process is which (e.g. is one consuming memory/CPU etc.), use the standard tool jps to identify the Java processes
Netbeans and Eclipse both ship with an .exe file that in turns launches a JVM. The exe itaself probably does nothing after launching the VM. You see the NetBeans javaw.exe in the Task Manager also, I suspect.
So you'll need to write a native exe (using some windows tool) that does a similar thing.
Just answered this a second ago here: Get JVM to grow memory demand as needed up to size of VM limit?
It's actually a lot easier than folks are saying (but you do have to have a c/c++ compiler handy).
There are mainly 2 approaches: one is as already described: using tools like Launch4j, WinRun4J to create native Windows launchers.
Another approach that seems better is to use Apache Procrun to wrap the java application as a Windows service. During the install service process, we can give the process an meaningful name such as OurApp.exe.
All we need do is rename prunsrv.exe to OurApp.exe and replace every occurrence of prunsrv.exe in our install|start|stop|uninstall service scripts to MyApp.exe.
See more from Using Apache Procrun to Rename Process Name of a Java Program in Windows

Categories

Resources