Hi all,
I have about 10 jars and when I run my program I invoke the following:
java -Xmx1024m -cp a.jar;b.jar;c.jar;whatever.jar -Dfile.encoding=UTF-8 [package][class]
I want to make this as an executable jar. I thought that when I unjar (that sounds weird, but I'm using java command - not using general zip program) the jar containing [package][class], update manifest.mf and rejar it. Unfortunately, this did not work.
Is it possible for me to make it as an executable jar or I should unjar it and sum it all?
You just need this line in your manifest file
Main-Class: my.package.Main
Then run the jar command with the m flag, and give it the name of your manifest file:
jar cmf manifest main.jar *.class
Unfortunately, jars can't contain other jars. Give the jar containing your entry point a Main-Class, and then set the classpath so that the other 9 jars are accessible.
Jar is a collection of files. To make a jar an executable the most important thing is that it should contain a class that has the main() method. So, you can make a jar executable only if contains a .class file that has a main method. That class will look like this.
public class AClass
{
public static void main(String[] args)
{
...
}jar
...
}
Then and only then can you make a jar executable.
If the main() method class is present. Then to make the jar executable do this.
Extract the jar
In the manifest file add this line Main-Class:AClass and don't forget to press enter after this line. follow this link
Rejar the class files. You got the executable jar now
To execute the jar, (suppose you made a.jar executable), then run java -jar a.jar to run the jar file.
Second thing is that the command that you have posted in the question does not require those jars to be executable. When you use -cp, then the parameters (i.e. a.jar etc..)are basically libraries or in other words, when java will look for a class file to find definition of a class or a function or whatever for that matter, it will look inside these jars if what it is looking for is not part of the java standard library.
Related
I'm learning Java and I have a problem. I created 6 different classes, each has it's own main() method. I want to create executable .jar for each class, that is 6 executable .jar files.
So far I tried
java -jar cf myJar.jar myClass.class
and I get 'Unable to access jarfile cf'. I'm doing something wrong but I don't know what. I'm also using Eclipse IDE if that means something.
In order to create a .jar file, you need to use jar instead of java:
jar cf myJar.jar myClass.class
Additionally, if you want to make it executable, you need to indicate an entry point (i.e., a class with public static void main(String[] args)) for your application. This is usually accomplished by creating a manifest file that contains the Main-Class header (e.g., Main-Class: myClass).
However, as Mark Peters pointed out, with JDK 6, you can use the e option to define the entry point:
jar cfe myJar.jar myClass myClass.class
Finally, you can execute it:
java -jar myJar.jar
See also
Creating a JAR File
Setting an Application's Entry Point with the JAR Tool
Sine you've mentioned you're using Eclipse... Eclipse can create the JARs for you, so long as you've run each class that has a main once. Right-click the project and click Export, then select "Runnable JAR file" under the Java folder. Select the class name in the launch configuration, choose a place to save the jar, and make a decision how to handle libraries if necessary. Click finish, wipe hands on pants.
Often you need to put more into the manifest than what you get with the -e switch, and in that case, the syntax is:
jar -cvfm myJar.jar myManifest.txt myApp.class
Which reads: "create verbose jarFilename manifestFilename", followed by the files you want to include.
Note that the name of the manifest file you supply can be anything, as jar will automatically rename it and put it into the right place within the jar file.
way 1 :
Let we have java file test.java which contains main class testa
now first we compile our java file simply as javac test.java
we create file manifest.txt in same directory and we write Main-Class: mainclassname . e.g :
Main-Class: testa
then we create jar file by this command :
jar cvfm anyname.jar manifest.txt testa.class
then we run jar file by this command : java -jar anyname.jar
way 2 :
Let we have one package named one and every class are inside it.
then we create jar file by this command :
jar cf anyname.jar one
then we open manifest.txt inside directory META-INF in anyname.jar file and write
Main-Class: one.mainclassname
in third line., then we run jar file by this command :
java -jar anyname.jar
to make jar file having more than one class file : jar cf anyname.jar one.class two.class three.class......
Put all the 6 classes to 6 different projects. Then create jar files of all the 6 projects. In this manner you will get 6 executable jar files.
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.
I'm learning Java and I have a problem. I created 6 different classes, each has it's own main() method. I want to create executable .jar for each class, that is 6 executable .jar files.
So far I tried
java -jar cf myJar.jar myClass.class
and I get 'Unable to access jarfile cf'. I'm doing something wrong but I don't know what. I'm also using Eclipse IDE if that means something.
In order to create a .jar file, you need to use jar instead of java:
jar cf myJar.jar myClass.class
Additionally, if you want to make it executable, you need to indicate an entry point (i.e., a class with public static void main(String[] args)) for your application. This is usually accomplished by creating a manifest file that contains the Main-Class header (e.g., Main-Class: myClass).
However, as Mark Peters pointed out, with JDK 6, you can use the e option to define the entry point:
jar cfe myJar.jar myClass myClass.class
Finally, you can execute it:
java -jar myJar.jar
See also
Creating a JAR File
Setting an Application's Entry Point with the JAR Tool
Sine you've mentioned you're using Eclipse... Eclipse can create the JARs for you, so long as you've run each class that has a main once. Right-click the project and click Export, then select "Runnable JAR file" under the Java folder. Select the class name in the launch configuration, choose a place to save the jar, and make a decision how to handle libraries if necessary. Click finish, wipe hands on pants.
Often you need to put more into the manifest than what you get with the -e switch, and in that case, the syntax is:
jar -cvfm myJar.jar myManifest.txt myApp.class
Which reads: "create verbose jarFilename manifestFilename", followed by the files you want to include.
Note that the name of the manifest file you supply can be anything, as jar will automatically rename it and put it into the right place within the jar file.
way 1 :
Let we have java file test.java which contains main class testa
now first we compile our java file simply as javac test.java
we create file manifest.txt in same directory and we write Main-Class: mainclassname . e.g :
Main-Class: testa
then we create jar file by this command :
jar cvfm anyname.jar manifest.txt testa.class
then we run jar file by this command : java -jar anyname.jar
way 2 :
Let we have one package named one and every class are inside it.
then we create jar file by this command :
jar cf anyname.jar one
then we open manifest.txt inside directory META-INF in anyname.jar file and write
Main-Class: one.mainclassname
in third line., then we run jar file by this command :
java -jar anyname.jar
to make jar file having more than one class file : jar cf anyname.jar one.class two.class three.class......
Put all the 6 classes to 6 different projects. Then create jar files of all the 6 projects. In this manner you will get 6 executable jar files.