I tried double clicking it, I didn't get anything, I tried opening it in CMD, I got this:
no main manifest attribute, in MyRestaurant.jar
What should I do ? :(
When you create a JAR you need to specify which class has the main method you want to run, this is done by adding a manifest.txt as a file to the jar you create. It seems thats the thing its missing.
Create a manifest.txt file, just add one line Main-Class: ClassName followed by enter and add that file when you create your jar.
ex. -jar -cvmf manifest.txt app.jar ClassName.class
If you are using Netbeans IDE, the first time you complied, choose Run then Netbeans will ask you the main class. From next time, you can just Clean and Build.
Related
I am a bit confused about the process to create a .bat file of a java application. I have exported the executable jar using IDE say Application.jar in C: directory. Then I have written two lines in a .txt file as stated below and saved it as .bat file in the same directory where i have my application.jar. But on double click of the .bat file, the application is not getting executed.
.BAT file code
javac Application.java
java -cp . Application
Note: I have also set the JRE and JDK path in my environment variables till bin path in My Computer properties. But it is not working. Can someone suggest me how can I fix this, because I want to execute my code by doubleclickng on a .bat file. It will be nice if someone can provide me every step I need to follow to accomplish this as I havent ever done this before.
Thanks ,
The first line in your batch file is attempting to compile your program !?
The second line is attempting to run the Application.class file.
What you want if you have produced an executable jar file is:
java -jar Application.jar
But you don't really need the batch file at all. If you double click on the jar file and it runs your program then you can just create a shortcut to it.
Your .bat is just fine. When you double click it might be executing and then closes. This is because your program might not have any UI and it isnt waiting for any input. To verify this take a command prompt and then execute your bat file via that.
In other case I assume that you have a java class called Application and you need to run this via a batch file. In that case if the class have a main method then you just need one line in .bat file
java -cp <the path to class file> Application
So you might be using a javac just to take advantage of class path as current directory. So when you say
javac Application.java
java -cp . Application
It compiles the class to current folder and set that as class path and then execute. This is absolutely file as long as the Application.java doesnt have any third party dependency. But in this case again you need not set -cp to . (current directory will be taken as classpath automatically unless otherwise specificed). So below will also work fine.
javac Application.java
java Application
I support Jurgen reply. If you have an executable jar file and a jre in path then double clicking it will run the application. The META-INF folder inside the jar will have a MANIFEST.MF file which uses a property called Main-Class: to specify the main executing class. And on double clicking this class gets executed. However its only useful if you have a UI. Else it'll also have no effect.
In all these context the Application.jar you mentioned is irrelevant. If that is a third party jar that you need to run the you should include that in -cp argument.
So I made this (very simple) program with a swing GUI with NetBeans, and I clicked build to make a jar file. When I run it by double clicking it, it tells me it could not find the main class, which, after checking, I am sure is definitely there. But, when I run it from Command Prompt, it works perfectly. Any easily-determinable reason for this strange behaviour (if you want the source code, I can post it here)?
The things that seem to be needed in NetBeans are:
The project has to be the Main Project (by right clicking on it in the Projects list).
You have to set the Main Class in the project properties. (Right click, Properties, Run, Main Class.)
Then when you right click on your project and do a "Clean and Build", a jar will get built into the dist subdirectory.
If that fails to fix the problem, here's a longer story...
When you double click a jar file to run it, the operating system acts as if you had typed this from the command line:
java -jar filename.jar
(When you say it works for you from the command line, is this what you're typing?)
At that point, the Java executable looks for a file inside the jar named META-INF/MANIFEST.MF. And then in the contents of that file, it looks for the value of a property, Main-Class. And finally it looks for the class of that name in your jar and runs its static main(String[]) method.
So if your jar is failing to run, you can do the following to debug what's going on:
Clean and rebuild your project in NetBeans.
Double check that your class(es) are actually in the jar:
Start a command prompt
cd into the dist subdirectory of your project.
Use a command like jar tf filename.jar to list what's in there.
Double check that the MANIFEST.MF file is correct:
Again in a command prompt
cd into the dist directory.
Use a command like jar xf filename.jar META-INF/MANIFEST.MF to extract the manifest.
Look at the contents of that file (e.g. type META-INF\MANIFEST.MF) and make sure Main-Class is set to the appropriate class.
If all of the above check out, then double clicking the file should work.
Have you set the containing project as the "Main Project"?
I've been learning about JAR files and wanted to try and create and run one myself. I carried out the following steps:
Created a project folder with a 'source' subfolder and a 'classes' subfolder
I wrote 2 source files, one with a main method which creates an instance of the other class and runs a simple method in it.
Compiled these to the 'classes' subfolder. I checked to see if they would run. They did
I created a manifest.txt file and filled in the Main-Class: xxxx and hit the return key. I saved this in the sources subfolder
Created a jar file in the classes subfolder by writing
jar -cvmf manifest.txt zzz.jar *.class
Tried to execute the jar file by typing
java -jar zzz.jar
This gives a ClassNotFound exception. If I try to execute the jar by double clicking on it in windows I get an errorbox saying "Could not find the main class xxxx"
I've double checked the spelling of the class inside the manifest file and it's correct.
Possibly important: I have to compile my programs using java -cp . xyz as there is an issue with my classpath. Does this mean that I need to execute jars in a different way as well? I tried
java -cp . -jar zzz.jar
but ended up with the same exception.
Edit: I ended up starting from scratch and now it runs (with the basic -jar zzz.jar command). Frustrating that I don't know what I was doing wrong but glad that it is working!
Shouldn't number 5. be run in the classes subfolder, where all your class files are? And if your classes are in packages, which they should be, you'll likely want to use * instead of *.class..?
To check what your jar file contains you can run:
jar tf zzz.jar
You will probably have to supply the entire path of the .class file you wish to execute after the classpath. ie java -cp xxx.jar classes.mainProgram.class. Where classes is the name of the folder which contains your class files.
I am using NetBeans to build my executable JAR and haven't messed around with any Ant or project settings (using defaults).
When I go to Run >> Clean and Build Project, I see NetBeans building my JAR and putting it into my project's dist/ directory without a hitch.
When I double-click that JAR to run it, I get the following error message:
Could not find the main class: com.me.myorg.MainApp. Program will exit.
If I open up the JAR in WinRAR and extract the META-INF/MANIFEST.MF file, I see the following attribute defined:
Main-Class: com.me.myorg.MainApp
Obviously, com.me.myorg.MainApp is the "head" of my GUI (Swing) app.
Any ideas as to what is going on? How to troubleshoot? Thanks in advance.
Try in command prompt(Windows):
ftype jarfile="C:\PATH_TO\javaw.exe" -jar "%1" %
EDIT
Run the command prompt like this: Go to Start and in the Search box type CMD. Right click the Command Prompt icon and choose Run as Administrator.
http://www.howtogeek.com/howto/windows-vista/run-a-command-as-administrator-from-the-windows-vista-run-box/
Then run the command above.
Is there a new line after the main class declaration?
Main-Class: com.me.myorg.MainApp
# empty line here
The manifest file format requires each line to end with a line separator. If the line with main class is the last line in the file it is ignored.
I've just made a game in netbeans. The Problem is that after builting the game. I'm not able to execute the jar file and getting the exception:
Failed to load Main-Class manifest attribute from
Game.jar
What to do???
For a JAR to be self-executable, you have to include the Main-Class line in a manifest.
I'm not a NetBeans user, but this is how it's done.
Create a manifest.mf file:
Main-Class: YourGame
<newline>
Build the jar:
jar cmf manifest.mf Game.jar path/to/classes/*.class
You should now be able to to double-click on the JAR to run it (assuming Windows), or you can run it via the command line:
java -jar Game.jar
Of course, you can always run from the command line without the need for a manifest:
java -cp .;Game.jar YourGame
The Main-Class attribute needs a new line after it in order to be parsed correctly.
Show your manifest.mf,
Edit manifest file as proposed by others, or in NetBeans just right-click the project (in sidebar), select Properties, category Run and hit Browse... next to Main Class.