I have a jar file which mentions the main class in the manifest.
When I try to execute the jar using the following command
java -cp .;./* com.foo.MainClass
The code executes and works.
When I try to execute the jar using the following command
java -cp .;./* -jar myjar.jar
I get class not found execptions for some jars which are in the same folder as myjar.jar. I hoping that the -cp option will include those jars in class path.
I modified my code to print java.class.path property. In the first case it listed all jars in the current directory, in second case it just listed myjar.jar
I also modified the manifest to add Class-Path element to it with all jars. Then the second command works. But in my code I am trying to load a aribtrary class whose name is provided at command prompt, so I want the class path to contain all jars in a folder. How do I make the second command work in this scenario?
From this,
An executable JAR must reference all the other dependent JARs it
requires through the Class-Path header of the manifest file. The
environment variable CLASSPATH and any class path specified on the
command line is ignored by the JVM if the -jar option is used.
You will need your own classloader to deal with this. -jar only respects the information in the Manifest and wildcards are not allowed there.
You might find the example of a reloadable class useful: http://www.exampledepot.com/egs/java.lang/ReloadClass.html
Here is a good discussion on this issue.
Related
What is the difference between running a Java application withjava -cp CLASSPATH and java -jar JAR_FILE_PATH? Is one of them preferred to the other for running a Java application? I mean which one of these ways is more expensive for JVM (according to their machine resources usage)?
Which one will cause JVM to spawn more threads while trying to run the application?
I prefer the first version to start a java application just because it has less pitfalls ("welcome to classpath hell"). The second one requires an executable jar file and the classpath for that application has to be defined inside the jar's manifest (all other classpath declaration will be silently ignored...). So with the second version you'd have to look into the jar, read the manifest and try to find out if the classpath entries are valid from where the jar is stored... That's avoidable.
I don't expect any performance advantages or disadvantages for either version. It's just telling the jvm which class to use for the main thread and where it can find the libraries.
With the -cp argument you provide the classpath i.e. path(s) to additional classes or libraries that your program may require when being compiled or run. With -jar you specify the executable JAR file that you want to run.
You can't specify them both. If you try to run java -cp folder/myexternallibrary.jar -jar myprogram.jar then it won't really work. The classpath for that JAR should be specified in its Manifest, not as a -cp argument.
You can find more about this here and here.
PS: -cp and -classpath are synonyms.
When using java -cp you are required to provide fully qualified main class name, e.g.
java -cp com.mycompany.MyMain
When using java -jar myjar.jar your jar file must provide the information about main class via manifest.mf contained into the jar file in folder META-INF:
Main-Class: com.mycompany.MyMain
java -cp CLASSPATH is necesssary if you wish to specify all code in the classpath. This is useful for debugging code.
The jarred executable format: java -jar JarFile can be used if you wish to start the app with a single short command. You can specify additional dependent jar files in your MANIFEST using space separated jars in a Class-Path entry, e.g.:
Class-Path: mysql.jar infobus.jar acme/beans.jar
Both are comparable in terms of performance.
Like already said, the -cp is just for telling the jvm in the command line which class to use for the main thread and where it can find the libraries (define classpath). In -jar it expects the class-path and main-class to be defined in the jar file manifest. So other is for defining things in command line while other finding them inside the jar manifest. There is no difference in performance. You can't use them at the same time, -jar will override the -cp.
Though even if you use -cp, it will still check the manifest file. So you can define some of the class-paths in the manifest and some in the command line. This is particularly useful when you have a dependency on some 3rd party jar, which you might not provide with your build or don't want to provide (expecting it to be found already in the system where it's to be installed for example). So you can use it to provide external jars. It's location may vary between systems or it may even have a different version on different system (but having the same interfaces). This way you can build the app with other version and add the actual 3rd party dependency to class-path on the command line when running it on different systems.
There won't be any difference in terms of performance.
Using java - cp we can specify the required classes and jar's in the classpath for running a java class file.
If it is a executable jar file . When java -jar command is used, jvm finds the class that it needs to run from /META-INF/MANIFEST.MF file inside the jar file.
Java can run jar files from the command line like this:
java -jar foobar.jar
However, if foobar.jar depends on baz.jar, the above will throw an exception as soon as any of the classes in baz.jar is invoked, as the JVM has no way to know where to look for these.
However, the man page (OpenJDK 8 on Linux) states that:
When you use the -jar option, the specified JAR file is the source of all user classes, and other class path settings are ignored.
If repackaging is not an option, is there a way to run a jar file with dependencies from the command line?
When you use java -jar, dependencies are not specified on the command line.
You have 2 ways to add jars to the class path:
Call java with the main class and add jar files, including your foobar.jar, on the command line:
java -cp foobar.jar:baz.jar com.mycompany.MainClass
Include dependencies in foobar.jar's manifest file (and then run java -jar)
Class-Path: baz.jar
I believe you have only 'one' main class in foobar.jar. If it is more then 1, then you need to specify which one to execute.
You can simply set the classpath, before executing the jar
export CLASSPATH=$CLASSPATH:/JAR_LOCATION/bar.jar
Under Linux I am trying to run a jar file as follows:
java -jar plantuml.jar -testdot
while having CLASSPATH set to any of the following (the file is located at /home/user/plantuml.jar):
export CLASSPATH=/home/user
export CLASSPATH=/home/user/
export CLASSPATH=/home/user/plantuml.jar
In either case, no matter how I define CLASSPATH, the java command gives an error Unable to access jarfile plantuml.jar. What am I doing wrong here?
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.
export CLASSPATH="/path/to/class_or_jar1":"/path/to/class_or_jar2":"${CLASSPATH}"
Maybe you are missing name of the main class or path to the jar. Have you tried execute it:
java -jar full_path/plantuml.jar package.YourClass -testdot
Is your program depending on other classes? If yes you might want to add -cp parameter.
The classpath is used to find classes when you refer to them by name. It's essentially a list of paths (directories AND jar/zip files) where the JVM needs to look for classes, or other resources when using methods like ClassLoader.getResourceAsStream().
The value passed to the -jar option on the command line is the file-path to the JAR file.
So, it won't find a jar file if you are only referring to the jar file by name. The JAR file path in the CLASSPATH is supposed to be a path element that 'contains' other resources.
What you need to do here, is either
Provide the full path to the jar file when trying to execute the jar
Set the classpath to the jar file's path, and run the java command giving the name of the main class you want to execute.
In Linux, how to execute Java jar file with external jar files?
Either use the -cp flag:
java -cp /path/to/somefolder/*.jar:/path/to/otherfolder/*.jar com.YourMainClass
Or add a Class-Path: header to your jar's manifest (see Jigar's answer)
Note to others who answered with java -jar <etc>: The -jar flag deactivates the standard -cp flag and CLASSPATH environment variable, because it retrieves the classpath from the JAR manifest. Any answer that combines -jar and either -cp or $CLASSPATH will not work.
This information is well-hidden, but I finally found a reference:
-jar
Execute a program encapsulated in a JAR file. The first argument is the
name of a JAR file instead of a
startup class name. In order for this
option to work, the manifest of the
JAR file must contain a line of the
form Main-Class: classname. Here,
classname identifies the class having
the public static void main(String[]
args) method that serves as your
application's starting point. See the
Jar tool reference page and the Jar
trail of the Java Tutorial for
information about working with Jar
files and Jar-file manifests. When you
use this option, the JAR file is the
source of all user classes, and other
user class path settings are ignored.
Source: java - the Java application launcher
java -jar /path/to/externalJarFile.jar
Update
You can add the required library in manifest with Class-Path: header
For example :
Class-Path: MyUtils.jar
See
Adding Classes to the JAR File's Classpath
I want to add the classpath through a command line call. The call looks like this:
java -cp ..\conf -jar ..\lib\switcher.jar
In the conf directory is an XML file that defines the application context and the log4j.properties file. But when I run this command, Java obviously can't find those two files, although I've added them to the class path.
I also tried specifying the absolute path of the conf directory, but it didn't work either.
Any hints?
If you use the -jar option, java.exe ignores the classpath. It will only look in that jar.
Try using the manifest Class-Path instead to get Java to look in auxiliary jars.
I guess your XML file may contain the used ClassPath ...
Unfortunatly, the java command doesn't understand any subtle way to express your class path. You HAVE to give it a list of directories/jar/zip. Any other way of setting the classpath will result in no action.