Search code examples
javajar

How to run Java program in terminal with external library 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!


Solution

  • 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