I have performed the following steps:
A single java file called ComputeArray.java, with no specified
package
Navigated into the folder containing the program
Created the class file (and it runs)
The command I am having trouble with is as follows:
jar cvf computearray.jar ComputeArray.class
And I have tried using a manifest with cvmf and also tried adding ComputeArray.java at the end. I always end up with the same error "no such file or directory
You also have to specify the manifest file while creating jar file, something like:
$ jar cmf Hello.mf Hello.jar Hello.class Hello.java
Here is great documentation to Distributing your Application as an executable JAR file: https://introcs.cs.princeton.edu/java/85application/jar/jar.html
I have an executable jar file i.e. main class is bundled in the jar with manifest entry.
I was able to run the jar file with the following command:
java -jar myApp.jar
Which is able to pick my main class.
Because of some libraries missing in the executable jar file, I am getting ClassNotFoundException.
So, to fix the class not found errors, I tried to pass few more jar files as classpath to the java command as below:
java -classpath /Users/lib/httpclient.jar:/Users/lib/commons-logging.jar -jar myApp.jar
But my main class is not able to find the jar files specified in -classpath argument.
Added print statement in my main class as below:
System.out.println("Classpath: " + System.getProperty("java.class.path"));
This print statement is printing the classpath as below
Classpath: myApp.jar
Why I am not able to see the other jars(httpclient.jar & commons-logging.jar) in classpath? How to add those jar files to classpath for executable jar file in mac terminal?
I have a java file simpleMail.java and a jar file java-mail-1.4.jar (which is used in simpleMail.java). What I want to do is make an executable jar file out of these files and run on cmd.
Step 1. create manifest.txt file as
Main-Class: Main-Class-Name
Step2. compile all classes.
Step 3. run JDK's jar.exe utility like:
jar cvfm JarName.jar manifest.txt .class
cvfm is create a jar; show verbose output; specify the output jar file name; specify the manifest file name. (.class means all class files in the current directory.)
Step 4. specify jar dependency on classpath.
For detail refer http://www.skylit.com/javamethods/faqs/createjar.html
When I create a jar file in a subdirectory, the BouncyCastleProvider class from bcprov-jdk15on-159.jar fails to load with a ClassNotFoundException. I would think that the location where a jar file is created should have no impact on its contents and behavior.
Here is the example of creating a working jar.
$ jar cfm MyProject.jar Manifest.txt Main.class bcprov-jdk15on-159.jar
$ java -jar MyProject.jar
hello provider: BC version 1.59
And here is the example where running jar with exactly the same input files, but a different jar file destination, results in a failing jar.
$ jar cfm dist/MyProject.jar Manifest.txt Main.class bcprov-jdk15on-159.jar
$ java -jar dist/MyProject.jar
Error: Unable to initialize main class Main
Caused by: java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
This is the file Manifest.txt
Manifest-Version: 1.0
Main-Class: Main
Class-Path: bcprov-jdk15on-159.jar
and this is the Main.java file that uses the BouncyCastleProvider class.
public class Main {
public static void main(String... arg) {
java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
java.security.Provider p = java.security.Security.getProvider("BC");
System.out.println("hello provider: " + p);
}
}
I see this behavior both with JDK 8 and with JDK 9, and also both with the JDK jar command (shown above) and with Ant's jar task.
I stumbled on this problem while trying to upgrade the PCSecrets password manager to work under Java 9.
The reason the above fails is that Java will not load bcprov-jdk15on-159.jar from within the jar file, but from the class path, which seems to differ according the the location of invoked jar. When the generated jar file is in the same directory as bcprov-jdk15on-159.jar, it loads it from the directory and the command invocation works fine. When the generated jar file is in another directory, it fails to load bcprov-jdk15on-159.jar and throws the ClassNotFoundException. Solutions for including a jar within a jar are provided in this question. Interestingly, this answer, upvoted 26 times, is based on the same mistaken assumption.
I found the answer by comparing the binary images of the generated jar files. They seemed to differ only in the timestamps of the META-INF/MANIFEST.MF file. By generating the two files in parallel with the command jar cfm dist/MyProject.jar Manifest.txt Main.class bcprov-jdk15on-159.jar & jar cfm MyProject.jar Manifest.txt Main.class bcprov-jdk15on-159.jar I had two bit-identical jar files that still misbehaved. This prompted me to look at the environment of the two files' execution, rather than the files themselves.
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.