Home > Java, Linux > Java 101: building your java classpath from a lib folder

Java 101: building your java classpath from a lib folder

February 23rd, 2009 deevis

OK – I admit it – my IDE and tools make me weak in many ways.  I was just having the hardest time getting my maven-managed project to run from a basic “java my.class.ClassName” command line invocation.  Why?  Because the freaking classpath is a million miles away from being a concern to me anymore.  So, jog the memory and immediately remember the -cp ( or -classpath ) option.  Also remember that I cannot specify a directory if I want to include all of the jars in that directory ( specifying a directory in the classpath will only load exploded .class files! ).

What I’ve got before me is a target/lib directory full of all of my maven2-managed dependencies.  I need to add each jar file explicitly into the classpath – barf!

Well, here’s a handy little shell script which will build that string for you:

for file in `ls lib`; do echo -n ‘lib/’ >> classpath.txt;echo -n $file >> classpath.txt;echo -n ‘:’ >> classpath.txt;done

Awesome – so just open the classpath.txt file and use that big ‘ol path however you want to.  Windows users would use a semi-colon instead of a colon above.  Also, remember that you need to specify the classpath option prior to the name of the class to run – otherwise the classpath is ignored ( as it is assumed to be an argument to the program and not a jvm option ).

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • TwitThis
Categories: Java, Linux Tags:
Comments are closed.