Jan 12

Tomcat shutdown.sh leaves server running

Posted by James Netherton | Tuesday 12 January 2010 19:21 PM | In Java

If you’ve been using Tomcat’s shutdown.sh script and find that Tomcat hasn’t stopped, it’s probably because there are some non daemon threads still running. The JVM will not shut down until there are no active non daemon threads.

Try the following to diagnose and resolve the problem….

Find Tomcat’s process id:

ps -ef | grep tomcat

Now kill the Tomcat process using the -3 flag (SIGQUIT signal) to force a thread dump:

kill -3 tomcat process id number goes here

Once the process has stopped take a look at the tomcat log file. You should see some thread dumps which will help to identify the application and code that is hanging on to threads.

Jan 02

Ubuntu Ant FTP task NoClassDefFoundError

Posted by James Netherton | Saturday 02 January 2010 13:10 PM | In Java

I came across a problem when trying to use the Ant FTP task on Ubuntu. Whenever I tried executing my build file I kept getting:

java.lang.ClassNotFoundException: org.apache.commons.net.ftp.FTPClientConfig

The simple fix is to make sure Ant can find the Apache commons-net Jar, either from under the Ant lib directory or in a custom location.

I installed Ant using Aptitude which places all of the shared Ant jars in /usr/share/ant/lib. I was confused because there was a symbolic link to ant-commons-net.jar which I thought should contain the missing classes. Turns out this Jar is actually the Ant FTP task library, you still need to download the commons-net Jar file.

Aug 17

Connecting JConsole To Eclipse

Posted by James Netherton | Monday 17 August 2009 19:43 PM | In Java

Ever wanted to connect JConsole to an application that has been launched by Eclipse???

It’s quite simple to do. Just ensure you pass the following JVM argument when launching the application…..

-Dcom.sun.management.jmxremote

Start JConsole and you should get the option of connecting the the application you launched from Eclipse.

May 02

Installing Firefox Java Plugin on 64 Bit Ubuntu

Posted by James Netherton | Saturday 02 May 2009 21:40 PM | In Java, Testing

Finally! – I’ve got Java working in Firefox again after upgrading to Ubuntu X64. Lots of guides I found mention installing the 32Bit version of Firefox and the Java runtime. There’s no need for any of this. Just follow the simple guide here:

http://yokohead.com/2009/02/installing-java-x64-in-ubuntu-810/

Apr 22

Debugging a Java Web Start application from Eclipse

Posted by James Netherton | Wednesday 22 April 2009 17:52 PM | In Java

I just spent some time debugging a weird problem that was only occurring when an application was launched via Java Web Start. Something I found useful was being able to attach the Eclipse Java debugger to the application process. Here’s how to set this up.

From the command line execute (In windows use set instead of export):

export JAVAWS_VM_ARGS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y"

In Eclipse, configure a new debug configuration by choosing menu options Run -> Debug Configurations. Find the ‘Remote Java Application’ section, right click and choose ‘New’.

Change the port number to whatever you specified when setting up the JAVAWS_VM_ARGS environment variable. Click the apply button and close the window.

Now start your application:

javaws http://hostname:port/yourjnlpfile.jnlp

You can now attach the Eclipse debugger to the process by running the debug configuration you just set up. Set some breakpoints in your code and you should find the application suspends at the relevant points where you can debug the application as per normal.

Mar 25

Installing Java 1.5 on Mac OS X Panther

Posted by James Netherton | Wednesday 25 March 2009 21:49 PM | In Java, Mac

There is no official support for running / installing Java versions greater than 1.4 on Mac OS X Panther. Here’s how I installed and configured my old Mac Mini G4 OS X Panther machine to run Java 1.5.

1. Download and install Pacifist

2. Download Java 1.5 from the Apple website and run the .dmg file

3. Start Pacifist. Drag the .pkg install file from the mounted Java .dmg file. Select the root of the package hierarchy and click the install button

4. When prompted to overwrite existing files choose ‘Leave Alone’

5. When the installation process has completed you can now set the default Java VM to 1.5. Open a terminal session and run the following:

cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo ln -fhsv 1.5 CurrentJDK

Confirm that everything has worked by executing java -version from the command line.

Nov 04

Make your XML look pretty

Posted by James Netherton | Tuesday 04 November 2008 11:11 AM | In Java

Credit goes to Joe Rinehart for most of this from this post.

ColdFusion ships with JDom which gives you full access to the JDom API. Many of the applications I work with have some kind of XML component to them and sometimes the XML is difficult to read if it’s not formatted nicely.

I’ve used Joe’s code example to create a simple interface on top of it. It also works with valid XHTML.

<cfparam name="form.submit" default=""/>
<cfparam name="form.xml" default=""/>
<cfscript>
function prettyXML(xmlString){

	var fileObj = "";
	var builder = "";
	var format = "";
	var out = "";
	var document = "";
	var fileInStream = "";
	var fileOutStream = "";
	var filePath = expandPath("./prettyXML.xml");

	try{
		fileWrite(filePath, arguments.xmlString);

		fileObj = createObject("java", "java.io.File").init(filePath) ;
		builder = createObject("java", "org.jdom.input.SAXBuilder").init() ;
		format = createObject("java", "org.jdom.output.Format").getPrettyFormat() ;

		format.setIndent("	");
		out = createObject("java", "org.jdom.output.XMLOutputter").init(format);
		fileInStream = createObject("java", "java.io.FileInputStream").init(fileObj );

		document = builder.build(fileInStream);

		fileInStream.close();

		fileDelete(filePath);

		return out.outputString(document);
	}
	catch(Any excpt){
		if(isDefined("fileInStream") and fileInStream neq ""){
			fileInStream.close();
		}

		writeOutput("<p>#excpt.message#</p>");
		writeOutput("<p>#excpt.stacktrace#</p>");
	}
}
</cfscript>
<html>
<head>
<title>Paste your ugly XML</title>
<style type="text/css">
	body, input{
		font-family:Trebuchet MS;
	}
</style>
</head>
<body>
<h3>Input your XML:</h3>
<cfoutput>
<form action="#cgi.SCRIPT_NAME#" method="post">
<p><textarea name="xml" rows="20" cols="100">#form.xml#</textarea></p>
<input name="submit" type="submit" value="Go"/>
</form>
<h3>Pretty XML</h3>
<cfif len(form.submit) and len(form.xml)>#htmlCodeFormat(prettyXML(form.xml))#</cfif>
</cfoutput>
</body>
</html>

May 18

Eclipse Memory Manager Plugin

Posted by James Netherton | Sunday 18 May 2008 7:05 AM | In Java

I thought I’d mention this as it ties in quite nicely with my previous post.

Currently in incubation phase, Eclipse has a memory manager plugin which can analyse and provide some nice graphs and statistics from Java heap dumps.

The plugin can be downloaded here. There is an additional dependency on the BIRT charting and reporting plugin. Instructions for installing this are here.

Once everything is installed you can begin heap analysis. Use the jmap utility to create a heap dump file from a running JVM instance. You’ll need the JVM process ID in order to do this.

Open up a command prompt or terminal session and execute the command below. Be warned that the size of the file produced can be quite large (over 100Mb).

jmap -dump:format=b,file=heapdump.hprof <process id>

From eclipse you can use the memory manager to open up and analyse the dump file.

Java 1.6 includes a simpler heap analysis tool named jhat. To analyse the heap dump we obtained from jmap, execute the following command:

jhat heapdump.hprof

Now point your web browser at http://localhost:7000 to see view the heap analysis results.

All of this may be useful if you’re experiencing the Java OutofMemory exception. You can set the JVM parameter -XX:+HeapDumpOnOutOfMemoryError to dump the heap on out of memory exceptions.

May 16

JVM Monitoring Tools For ColdFusion

Posted by James Netherton | Friday 16 May 2008 6:05 AM | In Java

I’ve been experimenting with JVM monitoring tools out of curiosity for what impact some applications have on memory and CPU usage. Here is a brief rundown on some of the tools that I used. All of these tools should be included within the bin directory of whatever JDK distribution you are using, providing the Java version is 1.5 or greater.

You may read this post and wonder why I bothered to research any of this, seeing as ColdFusion now has built in monitoring and profiling tools, together with many third party alternatives. Those tools are all good at providing a relatively high level of detail, showing what’s going on inside of the application server but the tools outlined below can offer a more granular level of detail.

jps

Some of the tools require an argument to specify the id of the JVM that you want to interrogate, jps can be used to retrieve this id:

jps localhost

358 Jps
268

Other tools require that you pass an argument for the JVM process ID. This information can be easily obtained by using Task Manager, Activity Monitor, top, ps or whatever your tool of choice is for your environment.

JConsole

JConsole is a graphical tool that displays the current state of the JVM in terms of memory usage, thread usage and classes loaded. There are a couple of other items such as MBean management, but that’s outside the scope of this post.

To start JConsole you need to the process ID of the JVM. I’m going to use the PID of JRun (2219) which of course is my ColdFusion JVM instance:

jconsole 2219

You’ll then see graphs representing the current JVM memory usage. Execute some CF pages or fire up an application and you should see the memory graphs rise and fall as memory is allocated and garbage is collected. You can even force garbage collection by clicking the ‘Perform GC’ button.

jstack

jstack produces a stack trace dump of all threads within the JVM.

jstack 2219

Note that the output is very verbose so it’s probably better to pipe the output to a file.

jmap

jmap can be quite useful to gather details about JVM memory details. For example, you can view heap statistics relating to number of objects loaded and the amount of memory allocated. Again, it’s best to pipe the output to a text file:

jmap -histo 2219 > heap_stats.txt

jstat

jstat displays performance statistics for a JVM instance. The tool requires the id of the target JVM to be passed as a command line argument. You can also set the data refresh rate by specifying a value in milliseconds. The example below outputs a summary of garbage collection statistics every 5 seconds:


jstat -gcutil 1700 5000

There a many different switches that can be used to obtain different monitoring statistics. The Sun jstat web page has an overview of these options.

Some other tools that I looked at:

visualgc – similar in some ways to JConsole but focuses on memory and garbage collection statistics.

profiler4j – I couldn’t get this working with JRun.

Eclipse Profiler – I got the Eclipse plugin installed ok but JRun choked whenever I tried to start ColdFusion after adding the specified profiler arguments into the jvm.config file. I only tried this with CF 8 so it may work with CF 7 and Java 1.4.2.

JRockit Mission Control – A product from BEA. It has been used to good effect in the past to find memory leaks in ColdFusion:

Mike Schierberl – ColdFusion Memory Leaks

Jason Sheedy – Cold Fusion Memory Leak

I got bored of trying to hunt down a free version of JRockit on the BEA website, but this tool can potentially offer some nice insights into what’s happening under the hood of an application.