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
Related
So, when I write my java file:
public class Program
{
public static void main(String[] args)
{
System.out.println("Serious business logic.");
}
}
Then in windows cmd, I compile this way:
javac Program.java
jar cfe Program.jar Program Program.class
java -jar Program.jar
It's fine, and the result is:
"Serious business logic."
When in Netbeans I create a Project, it adds this line:
package program;
And I can not compile in cmd, only inside the IDE.
I've tried manifest.txt, UTF8 encoding without BOM, plus linebreak at the and of file.
Manifest.txt:
Main-Class: program.Program
"jar cvfm Program.jar Manifest.txt Program.class"
and without manifest.txt, just in cmd program.Program
"jar cfe Program.jar program.Program Program.class"
When I tried to run:
java -jar Program.jar
it results in:
"Error: Could not find or load main class program.Program"
I've already checked the following websites:
http://www.skylit.com/javamethods/faqs/createjar.html
https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
and haven't got any idea how to do. Could you please help me?
How do I compile with package keyword? What is the proper entry point?
Thanks!
(ps jre1.8.0_91 ; jdk1.8.0_66 should I use same 32 or 64 bit for both of jre and jdk?)
Make sure when you are compiling your program as a JAR, Program.class is inside of a folder called program. The package keyword that Netbeans has added at the beginning of your script is telling the executable that it is inside of a folder called program. If you are just adding the class file without making sure it is in the correct package (folder), it will not run properly because it does not know where to find it. Your command should be changed to:
jar cvfm Program.jar Manifest.txt program
where program is a folder containing Program.class. Your manifest may be left alone but also needs to be included with compilation.
I wrote a HelloWorld class in a folder "D:\Workspaces\Workspace\Packaging Programs into JAR Files\src". I am trying to create a Jar file containing this HelloWorld.class file, following this tutorial. So I opened cmd in the src directory and executed the following command.
jar cv HelloWorld.jar HelloWorld.class
I got this:
D:\Workspaces\Workspace\Packaging Programs i
nto JAR Files\src>jar cv HelloWorld.jar HelloWorld.class
HelloWorld.jar : no such file or directory
δ╗£H ♦ META-INF/■╩ ♥ ☻ PK♥ δ╗£H ¶ ME
K-*╬╠╧│R0╘3ασr.JM,IM╤u¬♦ ÿδ↓─¢[*h°↨%&τñ*8τ↨§Σ↨%û òk≥r±r☺ P╖îqëC D PK♥
c╖£H ► HelloWorld.classmPMK├#►}█|515╡╡⌡│P☼B¶1ŧ/éx(*D⌠α)iù▓%╔JL¶⌂û
▲¶<°♥ⁿQΓl¶é╨à▌Ö}≤▐╠█²·■°♦päü♥‼₧ì6VÜΦ8Φb╒B╧Bƒ┴<▬Ö(N↑4⌂∩åA?òS╬αìE╞/╩4µ∙u¶'äΦi$2å╛⌂
7₧GÅQÉD┘ï\d│æ↕:í,≤ ?‼è∞¥≤$æ╖2Oªçèφ┬B╙┬Üïul0┤¬≥┴≡I◄v,l║╪┬6⌡¿e♀φz╠e<τôΓ▼¶>?¶<
┌d∟♀6e♥èîó▒*♫÷B ↓ª╙¼#EYé√G▌%⌐BφW4:┌←⌠ÜεT]5Ω¬‼╥¬ª,* P♥ èY!☺ ⌐☺ PK δ╗£H ☻
♦ META-INF/■╩ PK☺☻¶ δ╗£H╖îqëC D ¶ = ME
TA-INF/MANIFEST.MFPK☺☻¶ c╖£H♥ èY!☺ ⌐☺ ► ┬ HelloWorld.classPK♣♠
♥ ♥ ╗ !☻
D:\Workspaces\Workspace\Packaging Programs i
nto JAR Files\src>
Why is this happening? Did I make a mistake?
You are missing the -f parameter which tells the JAR tool to output to a file rather than the console:
jar cvf HelloWorld.jar HelloWorld.class
Update:
You could achieve the same result as above without the -f flag by telling JAR to send the output to the standard output, and then redirecting into the actual JAR file you want. Hence, the following would accomplish the same as above:
jar cv HelloWorld.class > HelloWorld.jar
It should be noted that piping the output from JAR into an output file is a feature of the OS and not a feature of Java.
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'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 have created a runnable jar file that contains the following classes:
Main.class (This contains the main method)
Form.class
WrapLayout.class
The manifest file (Manifest.txt) contains the following:
Main-Class: Main
(It has a new line at the end)
I have created the jar file using this command:
jar cfm Organizer.jar Manifest.txt Main.class Form.class WrapLayout.class
The jar file is created, but when I run it, the command window opens up for a split second, closes, and nothing else happens. The file is a swing application, and I am running Windows XP on Parallels Desktop for Mac, but I don't think that matters. Does anyone know what's wrong?
EDIT: I tried running the jar file in mac, and I got this error message in the console:
Exception in thread "main"
java.lang.UnsupportedClassVerionError: Main : Unsupported major.minor version51.0
follow these steps an test:
Start Command Prompt. Navigate to the folder that holds your class
files: C:>cd \mywork
Set path to include JDK’s bin. For example: C:\mywork> path
c:\Program Files\Java\jdk1.5.0_09\bin;%path%
Compile your class(es): C:\mywork> javac *.java
Create a manifest file: C:\mywork> echo Main-Class: YourMainClass >manifest.txt
Create a jar file: C:\mywork> jar cvfm YourMainClass.jar manifest.txt
*.class
Test your jar: C:\mywork> YourMainClass.jar