02
Jun
09

how to run a java project written in eclipse from the command line


I wanted to run a large java project from the command line. I knew I had to add the jar dependencies as parameters to the java binary, but I wasn’t sure how to do that. After a bit of googling around , I found out how to achieve this. Here’s a skeleton :

java -classpath lib1.jar;lib2.jar package.otherpackage.classname argv1 argv2 ... argvn

If you have your .class files in a folder, let’s name it classes, then you need to add that folder to the classpath, so the skeleton becomes :

java -classpath classes;lib1.jar ...

I hope you get the picture. If you don’t know what jars to include, but your project works in eclipse, you can open the .classpath file and extract the entries with kind=”lib”. Here is a sample .classpath file from a project :


<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
	<classpathentry kind="lib" path="lib/commons-codec-1.3.jar"/>
	<classpathentry kind="lib" path="lib/commons-collections-3.2.1.jar"/>
	<classpathentry kind="lib" path="lib/commons-httpclient-3.1.jar"/>
	<classpathentry kind="lib" path="lib/commons-io-1.4.jar"/>
	<classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>
	<classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
	<classpathentry kind="lib" path="lib/cssparser-0.9.5.jar"/>
	<classpathentry kind="lib" path="lib/htmlunit-2.5.jar"/>
	<classpathentry kind="lib" path="lib/htmlunit-core-js-2.5.jar"/>
	<classpathentry kind="lib" path="lib/javax.servlet.jar"/>
	<classpathentry kind="lib" path="lib/nekohtml-1.9.12.jar"/>
	<classpathentry kind="lib" path="lib/sac-1.3.jar"/>
	<classpathentry kind="lib" path="lib/serializer-2.7.1.jar"/>
	<classpathentry kind="lib" path="lib/xalan-2.7.1.jar"/>
	<classpathentry kind="lib" path="lib/xercesImpl-2.8.1.jar"/>
	<classpathentry kind="lib" path="lib/xml-apis-1.3.04.jar"/>
	<classpathentry kind="output" path="bin"/>
</classpath>

You can easily parse this file and extract them. I’ve used it today to create a rake task that operates on a java project written in eclipse.

Hope this helps someone.


3 Responses to “how to run a java project written in eclipse from the command line”


  1. 1 Imhoo
    September 29, 2009 at 11:05 pm

    This was very helpful. Thanks.

  2. 2 Sharon
    December 1, 2009 at 10:52 am

    This post was helpful. Many thanks. :)


Leave a Reply