Java 6 Refresher

JDK 4 introduced the assert keyword

JDK 5 introduced generics, autoboxing, enumerations, var-args, enhanced for loop.

New keywords in Java

assert (since 1.4)
enum (since 1.5)

keywords to watch out for

const
strictfp
goto

JavaBean standard for getter and setter for boolean:

getXXX
setXXX / isXXX

JavaBean standard for getter and setter for listners:

addXXXListener(XXXListener xxxlistener)
removeXXXListener(XXXListener xxxlistener)

The strictfp non access modifier

Applicable to class and method
Marking a class strictfp means that any method code in the class/method will conform to a floating point standard without relying on the underlying operating system.

Points to note:

All variables declared in an interface are implicitly “public static final”
All methods in an interface are implicitly “public abstract”
An interface implicitly is always abstract
An interface method cannot be static since interface methods are meant to be overrridden and static methods cannot be overridden.
Interface methods cannot be native/strictfp
Abstract classes have to be explictly declared “abstract”
An interface may have public/default access modifiers just like classes
An interface can exend more than one interfaces
Abstract classes can extend other abstract classes
Abstract classes can implement one or more interfaces.
Abstract classes can never contain private methods since all its methods are meant to be overridden.
A native method must end with a semicolon with the implementation omitted.
Native methods are implemented in platform dependent code.

Flavors of the final modifier

final methods cannot be overrridden
final variables cannot change the value assigned to them
final classes cannot be extended
final references cannot be used to reference another object
*final reference variables must be initialized before the constructor completes

the abstract modifer cannot be combined with the static modifier for methods since abstract implies overriding while static methods cannot be overrridden.

“synchronized” modifier applies to code blocks and methods
*”sychronized” can be combined with static/ final

class TestFinalVar {
	public void testFinalVar(final int a, final int b) {// a and b cannot be
														// reassigned //within
														// this method
		System.out.println(a + b);
	}

	public static void main(String args[]) {
		TestFinalVar tfv = new TestFinalVar();
		tfv.testFinalVar(2, 3);
	}
}

Var-args:
Things to remember:
Only one var-arg is allowed in a list of arguemnts
A var-arg always has to be the last in a list of arguments.
var-args can be part of arguments to a constructor
more on var-args…

Note:
Negative numbers in Java are represented by 2’s complement notation:
1. subtract 1
2. take 1’s complement of the result(replace 1 with 0 and vice versa)

byte(8 bits=1 byte)
short(16 bits=2 bytes)
int(32 bits-4 bytes) float(4 bytes-signed)
long(64 bits=8 bytes) double(8 bytes-signed)
boolean is OS dependent

Variables are always created on the stack while objects on the heap

Transient variables:
By declaring a variable transient you are asking the JVM to skip it during serialization

Volatile variables:
These are used to transmit single variables between threads. Synchronized blocks are preferred to volatile variables. Volatile vars will never use local copies. They will use shared memory for reading and writing. If you want to transfer two or more variables there is a risk that reading will be done while the writing has not yet finished. “volatile” indicates that the var may be used by other threads.

Nested classes:

An inner class can be static/ non-static/ abstract/ final/ public/ private/ protected
A method local class can be instantiated only inside a method where it is defined, a local block, a static initializer or an instance intializer (need more info)
It cannot access the local vars of the enclosing method unless they are declared final. The only modifier for a method local class is abstract/ final

class Outer {
	static class StaticNested {
		public void print() {
			System.out.println("Hallo World");
		}
	}

	public static void main(String[] args) {
		Outer.StaticNested ou = new Outer.StaticNested();
                // instance of StaticNested is not required.
		ou.print();
	}
}

As a separate class:

enum TShirtSize {// enum as a separate class
	SMALL, MEDIUM, LARGE,
}

public class TShirtTest {
	public void print() {
		TShirtSize tss = TShirtSize.SMALL;
		System.out.println("I always wanted " + tss);
	}

	public static void main(String args[]) {
		TShirtTest tt = new TShirtTest();
		tt.print();
	}
}

D:\java6\work>java TShirtTest
I always wanted SMALL

As a separate class:

enum ShirtSize {// enum as a separate class
	SMALL, MEDIUM, LARGE,
}

class Shirt {
	ShirtSize ss;
};

public class ShirtTest {
	public void print() {
		Shirt shirt = new Shirt();
		shirt.ss = ShirtSize.LARGE;
		System.out.println("I always wanted " + shirt.ss);
	}

	public static void main(String args[]) {
		ShirtTest tt = new ShirtTest();
		tt.print();
	}
}
D:\java6\work>java ShirtTest
I always wanted LARGE

As a class member:

class BallTest {
	enum Ball {// enum as a a class member
		SMALL, MEDIUM, LARGE,
	}

	public void print() {
		System.out.println("I always wanted " + Ball.MEDIUM);
	}

	public static void main(String args[]) {
		BallTest bt = new BallTest();
		bt.print();
	}
}
D:\java6\work>java BallTest
I always wanted MEDIUM

*Enum declarations outside a class must not be marked static/ final/ abstract/ private/ protected

enum with a constructor and method

enum Weight {// enum with a constructor and method
	LIGHT(20), MEDIUM(40), HEAVY(80);// note semicolon
	Weight(int pounds) {
		this.pounds = pounds;
	}

	public int getPounds() {// method
		return pounds;
	}

	private int pounds;;// instance var after enum value declaration
}

public class WeightTest {
	public static void main(String args[]) {
		Weight bert = Weight.LIGHT;
		Weight amy = Weight.MEDIUM;
		Weight brown = Weight.HEAVY;
		System.out.println(bert.getPounds() + "  " + amy.getPounds() + "  "
				+ brown.getPounds());
	}
};
D:\java6\work>java WeightTest
20  40  80

The compiler looks only at the reference type, not the instance type. At runtime the actual instance is looked up. Only overridden instance methods are dynamically invoked based on the real object’s type. Not static methods, not variables. But which overloaded version of the method to be called is decided at based on the reference type of the argument passed at compile time.

class Parent {
	public void study() throws Exception {
	}
};
class Child extends Parent {
	public void study() {
	}
}
public class CompilerVsRuntimeTest {
	public static void main(String[] args) {
		Parent p = new Parent();
		Child c = new Child();
		Parent pc = new Child();
		c.study();// ok
		pc.study();// compiler sees exception thrown
	}
}
D:\java6\work>javac CompilerVsRuntimeTest.java
CompilerVsRuntimeTest.java:17: unreported exception java.lang.Exception; must be
caught or declared to be thrown
pc.study();//compiler sees exception thrown
^
1 error
class Parent {
	public void groan() {
		System.out.println("Grooooooan");
	}
};
class Child extends Parent {
	public void groan() {
		System.out.println("Yeaaaaaaaaaaah!!");
	}

	public void play() {
		System.out.println("Will I be called?");
	}
};
public class InheritanceTest {
	public static void main(String[] args) {
		Parent p = new Parent();
		Parent c = new Child();// Child is the father of man?
		p.groan();
		c.groan();
		c.play();
	}
}
D:\java6\work>javac InheritanceTest.java
InheritanceTest.java:22: cannot find symbol
symbol  : method play()
location: class Parent
c.play();
^
class Parent {
	public void groan() {
		System.out.println("Grooooooan");
	}
};

class Child extends Parent {
	public void groan() {
		System.out.println("Yeaaaaaaaaaaah!!");
	}

	public void play() {
		System.out.println("Will I be called?");
	}
};

public class InheritanceTest {
	public static void main(String[] args) {
		Parent p = new Parent();
		Parent c = new Child();// Child is the father of man?
		p.groan();
		c.groan();
		// c.play();
	}
}
D:\java6\work>java InheritanceTest
Grooooooan
Yeaaaaaaaaaaah!!
class Parent {
};

class Child extends Parent {
};

public class OverloadTest {
	public void doStuff(Parent p) {
		System.out.println("Parent version");
	}

	public void doStuff(Child c) {
		System.out.println("Child version");
	}

	public static void main(String[] args) {
		Parent p = new Parent();
		Parent c = new Child();// Child is the father of man?
		OverloadTest ot = new OverloadTest();
		ot.doStuff(p);
		ot.doStuff(c);
	}
}
D:\java6\work>java OverloadTest
Parent version
Parent version

As of Java5 you are allowed to change the return type of the overriding method as long as the new return type is a subtype of the declared return type of the superclass method.

A class that implements an interface must follow all the rules for legal overrides for the methods it implements.

Only static variables and methods can be accessed as part of the call to super() or this().

This is because instance methods cannot be run before the super() call returns.

Loose coupling and tight cohesion

Playing with constructors

Child.java

public class Child extends Parent {
	static String staticNameVar = "NoNameYet";
	int ageInstanceVar = 5;

	Child(String name, int age) {
		this(name);
		System.out.println("My name is " + name + " and I am " + age
				+ " years old");
		doingThis("playing");
	}

	Child(String name) {
		super(doingThis("Studying"));// only static methods can be called before
										// constructor completes
		System.out.println("My name is " + name);
	}

	public static String doingThis(String activity) {
		System.out.println("I am " + activity);
		return ("Had enough of " + activity);
	}

	public static void main(String[] args) {
		Child child = new Child("Amy", 19);
	}

}
class Parent {
	Parent(String msg) {
		System.out.println("I am your parent; my message is " + msg
				+ ". Is it?");
	}

	Parent() {
		System.out.println("I am your parent I have no message for you");
	}
};
/**
Output ->

I am Studying
I am your parent; my message is Had enough of Studying. Is it?
My name is Amy
My name is Amy and I am 19 years old
I am playing

*/

Running a standalone jar through the command line

I am going to run the SAX application jar ( http://cuppajavamattiz.com/2009/03/19/using-a-sax-parser-to-map-an-xml-to-a-pojo-bean/ ) directly from the command line. I maybe accessing some parameters in a round about way but that is just to demonstrate some features that one might find handly in a real situation.
I have modified some of the main class for the SAX parser example While the code for the POJO value object and the default handler remains the same. The mapper.xml is also reused here.

The Main class: SaxMain.java

package com.mattiz.sax.reader;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

public class SaxMain {
	public static void main(String args[]) {
		String xmlLocation = null;
		String xmlFileName = null;
		try {
			File file = new File(
					System.getProperty("mattiz.saxReaderConfigLocation"));
			InputStream is = new FileInputStream(file);
			Properties sqlCache = new Properties();
			sqlCache.load(is);
			xmlLocation = sqlCache.getProperty("XMLlocation");
			if (args.length != 0) {
				xmlFileName = args[0];
			} else {
				System.out.println("Mapping xml not specified");
				System.exit(-1);
			}
			XMLReader parser = XMLReaderFactory.createXMLReader();
			DefaultHandler saxHandler = new SaxHandler();
			parser.setContentHandler(saxHandler);
			parser.parse(xmlLocation + "\\" + xmlFileName);
			parser = null;
			ArrayList<ElementsVO> elArray = ((SaxHandler) saxHandler)
					.getElementArray();
			for (ElementsVO a : elArray) {
				System.out.print(" Group is " + a.getGroup());
				System.out.print(" Chemical Name is " + a.getChemicalName());
				System.out
						.print(" Chemical Symbol is " + a.getChemicalSymbol());
				System.out.print(" Latin Name is " + a.getLatinName());
				System.out.println("");
				System.out.println("**********");
			}
		} catch (SAXException e) {
			// TODO
			e.printStackTrace();
		} catch (IOException e) {
			// TODO
			e.printStackTrace();
		}
	}
}

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="XMLReader" default="clean" basedir=".">
	<property environment="env" />
	<property name="build.dir" location="./build" />
	<property name="build.classes.dir" location="${build.dir}/classes" />
	<property name="build.dist.dir" location="${build.dir}/dist" />
	<property name="lib.dir" location="./lib" />
	<target name="clean" depends="jar">
		<delete dir="${build.classes.dir}" />
	</target>
	<target name="init">
		<mkdir dir="${build.dir}" />
		<mkdir dir="${build.classes.dir}" />
		<mkdir dir="${build.dist.dir}" />
	</target>
	<target name="compile" depends="init">
		<javac srcdir="${basedir}" destdir="${build.classes.dir}" debug="on"
			deprecation="on" optimize="off" />
	</target>
	<manifest file="MANIFEST.MF">
		<!--<attribute name="Class-Path" value="./xerces.jar" /> -->
		<attribute name="Main-Class" value="com.mattiz.sax.reader.SaxMain" />
	</manifest>
	<target name="jar" depends="compile">
		<jar destfile="${build.dist.dir}/XMLReader.jar" manifest="MANIFEST.MF">
			<fileset dir="${build.classes.dir}" includes="**/*.class"
				excludes="**/*.java" />
		</jar>
	</target>
</project>

The structure of the project resources on the Eclipse IDE I am using can be deduced from the property attribute values, so I am not describing that.

Runing the build.xml will produce XMLReader.jar under XMLReader/build/dist

I have created the following folder structure to place the resources to run the jar.

<sax-xml>
       XMLReader.jar
       <config>
            saxreader.properties
       <xml>
            mapper.xml

I am not using any additional jar files in this app but if any jars were to be referenced the commented line in the build.xml:

<attribute name="Class-Path" value="./xerces.jar" />

could be used to access the jars where value attribute would hold the jar and jar locations as shown.

Here is the content of saxreader.properties:

XMLlocation=.\\xml

In the java code you can get path of saxreader.properties by doing
System.getProperty(“mattiz.saxReaderConfigLocation”);
where mattiz.saxReaderConfigLocation property has been passed through the command line invocation as the path to the directory that hold the xml file. (directory could be absolute or relative path, it doesn’t matter)
The params to the script(in this case name of the xml file) is being passed on as is to the java program.
args [0] will give you the name of the xml file in this case mapper.xml
In this example, I have put the XMLReader.jar in the current directory and properties file in a subfolder called config and the xml file in a subfolder called xml.

Run the jar using

java -jar -Dmattiz.saxReaderConfigLocation=.\config\saxreader.properties XMLReader.jar mapper.xml

standing at sax-xml folder.
The output should be something like this:

D:\work\projects\JAVA6SANDBOX\sax-xml>java -jar -Dmattiz.saxReaderConfigLocation=.\config\saxreader.properties XMLReader.jar mapper.xml

Group is Reactive Elements Chemical Name is Sodium Chemical Symbol is Na Latin Name is Natrium
**********
Group is Reactive Elements Chemical Name is Potassium Chemical Symbol is K Latin Name is Kalium
**********
Group is Metals Chemical Name is Antimony Chemical Symbol is Sb Latin Name is Stibium
**********
Group is Metals Chemical Name is Copper Chemical Symbol is Cu Latin Name is Cuprum
**********
Group is Metals Chemical Name is Tin Chemical Symbol is Sn Latin Name is Stannum
**********


A point to remember:
If you you use java -jar xyz.jar then you cannot set classpath using -cp in the dos command invocation. It has to be in the manifest file as described in the commented build.xml entry.

Using a script to run a standalone jar

I want to demonstrate how to run Dom Parser application using a dos script (batch file). I maybe accessing some parameters in a round about way but that is just to demonstrate some features that one might find handly in a real situation.
I have modified some of the main class for the Dom parser example and the code for the POJO value object remains the same. The mapper.xml is also reused here.
Refer http://cuppajavamattiz.com/2009/03/19/using-jdom-to-map-an-xml-to-a-pojo-bean/ The Main class: ElementMapper.java

package com.mattiz.dom.reader;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
 
public class ElementMapper {
    Document doc;
    SAXBuilder builder;
 
    public ElementMapper(String xmlFile) throws Exception {
        try {
            builder = new SAXBuilder();
            // disabling xml validation
            builder.setValidation(false);
            builder.setIgnoringElementContentWhitespace(true);
            doc = builder.build(new File(xmlFile));
        } catch (JDOMException e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }
 
    private ArrayList<ElementsVO> getXMLData() throws Exception {
        ArrayList<ElementsVO> elementsVOArrList = new ArrayList<ElementsVO>();
        try {
            List ElementSetList = ((List) XPath
                    .selectNodes(doc, "//ElementSet"));
            for (int i = 0; i < ElementSetList.size(); i++) {
                Element elementNode = (Element) ElementSetList.get(i);
                // get children of this parent
                List<Element> elementList = (elementNode)
                        .getChildren("Element");
                // iterate over list of list
                ElementsVO elementsVO = null;
                for (Element element : elementList) {
                    elementsVO = new ElementsVO();
                    elementsVO.setGroup(elementNode.getAttributeValue("group"));
                    // get ChemicalSymbol child
                    Element ChemicalSymbolSibling = element
                            .getChild("ChemicalSymbol");
                    elementsVO.setLatinName(ChemicalSymbolSibling.getAttribute(
                            "latin").getValue());
                    // get value of ChemicalSymbol element
                    String chemicalSymbol = ((Element) ChemicalSymbolSibling)
                            .getValue();
                    // get ChemicalName child
                    Element ChemicalNameSibling = element
                            .getChild("ChemicalName");
                    // get value of ChemicalName element
                    String chemicalName = ((Element) ChemicalNameSibling)
                            .getValue();
                    // setting to valueobject
                    elementsVO.setChemicalSymbol(chemicalSymbol);
                    elementsVO.setChemicalName(chemicalName);
                    // add to arraylist
                    elementsVOArrList.add(elementsVO);
                }// for
            }// for
        } catch (Exception ex) {
            throw new Exception();
        }
        return elementsVOArrList;
    }
 
    public static void main(String args[]) {
        String xmlLocation = null;
        String xmlFileName = null;
        try {
            File file = new File(
                    System.getProperty("mattiz.domReaderConfigLocation"));
            InputStream is = new FileInputStream(file);
            Properties sqlCache = new Properties();
            sqlCache.load(is);
            xmlLocation = sqlCache.getProperty("XMLlocation");
            if (args.length != 0) {
                xmlFileName = args[0];
            } else {
                System.out.println("Usage: domreader.bat {xml_file_name}");
                System.exit(-1);
            }
            ElementMapper elmapper = new ElementMapper(xmlLocation + "\\"
                    + xmlFileName);
            List<ElementsVO> elementsList = elmapper.getXMLData();
            for (ElementsVO vo : elementsList) {
                System.out.print("  GROUP IS " + vo.getGroup());
                System.out.print("  CHEMICAL NAME IS " + vo.getChemicalName());
                System.out.print("  CHEMICAL SYMBOL IS "
                        + vo.getChemicalSymbol());
                System.out.println(" LATIN NAME IS " + vo.getLatinName());
                System.out.println("************");
            }
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
            // handle exception
            System.exit(-1);
        }
    }
}

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="DomReader" default="clean" basedir=".">
    <property environment="env" />
    <property name="build.dir" location="./build" />
    <property name="build.classes.dir" location="${build.dir}/classes" />
    <property name="build.dist.dir" location="${build.dir}/dist" />
    <property name="lib.dir" location="./lib" />
    <path id="build.path">
        <pathelement location="${lib.dir}/jdom.jar" />
        <pathelement location="${lib.dir}/jaxen-core.jar" />
        <pathelement location="${lib.dir}/jaxen-jdom.jar" />
        <pathelement location="${lib.dir}/saxpath.jar" />
    </path>
    <target name="clean" depends="jar">
        <delete dir="${build.classes.dir}" />
    </target>
    <target name="init">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${build.classes.dir}" />
        <mkdir dir="${build.dist.dir}" />
    </target>
    <target name="compile" depends="init">
        <javac srcdir="${basedir}" destdir="${build.classes.dir}" debug="on"
            deprecation="on" classpathref="build.path" optimize="off" />
    </target>
    <target name="jar" depends="compile">
        <jar destfile="${build.dist.dir}/DomReader.jar">
            <fileset dir="${build.classes.dir}" includes="**/*.class"
                excludes="**/*.java" />
        </jar>
    </target>
</project>

The structure of the project resources on the Eclipse IDE I am using can be deduced from the property attribute values in the build.xml, so I am not describing that.
Runing the build.xml will produce DomReader.jar under DomReader/build/dist
I have created the following folder structure to place the resources to run the jar from a script.

<dom-xml>
      domreader.bat
      DomReader.jar
      <lib>
           jdom.jar
           jaxen-core.jar
           jaxen-jdom.jar
           saxpath.jar
      <config>
           domreader.properties
      <xml>
           mapper.xml

The jar files will be found in the JDOM installation as mentioned in the previous post.
Here is the content of domreader.properties:

XMLlocation=.\\xml

Finally domreader.bat has the following contents:

REM Environment variables
set JAVA_HOME=”C:\Program Files\Java\jre1.6.0_02″
set CONFIGURATION_LOCATION=.\config\domreader.properties
set REQUIRED_JARS=.\DomReader.jar;.\lib\jdom.jar;.\lib\jaxen-core.jar;.\lib\jaxen-jdom.jar;.\lib\saxpath.jar;%classpath%
echo off
%JAVA_HOME%\bin\java -cp %REQUIRED_JARS%; -Dmattiz.domReaderConfigLocation=%CONFIGURATION_LOCATION% com.mattiz.dom.reader.ElementMapper %1
echo Exit Code : %ERRORLEVEL%

In the java code you can get path of domreader.properties by doing
System.getProperty(“mattiz.domReaderConfigLocation”);
where mattiz.domReaderConfigLocation property has been defined in the script and contains the path to the directory that hold the xml file. (directory could be absolute or relative path, it doesn’t matter)
The params to the script(in this case name of the xml file) is being passed on as is to the java program… See %1 at the end of the script.
So in the main method of the java program args[0] will give you the name of the xml file in this case mapper.xml
In this example, I have put the DomReader jar in the current directory (where script is being executed) and the remaining jars in a subfolder called lib; and properties file in a subfolder called config while the xml file is in a subfolder called xml.

Run the script using
domreader.bat mapper.xml
standing at dom-xml folder.

The output should be something like this:

D:\work\projects\JAVA6SANDBOX\dom-xml>domreader.bat mapper.xml
D:\work\projects\JAVA6SANDBOX\dom-xml>REM Environment variables
D:\work\projects\JAVA6SANDBOX\dom-xml>set JAVA_HOME="C:\Program Files\Java\jre1.6.0_02"
D:\work\projects\JAVA6SANDBOX\dom-xml>set CONFIGURATION_LOCATION=.\config\domreader.properties
D:\work\projects\JAVA6SANDBOX\dom-xml>set REQUIRED_JARS=.\DomReader.jar;.\lib\jdom.jar;.\lib\jaxen-core.jar;.\lib\jaxen-jdom.jar;.\lib\saxpath.jar;.;
D:\work\projects\JAVA6SANDBOX\dom-xml>echo off
GROUP IS Reactive Elements CHEMICAL NAME IS Sodium CHEMICAL SYMBOL IS Na LATIN NAME IS Natrium
************
GROUP IS Reactive Elements CHEMICAL NAME IS Potassium CHEMICAL SYMBOL IS K LATIN NAME IS Kalium
************
GROUP IS Metals CHEMICAL NAME IS Antimony CHEMICAL SYMBOL IS Sb LATIN NAME IS Stibium
************
GROUP IS Metals CHEMICAL NAME IS Copper CHEMICAL SYMBOL IS Cu LATIN NAME IS Cuprum
************
GROUP IS Metals CHEMICAL NAME IS Tin CHEMICAL SYMBOL IS Sn LATIN NAME IS Stannum
************
Exit Code : 0

We could use the following shell script to run the jar on a Linux machine. The shell script should be placed in the same directory structure where the batch file stood.

#!/bin/bash
## Environment variables
export JAVA_HOME=/space/java/jdk1.5.0_07
export JAR_CLASSPATH=./DomReader.jar:./lib/jdom.jar:./lib/jaxen-core.jar:./lib/jaxen-jdom.jar:./lib/saxpath.jar
export CONFIGURATION_LOCATION=./config/domreader.properties
# If we got less than 1 arg, print usage and exit
if [ $# -lt 1 ]; then
echo 'Usage: domreader.sh {mapping xml file}'
exit -1
fi
$JAVA_HOME/bin/java -cp $JAR_CLASSPATH -Dmattiz.domReaderConfigLocation=$CONFIGURATION_LOCATION com.mattiz.dom.reader.ElementMapper $*
exit $exitValue

Tips
* The params to the shell script is being passed as is to the java program… See $* at the end of the shell script.
* Remember to specify the below as the first line of the sh script. You want to be sure the script is executed in bash and not some other shell, csh, ksh etc.
#!/bin/bash

* If you want to return the exitValue to the calling program, assuming some other shell script is invoking your shell script, and wants to know the status you can do it by
exit $exitValue
* Don’t forget to make the shell script executable.
chmod ugo+x *.sh

Call the script thus: domreader.sh mapper.xml