Pure Danger Tech


navigation
home

Hey Java process – who are you?

10 May 2007

It was a dark and stormy night. The kind where the rain can knock you down. But that didn’t matter to me as I was working late at my terminal. Our continuous build server’s emails had mysteriously stopped arriving and I suspected something seedy was going on at the build box.

I clicked warily on Remote Desktop to check it out for myself. The Task Manager showed the CPUs cranking away at suspiciously high levels. There was definitely something strange going on here. I checked the Processes tab and sorted by Image Name. Just my luck – there were 7 java.exe and javaw.exe processes running. It’s a lonely job being the build expert on a Java project.

I was tempted to just unplug the damn thing and bring everything up from scratch. Painful, but I could get back to writing the code that pays the bills. Then I remembered, a new command was added to the JDK in Java 5: jps, which is a Java-aware process listing command.

I popped up a shell and tapped out a quick jps -l command. The -l is always a good idea as it gives you the full class name. If you’re not satisfied with that, you might want to add -v and -m to see the VM and main arguments respectively.

With the extra info, I could see the PID for the process taking all the memory. Looked like a rogue unit test was hosing up the build. Time to take it out back and shoot it.

Sure would have been nice if I could have figured this out without Remote Desktop. A few minutes of interrogation and the docs coughed up the answer. If I start up the jstatd daemon on the build box, I can even use jps remotely.

Of course, starting up jstatd is no picnic. You don’t want just anyone starting up a daemon that provides hooks into your instrumented JVMs after all, do you? If you’re feeling lucky, just create a java security policy file jstatd.all.policy that allows access: [source:java]

grant codebase “file:${java.home}/../lib/tools.jar” {

permission java.security.AllPermission;

};

[/source]

and start jstatd with the policy: [source:java]

jstatd -J-Djava.security.policy=jstatd.all.policy

[/source]

jstatd will start an embedded RMI server and you can now use jps remotely like this to access the box: [source:java]

jps -lvm box

[/source]

With that out of the way, it’s time to do some serious work.