Give somekind of signature/identifier to a Java process - java

Is there a way to start my Java process that I can easily identify it, from another program that runs a ps in the system?

You can set a dummy system property using the java command line. This will be easily visible in a ps.
java ..... -Djvm.identifier=xyz

java -Dvisualvm.display.name=wibble MyClass
Then you can find it with:
ps -ef | grep "visualvm.display.name=wibble"
If you use the visualvm.display.name property name then it will use that name when displaying your java processes in jvisualvm.

Related

How to find out default jvm options when you start jar

How can i find out default options of a jvm when i start some jar file ? Except those options, which are specified in a command, like java -jar somefile.jar -XX:MaxPermSize=256m. So, what i need to know is there any other hided options of a jvm which i can find ?
One of the way to do that is to search for a java process by running
ps -ef | grep java
which will show you all the JVM params.
Note: this will work only on Linux and probably MacOS

How to find the process id of a running Java process on Windows? And how to kill the process alone?

I want to kill the particular Java process in Windows, like in Linux (ps -aux to get processid and then kill processid to kill the process).
You can use the jps utility that is included in the JDK to find the process id of a Java process. The output will show you the name of the executable JAR file or the name of the main class.
Then use the Windows task manager to terminate the process. If you want to do it on the command line, use
TASKKILL /PID %PID%
You can also find the PID of a java program with the task manager. You enable the PID and Command Line columns View -> Select Columns and are then able to find the right process.
Your result will be something like this :
This will work even when there are multiple instance of jar is running
wmic Path win32_process Where "CommandLine Like '%yourname.jar%'" Call Terminate
After setting the path of your jdk use JPS.Then You can eaisly kill it by Task ManagerJPS will give you all java processes
The solution I found is very simple. Use Window's WMIC & Java's Runtime to locate & kill the process.
Part 1: You need to put some sort of identifier into your app's startup command line. E.g. something like:
String id = "com.domain.app";
Part 2: When you run your app, make sure to include the string. Let's say you start it from within Java, do the following:
Runtime.getRuntime().exec(
"C:\...\javaw.exe -cp ... -Dwhatever=" + id + " com.domain.app.Main"
);
Part 3: To kill the process, use Window's WMIC. Just make sure you app was started containing your id from above:
Runtime.getRuntime().exec(
"wmic process Where \"CommandLine Like '%" + id + "%'\" Call Terminate"
);
In windows, we can use the PowerShell to list the java running process. Then using the process id we can kill the process. Please find the below commands that needs to be executed in the PowerShell.
To list the Java Process.
ps | Where-Object -Property ProcessName -EQ -Value 'Java'
To kill the java process with specific id.
Stop-Process <PID>
The above approach worked for me.
In windows XP and later, there's a command: tasklist that lists all process id's.
For killing a process in Windows, see:
Really killing a process in Windows | Stack Overflow
You can execute OS-commands in Java by:
Runtime.getRuntime().exec("your command here");
If you need to handle the output of a command, see example: using Runtime.exec() in Java
This is specific to Windows.
I was facing the same issue where I have to kill one specific java program using taskkill. When I run the java program, tasklist was showing the same program with Image name set as java.exe.
But killing it using taskkill /F java.exe will stop all other java applications other than intended one which is not required.
So I run the same java program using:
start "MyProgramName" java java-program..
Here start command will open a new window and run the java program with window's title set to MyProgramName.
Now to kill this java-program use the following taskkill command:
taskkill /fi "MyProgramName"
Your Java program will be killed only. Rest will be unaffected.
Open Git Bash
Type ps -ef | grep java
Find the pid of running jdk
kill -9 [pid]

How to know what class is running in Unix?

I have a class that is being executed through a script in unix (Solaris 9). Inside of the script the class is being run like this:
java -cp $CLASSPATH myPackage.component.MyMainClass $PROPERTIES_PATH/myMainClassProperties.properties
That shell needs to be monitored in order to see whether it works correctly or it fails, and it also is going to be run parallel with a different parameter file. So, my questions are:
How can I know what class is being executed if I use top. Is that possible?**
Is there a way to uniquely identify the class so it doesn't crash when running parallel?
Will it always be shown as myPackage.component.MyMainClass in the table of processes?
When I say parallely, I refer to something like:
java -cp $CLASSPATH myPackage.component.MyMainClass $PROPERTIES_PATH/myMainClassProperties.properties
and in another window/session/job
java -cp $CLASSPATH myPackage.component.MyMainClass $PROPERTIES_PATH/mySomeOtherProperties.properties
**When I say so, is because top shows something like this:
PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
8545 batman 47 4 10 190M 112M sleep 0:04:00 0.07% java
9022 joker 91 4 10 286M 211M sleep 0:01:00 0.09% java
You can let top tell you the complete command, that will include any arguments you pass to the VM.
Also ps will give you the complete command as well (with the matching arguments).
In my debian I can switch the displayed command with the c toggle (start top and hit c until it shows the whole command)
for ps I use the arguments -ef (but -f) alone should do for that situation.
You may want to read the output of man ps and man top
It was possible to get the info using ptree | grep java or ptree | grep MyMainClass
It lists a tree of the processes that are being executed as well as the command that is being runned in there for example:
13456 ksh
123476 java -cp java -cp /classpath/fullpath myPackage.component.MyMainClass /full/properties/path/myMainClassProperties.properties
The first line is a shell script, and inside it (next line) calls the java jar. This works for Solaris 9.
Thanks #Angelo-Neuschitzer for the heads up.

Getting execution time of a user process in Linux

How can I get the details of a process in Linux. I want the total execution time, memory map of a process. In this context the process will be a simple Java program. I only know the file name, not the process id. Any help will be appreciated
If the process is not already running, then time will print total execution time. There is both a bash builtin and a standalone command of that name, with somewhat different behaviour. But as your mention of memory maps suggests an already running process, I'll concentrate on that.
You can find a process by executable using ps -C java. If you want to know what that java binary is running, you can do things like this to find the pid:
ps -C java -o pid,time,cmd | awk '/foo\.jar/{print $1,$2}' | \
while read pid time; do
echo "PID $pid, TIME $time"
cat /proc/$pid/maps
echo ""
done
If you don't like the output format, replace the loop with something else.
Here is what this does:
-C java: Find java processes
-o pid,time,cmd: Print these fields
/foo\.jar/: Only take lines containing that regular expression
print $1,$2: Print pid and time columns
/proc/$pid/maps: Access proc file conatining the memory map
If you know only the program name, and not the process id, you first have to find out which processes (they can be many of them) are running that program.
You could use e.g. some of the below commands to find out, assuming the program name is progname:
ps auxw | grep progname
pidof progname
pgrep progname
(notice that if you are using a JVM to run some Java program the progname is always java which does not help much)
You might also use top or htop
Once you got an interesting pid, e.g. 1234, you could get more information about that process with e.g.:
ps -lw 1234
cat /proc/1234/status
cat /proc/1234/stat
cat /proc/1234/stack
cat /proc/1234/maps
cat /proc/1234/smaps
Look also in other files and directories of /proc/1234/ such as /proc/1234/fd/ and others.
The Linux kernel is telling information about processes thru /proc so you should learn more about it.
I'm planning to use some memory profiling tools such as valgrid.

How to set Process Name for my own application

I have created a simple java networking program. I am using Fedora. whenever I want to see what the processes run on my system I found that for my application the process Name is java. I want give process name for my application. How to give process name.
Thanks
Sunil Kumar Sahoo
One way to change the process name of an application is to use a native launcher (or to copy the java/java.exe executable to another name).
Personally I've had good results with Launch4j
You could pass a java property to the jvm when you start the process then that should show up when running a ps -eaf and you could even do a ps -eaf|grep myprop to see if it's running.
so you start the app like this:
java -cp . com.whatever.MyApp -DMyAmazingProgram=true
then you should see the MyAmazingProgram=true in the ps output.
Another way would be to start your app from a bash script file e.g, startMyAmazingApp.sh then that should show up in the ps output until the process ends.
That script would have to not exit until the java process finished so you'd need to have a script a bit like this (rough guess):
#!/bin/bash
RESULT=`java -cp com.whatever.MyApp`
HTH

Categories

Resources