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.
set java classpath to multiple folders that have jar files and run from command line
Here is the solution that finally worked as expected. When the '*' character is used in the classpath for my specific scenario, it skipped everything after the first path. Using the double quotes(") for each path separately and then using ";" as the delimiter is the solution.
Having any space before or after the semi-colon ";" will also not work
javac -verbose -classpath "C:\Program Files\lib\java\core\*";"C:\Program Files\lib\java\core\locale\*";"C:\Program Files\lib\java\modules\*";"C:\Program Files\lib\java\modules\ext\*" testClass.java
How to get the path of a running JAR file?
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation()
.toURI()).getPath();
Replace "MyClass" with the name of your class.
Obviously, this will do odd things if your class was loaded from a non-file location.
Determine which JAR file a class is from
Yes. It works for all classes except classes loaded by bootstrap classloader. The other way to determine is:
Class klass = String.class;
URL location = klass.getResource('/' + klass.getName().replace('.', '/') + ".class");
As notnoop pointed out klass.getResource()
method returns the location of the class file itself. For example:
jar:file:/jdk/jre/lib/rt.jar!/java/lang/String.class
file:/projects/classes/pkg/MyClass$1.class
The getProtectionDomain().getCodeSource().getLocation()
method returns the location of the jar file or CLASSPATH
file:/Users/home/java/libs/ejb3-persistence-1.0.2.GA.jar
file:/projects/classes
Spring Boot Executable Jar with Classpath
If you just want add external libraries you can use the loader.path
property.
java -Dloader.path="your-lib/" -jar your-app.jar
UPDATE
If you also need to read additional files from the classpath you have to create/change the manifest file of your application.
Lets assume that your are initializing your Spring Boot context from the class de.app.Application
. Your MANIFEST.MF
should looks as follows:
Manifest-Version: 1.0
Main-Class: de.app.Application
Class-Path: your-lib/
And the you can simply start your app with java -Dloader.path="your-lib/" -jar MyApp.jar
.
For more information about the MANIFEST.MF please see Working with Manifest Files: The Basics.
Java Classpath not using current directory
Relying on the CLASSPATH
system variable is a bad idea. One machine can be used to run many varied apps, so the concept of a 'global setting' is silly.
Try java -cp . Main
.
If that DOES work
Then you've been messing with your CLASSPATH
environment var and removed .
from it. Put it back. Or better yet forget about CLASSPATH; always use -cp
; use -cp .
if you like. Or make jar
files and run them with java -jar thejar.jar
, which ignores both CLASSPATH
and the -cp
parameter (instead that will look at the Class-Path
entry in the jar manifest).
If that does not work
Then you were not in the appropriate directory. Or, you're confused about packages. Let's say your Main.java
looks like this:
package com.foo;
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
}
And you compile it; a Main.class
file shows up.
java
cannot run this unless Main.class
is in a directory named foo
, and that directory is in turn in a directory named com
. Then, you put the directory that contains com
on the classpath. Not the directory that contains Main.class
. And then you run java -cp theDirThatContainsCom com.foo.Main
. It is not possible to run this file with java -(does not matter what you try it cannot be done) Main
- because you provide the full name of the class you want to run to java
, and the full name is com.foo.Main
.
Execute jar file with multiple classpath libraries from command prompt
Let maven generate a batch file to start your application. This is the simplest way to this.
You can use the appassembler-maven-plugin for such purposes.
how to set class path for more than one jar is used in command prompt in java
For multiple jars you can do
java -cp "path1/servlet.jar;path2/mysql.jar" yourPackage.Class
Or put all jars in one folder (let it be path1/lib) and do something like following
java -cp "path1/lib/*" yourPackage.Class
Note : classpath wildcards can be used only in Java 6 or later.
Related Topics
Why Does "While(True)" Without "Thread.Sleep" Cause 100% CPU Usage on Linux But Not on Windows
Android: Difference Between Onintercepttouchevent and Dispatchtouchevent
How to Make This Jbutton Visible? When I Have Progressive Scan Background Jwindow()
Amazon Ec2 Ftp Write Permission
Run Custom Textsecure (Signal) Server
Kiosk Mode for Linux Java Swing Application
How to Set Ld_Library_Path for Java Process
Couldn't Install Netbeans 11.3 with Java 14 Due to Error: "Unsupported Jvm Version"
Why This Javac: File Not Found Error
How to Check If a File Is Open by Another Process (Java/Linux)
File.Encoding Has No Effect, Lc_All Environment Variable Does It
How to Avoid Java.Net.Bindexception: Address Already in Use
Could Not Reserve Enough Space for Object Heap to Start Jvm
Rjava Install Error "Java_Home Cannot Be Determined from the Registry"
How to Add Multiple Jar Files in Classpath in Linux
How to List Target Platforms. Please Make Sure the Android Sdk Path Is Correct