I have a big project that uses maven and I'm creating the project's jar. The problems is that the project has several classes that can be executed. How can I accomplish this?
Every time that I try to execute a class I get this message
java -jar library.jar ExecutableClass1
Failed to load Main-Class manifest attribute from
library.jar
The manifest.mf file in your JAR can only have one Main-Class entry.
Main-Class: classname
The jar would then by executed by running below.
java -jar library.jar
If you have several "executables" that could be run then you can either pass command line parameters to the JAR and then run the correct code or you can generate multiple JAR files for each "executable".
A possible alternative to using the -jar switch is to explicitly start one of the "executables":
java -cp library.jar ExecutableClass1
If your jar has dependencies, then these will need adding to the classpath as well.
Related
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
I had build/package two main-class into one jar package, and I would like to specialize one of them to start up according to my needs.
For sample, there is com.sample.MainClassOne and com.sample.bat.MainClassTwo in sample.jar. com.sample.MainClassOne will always startup when I run java -jar sample.jar. And sometimes I would like to start up com.sample.bat.MainClassTwo, how should I?
A runnable jar file is still a normal jar file, you can do:
java -cp sample.jar com.sample.bat.MainClassTwo
(in addition to being able to do java -jar sample.jar to run com.sample.MainClassOne )
If you are using eclipse, you can select which is the main class of a jar while you are creating runnable jar.
You can see it in MANIFEST.MF file inside jar file.
Change the Main-Class key entry to your desired class name
Is there a way to pass an external jar file when running a .jar application?
I'm trying to run my jar like this:
java -jar myJar.jar -cp externalJar.jar
The jar file executes fine but I want to look for classes in the external file. I can't include the other classes into my jar, because I want to be able to put any jar file in the same folder as my Jar file and look for classes in there.
The only way to do this right now is by running my app like this:
java -cp myJar.jar;externalJar.jar MainClass
I do not want to explicitly enter the path to my MainClass to run it's main method.
It really seems that the -cp option is completely ignored when you use the -jar option. At least this is what you can read on the manpage of java about the -jar option:
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.
Note that JAR files that can be run with the "java -jar" option can
have their execute permissions set so they can be run without using
"java -jar". Refer to Java Archive (JAR) Files.
I found this in this blogpost here: http://happygiraffe.net/blog/2009/04/30/java-jar-blats-your-classpath/
Did you try adding a specific folder to the classpath during startup and then add your jar file to the folder at later point ?
How can I run a jar that needs several jars to work?
Let me explain, I have for example a project with a jar "Main.jar" but to run this Main.jar I need jdom.jar(for xml file), jGit.jar...
Assume we need more than two jars. How can I run my Main.jar?
By including the needed jar files in the classpath. Something like:
java -cp "Main.jar;jdom.jar;jdom.jar" MainClass
If you are under Windows and would like to execute the Main.jar with a double click, you will need to create a .bat file and use that one instead to run your program. The content of the .bat file will have the above command.
Under Unix/Linux you will create a shell file with the similar content.
Note that the -cp argument values will need to contain all the jars that your Main.jar is depended on.
Run the jar with main class and add all other jars to classpath.
java -cp yourJars yourClass
see this post for more info. See this java tutorial
I have created an executable jar file that contains two main classes. All libraries are included in the jar and the main Main-Class works fine when executing like this:
java -jar MyApplication.jar
But when I try to run the other main class like this:
java -cp MyApplication.jar my.other.mainClass
It does not include the classpath of the manifest.mf and it can not find the libraries that are in the jar file.
Is there a simple way so that the other main class can use the classpath from the manifest.mf? or should I create two separate executable jars?
You could write a class that invokes the main method of whatever class is passed as its first argument using Reflection - and configure this as the Main-class in your jar. This way you can invoke multiple main methods from the same jar with java -jar file.jar my.other.mainClass
Is there a simple way so that the other main class can use the classpath from the manifest.mf? or should I create two separate executable jars?
The JAR manifest classpath is only used if you use -jar option, and conversely the command line argument is only interpreted as a classname if -jar is NOT used. You cannot mix the two approaches.
However, this doesn't mean you have to create a second JAR file. For instance, you could write a simple shell script to launch the JVM using the classpath copied from the manifest and the secondary entry point classname.
Are you sure your problem is with libraries within the jar? What version of java are you using?
I suggest you try the following:
java -cp MyApplication.jar <add external libraries here> my.other.mainClass
So you only need to add paths to classes that are not already in the jar. You can use wild cards to shorten the list.
Here is another interesting option, Enable your unrunnable JARs to run with the java -jar command. It describes how to select a main class in the jar file and make another runnable copy.