10

I have written a Java code which imports an external jar file. How can I compile and run it on the command-line?

Thanks in advance!

Pavithra Gunasekara
  • 3,902
  • 8
  • 36
  • 46

1 Answers1

31

Compiling from the command-line:

javac -cp path_to_jar1.jar:path_to_jar2.jar Example.java

Running:

java -cp .:path_to_jar1.jar:path_to_jar2.jar Example

For Windows, use ; as a path-separator (instead of :).

khachik
  • 28,112
  • 9
  • 59
  • 94
  • @ khachik Sorry for the late reply ... Thanx a lot for your advice... I marked your answer as correct... thanx again :-) – Pavithra Gunasekara Jan 02 '11 at 07:22
  • I still get an error: if I invoke `java -cp /usr/share/java/mysql-connector-java-5.1.42.jar Connect` I get the error that Java cannot find the `Connect` class. – Daniele Aug 16 '17 at 13:54
  • @Daniele 4 years late; but that would mean the Connect.class file is not being found, did you compile first? – HKVariant Aug 26 '21 at 16:11