"Could not find the Main class" when opening a .jar file - java

When I try to open a .jar file by double-clicking it I get this message that "Could not find the Main Class", however it works when opening it with the command line.
I've been looking for a solution for some days, I found a lot of people asking the same questions, some of the answers I found suggest that the main class is not declared in the manifest.mf, however I checked it and it is declared, here is my manifest.mf
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.7.0_13-b20 (Oracle Corporation)
Class-Path:
Main-Class: my_pack.Mainprog
[empty line]
[empty line]
I tried to run the jar that NetBeans builds, also with the fat jar plugin for eclipse and always get the same error.

What I would do is:
Create a dummy project in your Netbeans and export it as a runnable jar. Then take your manifest and the dummy-project manifest and compare it.
Manifests are really a pain in the code. They need to be absolutely correct formatted. Take care on trailing whitespaces, missing/additional empty rows etc...
Maybe check the Java Version, you are using. Make sure, that your command line uses the same one, like setup in your environment variables. Maybe there are doubled declarations...
Which java.exe is associated with *.jar files in windows explorer? Try exactly THIS java.exe to run your jar from the CLI. Same issue then?
Good luck!

Related

JAR file of java application is not working

I have written a java application in eclipse and then I have made its runnable JAR file, but the problem is that the application is not opening when I click its JAR file to open it, but the application is opening and running perfectly when I open it in the eclipse, I have made the JAR file number of times again and again but I am facing the same issue every time, no error is shown when I click the JAR but it is not opening.
So please tell what can be the problem with the JAR file.
Any number of things can be wrong with a jar file that prevents you from being able to run it after you create it. Some things to check:
Do you have java associated with your jar file?
Did you specify a main class when you built your manifest?
Are there dependent library jars your application depends on that did not get added to the classpath? The manifest file in your jar file should tell you any additional classpath entries that were generated.
Do you get an exception when you run it from command line?
You can run it two ways:
java -cp [path to your jar] your.package.MainClass
java -jar [path to your jar]
See if any of these are your issue and it should point you to the solution to your problem.
When you generate a JAR file, it doesn't mean something is going to happen, maybe it is just a library you generated, a reusable module of the system, etc.
"JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file to distribute application software or libraries on the Java platform." - Wikipedia
So, Make sure you have a main class in your manifest, for example:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 16.3-b01 (Sun Microsystems Inc.)
Main-Class: com.example.MainClass
Class-Path: lib/lib1.jar lib/lib2.jar

Creating a JAR file and running on Raspberry PI

This is a very common question and there are chances that this might be marked as Duplicate, but even after reviewing a lot of answers and posts from stackoverflow and other communities, the problem just doesn't solve.
I have created a Project in my NetBeans IDE 7.3 and I Build the Project to get the JAR file by pressing SHIFT+F11. The next step, I perform is through WinSCP I copy the JAR file from my Windows Machine to the directory (/home/pi) in Raspberry PI.
Now, I am in the directory in my Raspberry PI where the JAR file is present and in the terminal I run the command
java -jar ProjectFinal.jar
I get the error "Could not find or load main class".
The contents of my JAR are as shown below (in the form of directories):
lib (contains all the libraries)
META-INF (contains a MANIFEST.MF file and the contents looks fine) and the contents are shown below:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.7.0_17-b02 (Oracle Corporation)
Class-Path: lib/super-csv-2.1.0.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: projectfinal.ProjectFinal**
and finally projectfinal (contains all the class files). I tried to change the contents of the MANIFEST.MF file by following this link. But this couldn't help me either.
Please suggest me any ideas that could get me out of this. It could be really helpful. I can share the code if it is needed.
I am using JRE version 7.
Any questions/inputs/ideas is deeply appreciated.
Please help.
Thanks a lot in advance.
I don't know about NetBeans but Eclipse IDE has an option to export into a "Runnable JAR File" instead of a normal JAR file.
I had the same issue and it solved my problem.
If not then follow this:
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.

Need help packing an executable .jar in java

Ok, I have this file I use for debugging purposes while I'm writing some code. However, I am looking to speed up the process by packing it in an executable .jar file. I have been creating a jar file via eclipse, and attempting to run it as is.
The contents of the jar (Derivitave Quiz.jar) are as follows:
DerivQuiz.java
DerivQuiz.class
Meta-INF (empty folder)
pass.txt
Also, would I have to re-write it to use a non-command line output, such as Jpane/JOptionPane, instead of System.out.print()?
My current code works fine in Eclipse, but not after porting to a jar.
I think you are making this far too complicated, especially if you're flummoxed by a task as simple as creating a JAR file.
All you need is javac.exe and jar.exe that come with the JDK. Look at this tutorial to show you how to do it properly:
http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/jar.html
I did have to change all my print statements to use JOptionPane.showMessageDialog() instead of System.out.print().
Upon further inspection, the jar was being created without a manifest file, not allowing the jar to run.
I fixed this by adding a manifest file under META-INF/manifest.xml, with the following code:
Manifest-Version: 1.0
Created-By: 1.6.0_27 (Sun Microsystems Inc.)
Main-Class: MCExploit
Adding this allows the jar to run flawlessly, in addition to the other adjustments I made.
Duffymo's answer is good. But let me add something: if you're using Eclipse, you simply go to File -> Export then choose 'Executable Jar', and it does it automatically. Here's a nice guide for doing it a few different ways (including with Eclipse):
http://viralpatel.net/blogs/2008/12/create-jar-file-in-java-eclipse.html

Find Fault in Java Program Launch?

I created a quick program in Java just to get started with the GUI-based elements of Java before I start writing the actual program. In Netbeans, everything compiles perfectly, and the GUI has all of the elements I put into it, so there is not problem when I am using Netbeans.
However, upon building and packaging into the JAR, I have no result on open. I have tried to use Webstart and everything else I could think of, but I get no different result. The JAR does, however, change size along with the varying of my program, so I know something is working, I just must be missing a file or something of that nature.
EDIT:
The manifest says the following after the build (This is inside the JAR):
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_26-b03 (Sun Microsystems Inc.)
Class-Path:
X-COMMENT: Main-Class will be added automatically by build
Main-Class: com.nmagerko.Execute
I have found the answer.
Using help from #Mocker Tim, I found that using java -jar program-name.jar allowed for details that Netbeans did not catch.
This ended up being the fact that my path to my icon had one too many "/"'s, and removing that one slash allowed for the program to be jarred correctly.

NoClassDefFoundError with Executable Jar

Probably a very usual problem with exec jar. I am trying to create and run an executable jar and its driving me crazy.
I have couple of classes(with package stmts), one of which has a main method, a simple empty constructor and ofcourse few biz methods, these form part of a small eclipse project.
I am bundling both these and a manifest file into a jar(arc.jar) using a build.xml. My program uses jdom library and also references from one of my other eclipse projects, so I am including both the jdom libray and other biz library while building my arc.jar.
This is the Manifest.MF I wrote, there is a new lineafter Main-Class
Manifest-Version: 1.2
Class-Path: jdom.jar other.jar
Main-class: uk.co.Art
When I unzip this arc.jar, it contains jdom.jar, other.jar, META-INF/Manifest.mf and the package with my classes.
Funnily, the Manifest file in jar looks like this -
Manifest-Version: 1.2
Created-By: 14.2-b01 (Sun Microsystems Inc.)
Main-class: uk.co.Art
Class-Path: jdom.jar other.jar
I got the ClassNotFoundException ClassNotFoundException: uk.co.Art when the value of Main class was without the package. With package the error changed to ClassNotFoundException: org.jdom.JDOMException.
So just for testing purpose, I tried giving the fullpaths with c:\ for both my executing class and the jars but still it is not able to locate the main class, errors out ClassNotFoundException: uk.co.Art.
I cant seem to understand why.
The command line I am using is - java -jar Arc.jar
The version on cmd prompt is -
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode)
while the eclipse installed jre is jre6
What can you folks suggest ?
Alex' solution did solve the problem for local execution.
I am also thinking of another way(as an extra option) of execution, to deploy the Arc.jar on server and run a batch on that machine which basically sets the java classpath and then calls either the jar or directly the class whichever way it works. But its only an option, not a preferred one though.
Or, also, set that classpath in my manifest and build jar locally and deploy on the server.
Would like to try out a few things though. But now I understand that the basic problem was jars into jar and hence classpath issue.
Thanks a lot folks.
You cannot bundle other JAR's in a JAR. They have to go in the same folder or at least in a folder relative to the Arc.jar. You specify then its relative path in the Class-Path entry.
Eclipse however has an useful export tool which allows you to embed the contents of other JAR's in your JAR so that you don't end up with multiple loose JAR's which are required to be placed in a specific location. Check the 2nd Library Handling option when you choose Export > Runnable JAR file.
When choosing Package required libraries into generated JAR, then Eclipse will add a special classloader which will load those JAR's transparently for you before executing the main class. It's doing that with help of JarRsrcLoader.
You do not have to pack your additional jars into your jar. These jars should be in the same directory as your application jar. By other words: put all jars into one directory and run. Everything will work.
Reasons: think what does executable jar mean. If you jar is not "executable" you run it as following:
java -cp art.jar;ldom.jar;other.jar uk.co.Art
when your jar is executable you use the following command line:
java -jar art.jar
No magic: jvm opens manifest and takes name of main class and other jars from there and virtually creates command line like first one. Now you understand that all jars in your case must be in the same directory.
Maybe the .jar is corrupt, like sometimes happened to me. Take a look at this.
Just couple of things to be noted when exporting.
JRE associated to the project is consistent with the JRE that would be used to run it.
Please export your project into a local drive, then move it into any shared drive (this fixed my issue).
Thank you.

Categories

Resources