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.

Advertisements

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


  1. 1 Imhoo
    September 29, 2009 at 23:05

    This was very helpful. Thanks.

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

    This post was helpful. Many thanks. ๐Ÿ™‚

  3. 4 TR
    January 8, 2010 at 16:19

    Thanks for the help, found you via Google

  4. 7 emanuelvianna
    August 9, 2010 at 05:32

    thanks for the help! ๐Ÿ™‚

    found you via Google [2]

  5. 9 chittineni
    December 23, 2010 at 11:54

    very helfull coding………..

  6. January 30, 2011 at 17:59

    Hi,

    Its worth noting that if you have two classes with same name in classpath in that case one which comes earlier in classpath will get picked up. this concept is very useful to test patch releases where you update only few classes to quickly test patch release or have added some debug print statement to troubleshoot any issue. to read more about How classpath works in Java

    Thanks
    Javin
    FIX Protocol Interview Questions

  7. 11 carlos
    June 8, 2011 at 19:33

    Compile:

    javac -cp .:lucene-core-3.0.2.jar:mysql-connector-java-5.1.16-bin.jar Indexer.java

    Run:

    java -cp .:lucene-core-3.0.2.jar:mysql-connector-java-5.1.16-bin.jar Indexer /home/public_html/index_main

  8. 12 Oleg
    February 16, 2012 at 18:38

    Thank you, very helpful indeed!

  9. 13 Andreas Draganis
    June 28, 2012 at 01:21

    This helped me as well, thanks!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Blog Stats

  • 221,399 hits

%d bloggers like this: