Mikecit22
Mikecit22

Reputation: 157

Run java class from command line, including libraries from a jar

I have a file 'Junit.class' and I run it from command line like normal (java Junit). In eclipse I run it and the imported statements work, getting them from the .jar files I have. when I run it from the command line it wont run. Eclipse made the Junit.class file and I do

:~java Junit

Errors come from it not being able to find the imported classes.Do I need to include the jar file and if so how, for example is there a way to do ?

:~ java (jar_File_1) (Jar_file_2) Junit.class

Basically I just need the jar files for the imported classes in my Junit class.

Upvotes: 2

Views: 1422

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272257

You need to set the classpath to reference the relevant .jar files and directories e.g.

$ java -cp jarfile1.jar;jarfile2.jar;/home/user/classes mypackage.MyClass

Note the ; separator is for Windows, and you should use : for Unix. Note also that you specify the classname without the .class suffix.

Check out the official Oracle documentation for more info on this often confusing subject.

Upvotes: 5

Related Questions