« Pulse on PostgreSQL | Main | Bug Reporting 101 »

Monitoring Java Processes under Mac OS X

Listen to this articleListen to this article

Update: After a bit of googling I discovered that it's a bug which is fixed in Java 6 (Mustang) and relates to, wait for it, user names containing an underscore. Go figure!

So today, after I don't know how many years of doing Java development, I find out there is a command to list all the running Java processes. It's available on all platforms I could find and it's called jps. On most platforms it'll display something like:

1170 Jps -lm
1162 org.apache.tools.ant.launch.Launcher -cp  dist

Except under Mac OS X where you'll get something more like:

1170 Jps -lm
1162 -- process information unavailable

Pretty bloody useless!

So, I wrote a little ruby script to simulate the desired behaviour as much as possible (by using the built-in jps to collect the process ids; then filtering the output from ps accordingly):

#!/usr/bin/env ruby

require 'set'

pids = Set.new

`#{ENV['JAVA_HOME']}/bin/jps`.each_line do |line|
  pids.add($1) if line =~ /^(\d+) /
end

`ps -x -w -w -o pid,command`.each_line do |line|
  print line if line =~ /^ *(\d+) / && pids.include?($1)
end

Which, when run, produces output similar to (line continuation not included):

1162 /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/java \
  -classpath /opt/local/share/java/apache-ant/lib/ant-launcher.jar \
  -Dant.home=/opt/local/share/java/apache-ant \
  -Dant.library.dir=/opt/local/share/java/apache-ant/lib \
  org.apache.tools.ant.launch.Launcher -cp  dist

Granted it does produce lots more output than standard jps but it does at least produce something useful to look at when monitoring Java processes. Best of all, it means we can automate the monitoring in a manner that works across most platforms.

If anyone has a Better Way™, I'm all ears.

Comments

On my mac jps works perfectly (macosx 10.4.8, java version "1.5.0_07")

Really? I'm running what appears to be the same as you: 10.4.8 with java build 1.5.0_07-87. No matter how I run jps I always get process information unavailable. Even reading the documentation on the apple website suggests that only the process id is available.

Post a comment