DrXCheng
DrXCheng

Reputation: 4132

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!

Upvotes: 45

Views: 126728

Answers (5)

Jan
Jan

Reputation: 432

  1. First, you should put your source code .java files in src/.

  2. Second, you make build/ folder in your root project

  3. Third, you make a jars/ folder in your root project

So, now you will have src/ and build/ and /jars under your root project

To compile

javac -d ./build -cp ./jars/itextpdf-5.5.13.3.jar:jars/pdfbox-app-2.0.27.jar src/*.java

To run:

java -cp ./build:./jars/itextpdf-5.5.13.3.jar:jars/pdfbox-app-2.0.27.jar Main

Upvotes: 0

Raihanul Alam Hridoy
Raihanul Alam Hridoy

Reputation: 571

Suppose your jar application "myapp.jar" has the following code snippet written inside it.

import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
    // write your code here
        System.out.println("Hello World!");
        JSONObject jo = new JSONObject("{ \"abc\" : \"def\" }");
        System.out.println(jo.toString());
    }
}

It is using the external library json.jar from which we imported "org.json.JSONObject". Running the following command will result in an error.

java -jar myapp.jar

Exception message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject at com.reve.Main.main(Main.java:10) Caused by: java.lang.ClassNotFoundException: org.json.JSONObject at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source) at java.base/java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more

We must include the json.jar file while running the jar file. We have to specify the class path of the file before building our myapp.jar file.

Inside META-INF/MANIFEST.MF file:

Manifest-Version: 1.0
Class-Path: lib/json.jar lib/example2.jar
Main-Class: com.reve.Main

Specify the external libraries separated by spaces under the Class-Path keyword. Then after building the project and the artifact, we can run the jar file by simply writing the same command we discussed above.

Upvotes: 0

SparkOn
SparkOn

Reputation: 8956

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

Upvotes: 7

RanRag
RanRag

Reputation: 49577

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

Upvotes: 84

Seid.M
Seid.M

Reputation: 185

  1. you can set your classpath in the in the environment variabl CLASSPATH. in linux, you can add like CLASSPATH=.:/full/path/to/the/Jars, for example ..........src/external and just run in side ......src/Report/

Javac Reporter.java

java Reporter

Similarily, you can set it in windows environment variables. for example, in Win7

Right click Start-->Computer then Properties-->Advanced System Setting --> Advanced -->Environment Variables in the user variables, click classPath, and Edit and add the full path of jars at the end. voila

Upvotes: 0

Related Questions