Finding Java System Properties

// File: io/properties/SysPropList.java
// Description: Shows system properties.  This must be an application.
//              An applet can't get this information.
// Author: Fred Swartz
// Date:   2 Feb 2005

import java.awt.*;
import javax.swing.*;
import java.util.*;

/** Generic main program. */
public class SysPropList {
    public static void main(String[] args) {
        JFrame window = new JFrame("System Properties");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setContentPane(new SysPropListGUI());
        window.pack();
        window.setVisible(true);
    }
}

/** Panel to display the (limited) GUI intereface. */
class SysPropListGUI extends JPanel {
    JTextArea m_propertiesTA = new JTextArea(20, 40);

    /** Constructor sets layout, adds component(s), sets values*/
    public SysPropListGUI() {
        this.setLayout(new BorderLayout());
        this.add(new JScrollPane(m_propertiesTA), BorderLayout.CENTER);

        //... Add property list data to text area.
        Properties pr = System.getProperties();

	// TreeSet sorts keys
        TreeSet propKeys = new TreeSet(pr.keySet());  
        for (Iterator it = propKeys.iterator(); it.hasNext(); ) {
            String key = (String)it.next();
            m_propertiesTA.append("" + key + "=" + pr.get(key) + "n");
        }
    }
}

Configuring Hudson Java Memory Settings When Running It As A Service

The default hudson installation as a windows or linux service is configured to use 256MB of system memory. While the memory might be good enough to start with, it might not be high enough to do complex or larger builds. If your application build process is memory intensive, you will probably start seeing the build failures with Hudson complaining about heap space.

For example, when I try to run the Cobertura code coverage, which is a memory intensive process as it requires loading of several thousands of JUnit XML reports for my fairly large project, into the Hudson server that is running as a service, here is the exception stack trace that I encountered.

Publishing Cobertura coverage report...
Publishing Cobertura coverage results...
FATAL: Java heap space
java.lang.OutOfMemoryError: Java heap space
	at com.sun.org.apache.xerces.internal.util.XMLStringBuffer.append(XMLStringBuffer.java:205)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.refresh(XMLDocumentScannerImpl.java:1522)
	at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.invokeListeners(XMLEntityScanner.java:2070)
	at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(XMLEntityScanner.java:1415)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2793)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
	at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
	at javax.xml.parsers.SAXParser.parse(SAXParser.java:198)
	at hudson.plugins.cobertura.CoberturaCoverageParser.parse(CoberturaCoverageParser.java:81)
	at hudson.plugins.cobertura.CoberturaCoverageParser.parse(CoberturaCoverageParser.java:52)
	at hudson.plugins.cobertura.CoberturaPublisher.perform(CoberturaPublisher.java:265)
	at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:36)
	at hudson.model.AbstractBuild$AbstractRunner.perform(AbstractBuild.java:630)
	at hudson.model.AbstractBuild$AbstractRunner.performAllBuildSteps(AbstractBuild.java:608)
	at hudson.model.AbstractBuild$AbstractRunner.performAllBuildSteps(AbstractBuild.java:584)
	at hudson.model.Build$RunnerImpl.post2(Build.java:159)
	at hudson.model.AbstractBuild$AbstractRunner.post(AbstractBuild.java:553)
	at hudson.model.Run.run(Run.java:1391)
	at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
	at hudson.model.ResourceController.execute(ResourceController.java:88)
	at hudson.model.Executor.run(Executor.java:145)


Where does hudson define the memory settings for the service?

The memory settings for service are generally located in hudson.xml file in the same directory as your hudson.war.

Here is what a default memory configuration looks like (the content is formatted to remove the comments)

<service>
  <id>hudson</id>
  <name>Hudson</name>
  <description>This service runs Hudson continuous integration system.</description>
  <env name="HUDSON_HOME" value="%BASE%"/>
  <executable>java</executable>
  <arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "%BASE%hudson.war" --httpPort=8080</arguments>
  <logmode>rotate</logmode>
</service>



How the change the memory setting?

Just by looking at the hudson.xml file above, you must have noted that the memory settings are located in the element.

Edit the arguments to change the memory. For example, if you want the hudson to run with 1GB memory,

replace -Xmx256m with -Xmx1048m

Make sure to restart the hudson windows service once you have made the changes.

Java Reflections Tutorial

This tutorial gives an insight on how Java Reflection can be used to load Java classes dynamically at the run time. Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine.

package com.kushal.utilities;

/**
 * @author Kushal Paudyal
 * 08/05/2008
 * 
 * Simple demonstration of Java Reflection mechanism to
 * instantiate a class, get it's method and call that
 * method with proper parameter passing.
 */
import java.lang.reflect.*;

public class JavaReflectionsDemo {

	public static void main(String args[]) {
		try {
			/*
			 * Step 1 Define a class loader
			 */
			ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
			/*
			 * Step 2: Define a class to be loaded.
			 */
			String classNameToBeLoaded = "com.kushal.utilities.DemoClassToBeLoaded";

			/*
			 * Step 3: Load the class
			 */
			Class myClass = myClassLoader.loadClass(classNameToBeLoaded);

			/*
			 *Step 4: create a new instance of that class
			 */
			Object whatInstance = myClass.newInstance();

			String methodParameter = "ABC";
			/*
			 * Step 5: get the method, with proper parameter signature.
			 * The second parameter is the parameter type.
			 * There can be multiple parameters for the method we are trying to call, 
			 * hence the use of array.
			 */

			Method myMethod = myClass.getMethod("myMethod123",
					new Class[] { String.class });

			/*
			 *Step 6:
			 *Calling the real method. Passing methodParameter as
			 *parameter. You can pass multiple paramters based on 
			 *the signature of the method you are calling. Hence
			 *there is an array. 
			 */
			String returnValue = (String) myMethod.invoke(whatInstance,
					new Object[] { methodParameter });

			System.out.println("The value returned from the method is:"
					+ returnValue);

			/*
			 * Catching Probable Exceptions
			 * You can simply catch the Generic 'Exception' to reduce your coding.		 
			 */
		} catch (ClassNotFoundException e1) {
			System.out.println("EXCEPTION:CLASS NOT FOUND");
		} catch (IllegalAccessException e2) {
			System.out.println("EXCEPTION:ILLEGAL ACCESS");
		} catch (InstantiationException e3) {
			System.out.println("EXCEPTION:INSTANTIATION EXCEPTION");
		} catch (NoSuchMethodException e4) {
			System.out.println("EXCEPTION:NO SUCH METHOD");
		} catch (InvocationTargetException e5) {
			System.out.println("EXCEPTION: INVOCATION TARGET");

		}
	}

}
/**
 * @author Kushal Paudyal
 * 08/05/2008
 * www.sanjaal.com/java
 */

/*
 * This is the demo class that we will use to load and invoke method
 * using Java Reflection.
 */
class DemoClassToBeLoaded {
	public static String myMethod123(String someParam) {
		System.out.println("You Are Calling myMethod()");
		System.out.println("You Passed " + someParam + " as Parameter");

		/*
		 * Just returning the current class name
		 * This is just an example method. 
		 */
		return DemoClassToBeLoaded.class.getName();
	}

}