Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Java program in terminal with external library JAR

Tags:

java

jar

This should be simple but I have never done it before and didn't find any solution.

I am currently using Eclipse to code my program, which imports some external JAR library such as google data api library. I can use Eclipse to compile/build/run the program.

But now I want to run it in terminal, so where should I put those JAR files, and how to build and run the program?

Thanks!

like image 268
DrXCheng Avatar asked Jan 20 '12 23:01

DrXCheng


2 Answers

You can do :

1) javac -cp /path/to/jar/file Myprogram.java

2) java -cp .:/path/to/jar/file Myprogram

So, lets suppose your current working directory in terminal is src/Report/

javac -cp src/external/myfile.jar Reporter.java

java -cp .:src/external/myfile.jar Reporter

Take a look here to setup Classpath

like image 81
RanRag Avatar answered Nov 10 '22 01:11

RanRag


For compiling the java file having dependency on a jar

javac -cp path_of_the_jar/jarName.jar className.java

For executing the class file

java -cp .;path_of_the_jar/jarName.jar className

like image 39
SparkOn Avatar answered Nov 10 '22 00:11

SparkOn