i am want create jar of my project using cmd line.
I am using ini4j libs.
Everything compiles fine, but I do not know how to set the -cp to the library.
Compile:
javac -cp ".;lib/ini4j-0.5.2.jar;ini4j-0.5.2-jdk14.jar;lib/ini4j-0.5.2-jdk14.jar" gui_Frame/*.java
Create jar:
echo Main-class: gui_Frame/MainApp > manifest.txt
jar cvfm GVE.jar manifest.txt gui_Frame/*
But, if i want start java -jar GVE.jar i get following error:
Java.lang.NoClassDefFoundError: org/ini4j/wini
What am I doing wrong ?
You must specify the same classpath when running java as you did when compiling.
Or bundle all the necessary class files in one JAR.
==EDIT==
Try this:
java -cp "GVE.jar;.;lib/ini4j-0.5.2.jar" gui_Frame.MainApp
I have a java project that has 10 .java files. A1.java, A2.java......A9.java and Main.java.
The project contains a package grand.big.medium.small and all the .java files are inside this package.
I have compiled and created an executable jar file for the project using:
javac grand/big/medium/small/*.java
to run the project I used the command
java -cp . grand/big/medium/small/Main
to create the jar I used the command
jar -cvmf manifest.txt MyJAR.jar *.class
I want to run the jar file from command prompt and I am using the commad
java -cp . -jar grand/big/medium/small/MyJAR.jar
and I getting an error:
Error: Could not find or load main class Main
please can anyone tell me why I am getting this error?
Run as:
java -cp . -jar grand/big/medium/small/MyJAR.jar grand.big.medium.small.Main
Assuming you have grand as a directory within current directory and directory hierarchy follows.
I have created a GUI project and compile the program using CMD.
For Creating .jar File, I created manifest.txt and made the jar by using following command:
jar cmf manifest.txt Client.jar *.class
this created a .jar file but when I open the Client.jar a Window pops up saying
"A JAVA Exception has Occurred" but when I run the jar from CMD
java -jar Client.jar
It runs properly from CMD but not by clicking on it directly.
What Could be wrong ....?
This is my manifest.txt
Main-Class: ClientTextEdit
\n
\n
\n----> newline
these are the .class files:
ClientTextEdit.class
ClientTextEdit$1.class
ClientTextEdit$2.class
ClientTextEdit$3.class
ClientTextEdit$4.class
ClientConnection.class
I've written a java program that reads an excel file with jxl.jar. It's currently working, but I have to use cmd to run the program. Double clicking the jar file does not appear to be working. These are the commands I use to compile and run the code:
javac -classpath C:/workspace/jxl.jar:. main.java GUi.java
jar cvfm run.jar manifest.txt Main.class GUI.class GUI$1.class GUI$2.class GUI$3.class Main$1MyCustomTableCellRenderer.class Main$1YourTableCellRenderer.class Main$MyCustomTableCellRenderer.class
java -cp run.jar Main
I'm not really sure why it's any different from double clicking it. I've compiled the jxl file into the run.jar file so I don't understand why it doesn't work?
An Example directly from Java Tutorial on Adding Classes to the JAR File's Classpath as suggested by #MadProgrammer in his comment.
We want to load classes in MyUtils.jar into the class path for use in MyJar.jar. These two JAR files are in the same directory.
We first create a text file named Manifest.txt with the following contents:
Class-Path: MyUtils.jar
Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
We then create a JAR file named MyJar.jar by entering the following command:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
This creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0
Class-Path: MyUtils.jar
Created-By: 1.7.0_06 (Oracle Corporation)
The classes in MyUtils.jar are now loaded into the class path when you run MyJar.jar.
Read more...
Try this in command prompt:
Run the below command if you bundled the jar with all libraries
java -jar MyJar.jar
If you need to add jars at the time of execution please add all the jars at command line, as follows;
java -cp /path/jxl.jar;myJar.jar Class-Name-Main
I tried to put HelloWorld in a .jar file and running it, but it doesn't work. I created the java file and typed in the program, and then wrote in cmd:
javac HelloWorld.java
java HelloWorld
and it worked. Then I entered
echo Main-Class: HelloWorld >manifest.txt
jar cvfm HelloWorld.jar manifest.txt HelloWorld.class
and got the output
added manifest
adding: HelloWorld.class(in = 426) (out= 288)(deflated 32%)
I then entered
java -jar HelloWorld.jar
HelloWorld.jar
and the first line worked, while the second line gave me an error:
Error: Could not find or load main class path\HelloWorld.jar
which is the same output I got (in a rapidly closing window) when I tried to open it with the java.exe file in 64 bit jre7\bin, jdk1.7.0_51\bin, jdk1.7.0_51\jre\bin, as well as 32 bit jre7\bin. I've uninstalled and reinstalled both my jre and jdk and recreated my .class and .jar files, but the problem persists. I'm on win8.
Edit: I tried to do as aetheria suggested, but no luck. I put HelloWorld.java in path\com\stackoverflow\user\blrp, compiled it, and it worked by entering
java com.stackoverflow.user.blrp.HelloWorld
in path. I then created the manifest and jar by:
(echo Manifest-Version: 1.0
echo Class-Path: .
echo Main-Class: com.stackoverflow.user.blrp.HelloWorld) >manifest.txt
jar cvfm HelloWorld.jar manifest.txt com\stackoverflow\user\blrp\HelloWorld.class
and got the output
added manifest
adding: com/stackoverflow/user/blrp/HelloWorld.class(in = 454) (out= 312)(deflat
ed 31%)
but still, java -jar HelloWorld.jar worked and HelloWorld.jar didn't (same error). I also tried not doing the package thing, just the Class-Path in manifest, same result.
(Also, I solved the problem prior to asking the question by use of a .bat file, but it'd still be sweet to get that jar working.)
Thanks jbaliuka for the suggestion. I opened the registry editor (by typing regedit in cmd) and going to HKEY_CLASSES_ROOT > jarfile > shell > open > command, then opening (Default) and changing the value from
"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
to
"C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*
(I just removed the w in javaw.exe.) After that you have to right click a jar -> open with -> choose default program -> navigate to your java folder and open \jre7\bin\java.exe (or any other java.exe file in you java folder). If it doesn't work, try switching to javaw.exe, open a jar file with it, then switch back.
I don't know anything about editing the registry except that it's dangerous, so you might wanna back it up before doing this (in the top bar, File>Export).
I found this question when I was looking for the answer to the above question. But in my case the issue was the use of an 'en dash' rather than a 'dash'. Check which dash you are using, it might be the wrong one. I hope this answer speeds up someone else's search, a comment like this could have saved me a bit of time.
You can always run this:
java -cp HelloWorld.jar HelloWorld
-cp HelloWorld.jar adds the jar to the classpath, then HelloWorld runs the class you wrote.
To create a runnable jar with a main class with no package, add Class-Path: . to the manifest:
Manifest-Version: 1.0
Class-Path: .
Main-Class: HelloWorld
I would advise using a package to give your class its own namespace. E.g.
package com.stackoverflow.user.blrp;
public class HelloWorld {
...
}
Had this problem couldn't find the answer so i went looking on other threads, I found that i was making my app with 1.8 but for some reason my jre was out dated even though i remember updating it. I downloaded the lastes jre 8 and the jar file runs perfectly. Hope this helps.