45

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!

5 Answers 5

85

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

Sign up to request clarification or add additional context in comments.

11 Comments

For example, I put all the JAR files in src/external/ and the source file is in src/Report/Reporter.java, can you give the specific commands?
replacing ';' with ':' in java -cp .;src/external/myfile.jar Reporter worked for me..
@sattu: I believe you are on a linux system.
to run with external jar and external path (jar and my clas are in the same directory) /usr/bin/java -cp /home/username/folderx/j.jar:/home/username/folderx MyClass
@RanRag I am on a Windows system and I had to use ";" instead of ":"
|
7

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

Comments

0
  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

Comments

0

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.

Comments

0
  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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.