How to Run Java Program in Terminal With External Library Jar

How to run Java program in terminal with external library JAR

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

Compile and run java program with mutiple external jar files

If you want to add directory while contains all the required jars for running/ compiling your Java file you can use below command:

In Windows:

java -classpath ".;c:\lib*" MainClass

In UNIX/ Linux

java -classpath ".:/lib/*" MainClass

Note: In windows ; (semicolon) is the separator, while in UNIX/ Linux : (colon) is the separator for multiple jar for directory

. (dot) represents current directory

Compile java program in command line with external .jar

when you are executing below line, you are getting the below error right ?

javac -cp paillier.jar CoupleGen.java

Error: Could not find or load main class CoupleGen

That means compiler is looking for the CouplenGen.java in paillier.jar. Actually CoupleGen is outside of the jar file.

run java from terminal that depend on external libraries

First off, you just need to pass the class name to java:

java -Djava.library.path="libs/natives/" 
-cp libs/jars/lwjgl.jar:libs/jars/lwjgl_util.jar DisplayTest

(linebreaks for readability)

I would try the following:

1) Use the CLASSPATH enviroment variable, as in:

  CLASSPATH=.:/path/to/lwjgl/lwjgl.jar:/path/to/lwjgl/lwjgl_util.jar 
export CLASSPATH

Notice the dot (.) at the very beginning of the classpath;

2) Run your java application:

  java -Djava.library.path="libs/natives" DisplayTest

If this works, add the commands above to a shell script. Good luck!

Java command line with external .jar

Concatenate each jar file argument to cp with:

; on Windows
: on Linux or Mac

e.g.

java -cp <path>\TOOLS.jar;.;<path>\jar2.jar;<path>\jar3.jar HelloWorld

on newer JVMs (6+, I think) you can also use the * to append all JARs in a directory e.g.

java -cp .;<path>\*; HelloWorld

To go a step further and create a single packaged executable see this question.



Related Topics



Leave a reply



Submit