Instead of using
grep, you can use
-C flag to select all commands listed with the name 'java'. You may also want to use
-f flag to print the full command name of each listed process. That way, you can see what each java process is actually doing. Here is the command in full:
ps -fC java.
You could also use
pgrep to list all java processes.
pgrep -a java will return the PID and full command line of each java process.
Once you have the PID of the command you wish to kill, use
kill with the
-9 (SIGKILL) flag and the PID of the java process you wish to kill. Java doesn't always stop when it receives a 'SIGTERM' signal (processes are allowed to handle 'SIGTERM'), so sending it the 'SIGKILL' signal, which makes init kill the program without warning it first, is often necessary.
For example, if
ps -fC java returns
UID PID PPID C STIME TTY TIME CMD
jeff 9014 8890 0 08:51 pts/0 00:00:00 java IDE
jeff 11775 8890 6 08:59 pts/0 00:00:00 java TestProgram
psgrep -a java returns
9014 java IDE
11775 java TestProgram
and you wish to kill
java TestProgram, you should run
kill -9 11775.