How to add multiple jar files in classpath in linux
You use the -classpath
argument. You can use either a relative or absolute path. What that means is you can use a path relative to your current directory, OR you can use an absolute path that starts at the root /
.
Example:
bash$ java -classpath path/to/jar/file MyMainClass
In this example the main
function is located in MyMainClass
and would be included somewhere in the jar file.
For compiling you need to use javac
Example:
bash$ javac -classpath path/to/jar/file MyMainClass.java
You can also specify the classpath via the environment variable, follow this example:
bash$ export CLASSPATH="path/to/jar/file:path/tojar/file2"
bash$ javac MyMainClass.java
For any normally complex java project you should look for the ant script named build.xml
Set classpath permanently in Linux
You could add that export
statement to the end of your bash init file ~/.bashrc
, therefore it will in effect all the time.
By the way, a better setting of CLASSPATH
would be
export CLASSPATH="/path/to/file":"/path/to/file2":"${CLASSPATH}"
this will also preserve the initial value of that environment variable.
How to set CLASSPATH in Linux to let java find jar file?
You have to supply the complete path after the parameter -jar
. So for your example you have to call
java -jar /home/user/plantuml.jar -testdot
The $CLASSPATH
is only evaluated to find additional files (classes/resources) but not the jar file defined in the command line.
Setting classPath inside Linux Script
You need to use:
export CLASSPATH=/home/praveen/lib/commons-logging-1.0.4.jar:/home/praveen/lib/log4j-1.2.8.jar
rather than:
export CLASSPATH = /home/praveen/lib/commons-logging-1.0.4.jar: /home/praveen/lib/log4j-1.2.8.jar
You can't put spaces in between the variable name and the equals sign in shell scripting.
unix java classpath cp adding
Try this one:
java -cp /path/to/jar1.jar:/path/to/jar2.jar:. com.mylib.MyMainClass
Note that you should not omit that final .
in the classpath, which represent the current working directory, because using -cp
would override your previous classpath setting.
Related Topics
Image Encryption/Decryption Using Aes256 Symmetric Block Ciphers
Android Camera Will Not Work. Startpreview Fails
Select Random Document from Firestore
Rjava Install Error "Java_Home Cannot Be Determined from the Registry"
Accessing Linux Local File System from Java Web Application
Kafka - Broker: Group Coordinator Not Available
Why Does Killing Jvm Also Terminates Its Child Process If Waitfor Has Been Used
Do Threads Created in Java Behave Differently on Windows and Linux
Error While Loading Shared Libraries: Libjli.So in Java
What Does "|=" Mean? (Pipe Equal Operator)
How to Get the First Day of the Current Week and Month
Android: Difference Between Onintercepttouchevent and Dispatchtouchevent
Exception in Thread "Main" Java.Lang.Noclassdeffounderror: Helloworld
How to Avoid Java.Net.Bindexception: Address Already in Use
Best Way to Daemonize Java Application on Linux