Sunday, December 12, 2010

Dealing with external jar files on command prompt

I had a problem with compiling and running a java file in command line where it imports an external jar files. Here is the solution I've found.

Compilation

javac -cp <path to where the source codes resides>;<path to where the jar file resides> Example.java

run

java <path to where the source codes resides>;<path to where the jar file resides> Example <command-line arguments if any>

Note 1:  separator ; for windows and : for unix
Note 2:  multiple jar files can be used delimited by ;
Note 3:  . can be used to specify the current directory

3 comments :

  1. Thanks for sharing information dude. keep it up.

    Javin
    Why String is immutable in Java

    ReplyDelete
  2. Thanks a lot for the encouragement :-)

    -pavithra

    ReplyDelete
  3. Thanks. Since Java 6 there is also a path wildcard available.

    For example:
    java -cp "lib\*;lib\sub\*" Example

    This command adds all *.jar and *.JAR files from lib and lib\sub directory to the classpath.

    http://download.oracle.com/javase/6/docs/technotes/tools/windows/javac.html

    ReplyDelete