Running a standalone jar through the command line

I am going to run the SAX application jar ( ) 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:

package com.mattiz.sax.reader;

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(
			InputStream is = new FileInputStream(file);
			Properties sqlCache = new Properties();
			xmlLocation = sqlCache.getProperty("XMLlocation");
			if (args.length != 0) {
				xmlFileName = args[0];
			} else {
				System.out.println("Mapping xml not specified");
			XMLReader parser = XMLReaderFactory.createXMLReader();
			DefaultHandler saxHandler = new SaxHandler();
			parser.parse(xmlLocation + "\\" + xmlFileName);
			parser = null;
			ArrayList<ElementsVO> elArray = ((SaxHandler) saxHandler)
			for (ElementsVO a : elArray) {
				System.out.print(" Group is " + a.getGroup());
				System.out.print(" Chemical Name is " + a.getChemicalName());
						.print(" Chemical Symbol is " + a.getChemicalSymbol());
				System.out.print(" Latin Name is " + a.getLatinName());
		} catch (SAXException e) {
			// TODO
		} catch (IOException e) {
			// TODO


<?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 name="init">
		<mkdir dir="${build.dir}" />
		<mkdir dir="${build.classes.dir}" />
		<mkdir dir="${build.dist.dir}" />
	<target name="compile" depends="init">
		<javac srcdir="${basedir}" destdir="${build.classes.dir}" debug="on"
			deprecation="on" optimize="off" />
	<manifest file="MANIFEST.MF">
		<!--<attribute name="Class-Path" value="./xerces.jar" /> -->
		<attribute name="Main-Class" value="com.mattiz.sax.reader.SaxMain" />
	<target name="jar" depends="compile">
		<jar destfile="${build.dist.dir}/XMLReader.jar" manifest="MANIFEST.MF">
			<fileset dir="${build.classes.dir}" includes="**/*.class"
				excludes="**/*.java" />

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.


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


In the java code you can get path of by doing
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\ 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\ 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.

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: