Error: Could not find or load main class (double click) - java

I've just made a "runnable .jar" (or a normal .jar, where I set off manifest on my own. I tryed both) file from my java app with Eclipse.
When I try to open it by double click, i get this message: "could not find or load main class"
From Command Prompt I can run it with 'java -jar xy.jar'.
Works without any problems.
Can anybody help me?

Make sure your manifest file contains the main-class. Like this.
Main-Class: MyPackage.MyClass
You may want to let eclipse make your manifest just so you can see what it does, then save that manifest file and use it going forward. Also check the class path. You can specify that in the manifest also.
Here's a link to how to set the entry point in the manifest
You may also need to put the classpath in your manifest.
Manifest-Version: 1.0
Class-Path: MyUtils.jar
Created-By: 1.7.0_06 (Oracle Corporation)
That can be found here: Adding classpath to manifest

Related

fail to execute jar file in linux

I had developed a standalone program which don't have any GUI, it's only use in the linux machine(without any GUI). So, i converted my java source code into jar file using the following command
jar cfm hardcoded.jar manifest.txt hardcoded.class
The jar file was successfully created but when i try to execute the jar file in the terminal,i get this error
no main manifest attribute, in hardcoded.jar
Some of the information said that the problem due to the manifest file but i can't figure out where is the root cause because i am very new to java on linux,Some people said that the package also need to include but where can i find my package name?.My manifest file is showed as the following
Manifest-Version: 1.0
Class-Path: ./ commons-logging-1.1.2.jar httpclient-4.5.2.jar htt
pcore-4.4.1.jar java-json.jar java-rt-jar-stubs-1.5.0.jar javax-ssl-1
_1.jar joda-time-2.2.jar
Class-Path: .
Main-Class: hardcoded
As you can see my manifest file, i had some others external library, i know eclipse have the build in function to solve this issue, how i need to solve it in linux environment ?

Cannot find or load main class from .jar file

So I wrote a project in Eclipse and when I click on the green run button it works fine. When I export it into a jar and I double click on it a quick command prompt appears saying "cannot find or load main class...". I tried everything. I tried renaming registry keys, reinstalling java, changing environment variables and of course turning it on and off again. I read all the other threads on stack and tried everything they suggested. Here is my Manifest file:
Manifest-Version: 1.0
Class-Path: .
Main-Class: main.Main
My Main class is in a package called main.
I'd like to suggest to change the package name into something other than main.
don't forget to make a run configuration, which specifies the name of your main class.
package all the related other jar files into the same package.
run the executable.jar as java -jar YourExecutable.Jar
And if it does not solve your problem, just see my MANIFEST.MF file in my Executable jar file. Maybe it can give you some clue.
Manifest-Version: 1.0
Rsrc-Class-Path: ./ concurrentlinkedhashmap-lru-1.2.jar slf4j-api-1.7.
5.jar org.simpleframework.jar openflowj-0.3.7-SNAPSHOT.jar mongo-java
-driver-2.11.2.jar netty-3.9.0.Final.jar guava-15.0.jar org.restlet.e
xt.jackson.jar logback-classic-1.0.13.jar joda-time-2.2.jar org.codeh
aus.jackson.core.jar org.codehaus.jackson.mapper.jar openflowj-0.3.7-
SNAPSHOT-sources.jar org.restlet.jar logback-core-1.0.13.jar org.rest
let.ext.slf4j.jar org.restlet.ext.simple.jar openflowj-0.3.7-SNAPSHOT
-javadoc.jar
Class-Path: .
Rsrc-Main-Class: etri.sdn.controller.Main
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

Updating jar manifest file - java.io.IOException: invalid manifest format

I need to update the manifest in my jar file that I create (export) within Eclipse. I've tried to follow this explanation without success. I'm not quite sure what to specify on the command line. The Oracle web site is not too clear. I then found a post on SO that said to extract the manifest.mf file from the jar archive, update it, and add it back to the jar archive. I've tried that too, and it appears to work, however, at runtime, I get java.io.IOException: invalid manifest format. What is the correct way to update the manifest.mf to add new attributes? An example would be most helpful.
As manifest file is contained in META-INF subdirectory of jar file under the name MANIFEST.MF .Whenever you create a jar file for command prompt by the command
jar cvf Jarfilename FilesToadd
Then a default manifest file is created.
One can view this file and get an idea of valid Manifestfile.
In order to extract manifest file from jar type following command in cmd
jar xvf Jarfilename now a META-INF subdirectory will appear in the base directory from here you can view default manifest file.
Sometimes while updating manifest file we get java.io.IOException: invalid manifest format.This error comes because of following reasons:
1.You may have not left space between the name and value of any section in manifest file,
like Version:1.1 is inavalid section instead write Version: 1.1 that space between colon and 1.1 really matters a lot.
2.While specifying the main class you might have added .class extension at the end of class name.Simply specify the main class by typing Main-Class: Classname.
3.You may have not added newline at the end of file.You need not to write \n for specifying newline instead just leave the last line of your manifest file blank that will serve the purpose
4.Your text file for manifest must use UTF-8 encoding otherwise you may get into some trouble.
Finally i am providing an example of what a manifest file must look like.
Here package is calculator and the main class is Calculator.java
Manifest-Version: 2.1
Created-By: UselessCoder
Package-Name: calculator
Class-Name: calculator.Calculator.java
Main-Class: calculator.Calculator
The links offered by Peter were partially useful. However, I was able to solve this more or less by trial and error. The Oracle docs that explain how to do this need lots of work. They lack a good example of how to proceed. Anyway, for those who run into the same issues, here's what I did. I created a text file (eg. "Manifest.txt") using Notepad that contains the manifest attributes I wanted to add/update. In creating this file, I made sure to add a new line character to the last line by pressing the key on the keyboard. Next, I created a DOS bat file to do the actual modification. Here's what it looked like...
echo Updating manifest permissions...
"C:\Program Files\Java\jdk1.7.0_25\bin\jar" -umf "c:\some folder\Manifest.txt" "C:\some folder\jartoupdate.jar"
The order of the jar arguments as they relate to the actual paths that follow on the command line is important. The links from Peter's reply pointed that part out.
java.io.IOException: invalid manifest format error is also thrown when,
unwanted empty lines are present in manifest file.
For example:
Before manifest.txt
Manifest-Version: 1.0
Main-Class: Sample
After manifest.txt
Manifest-Version: 1.0
Main-Class: Sample
When modifying the contens of a jar you should more look into this direction:
http://docs.oracle.com/javase/tutorial/deployment/jar/update.html and especially
http://docs.oracle.com/javase/tutorial/deployment/jar/modman.html which describes the process of updating the manifest.
To update a manifest in a jar file, you found the answer in the oracle docs. Here is another place to see the answer. Assuming you have read access to the directory where the JDK is installed and the documentation was downloaded with it (easy to download the documentation, this is for SE7 http://www.oracle.com/technetwork/java/javase/documentation/java-se-7-doc-download-435117.html):
go to the [install_directory]/docs. In there is an index.html file.
>>>> e.g. mine for JDK 6 is called C:\my_TOOLS\Java_stuff\jdk_1.6.0_20\docs\index.html
Drag index.html onto a browser page.
That page shows you an overview of the JDK documentation and has many links to useful information.
In the top box's row under the Java Language row, click on JAR. It takes you to the page summarizing the documentation (with links) of the jartool.
In the "JAR Tools" section of that page, click on the reference page link for your platform (you probably want "JAR tool reference page for Windows").
That "JAR tool reference page" shows you the detailed documentation for the jar command. This is where you'll see the example "jar umf Manifest.txt my_jar.jar". (Solaris/Linux doesn't use the "-" in front of the args such as "umf".
I used it (on Linux) to merge 2 custom Manifest files into the default Manifest and put it in my jar. I do it in a two-step process but would be interested to know of a one-step command to do this. (Using two m's to the jar command causes the second manifest to overwrite the first manifest - not merged.)(Remember each manifest file must end with a blank line.)
Manifest.txt contains "Name: " and "Implementation-Version: "
Manifest.my_app.txt contains "Main-Class: " and "Class-Path: "
The default manifest contains "Manifest-Version: " and "Created-By: "
jar cmf my_app.jar Manifest.txt my_main.class my_utils.class
jar umf Manifest.my_app.txt my_app.jar
After this, my META-INF/MANIFEST.MF contains all the fields from all 3 manifests. I expected them to be appended one to another but the fields are jumbled together. Maybe someone can tell how to order them or straight append. This is the order in which they appear in my current META-INF/MANIFEST.MF.
Manifest-Version: 1.0
Implementation-Title: my_app
Implementation-Version: 1.2.1
Class-Path: ...
Name: My App
Created-By: 1.6.0_20 (Oracle Corporation)
Main-Class: My_App
I hope some of this is useful for you.

Failed to load Manifest attribute - eclipse

I am trying to export jar of a project via eclipse
when I run that jar it gives error "failed to load Manifest attribute"
although I give main-class-name in manifest file but it is not working
I put following in Manifest class
Manifest-Version: 1.0
Main-Class: HangMan
For additional information: directory structure of Main class is
"/TestHangMan/src/test/HangMan.java"
what I am doing wrong?
EDIT: I forgot to mention, It(HangMan) is an Applet.Apologies!
Try exporting the Project as "Runnable Jar" instead of "Jar". It will configure your Manifest for you just fine. Im not 100% sure, where that Error comes from, but I think it is because it is missing the classpath for your jar.
There is a simple way to export simple Java programs as runnable JARs from Eclipse:
Remove the MANIFEST.MF
Move HangMan.java to the src folder (that's where your final code should be located)
Run your program once inside of Eclipse to create a launch configuration.
Choose File->Export->Java->Runnable JAR File
In the dialog Runnable JAR file Export choose the correct launch configuration and destination. If you are not using any 3rd party libraries leave the rest as a default.
Try running the JAR, on Linux for example with
java -jar $HOME/tmp/HangMan.jar
Good luck!
Update:
OP's HangMan is a Java applet (see comments) :-).

create exe using launc4j java

I have created an .exe file by luunch4j and build.xml ant file in Java but when I click .exe I'm getting "could not find main class program will exit" kind of message. If I click on the .jar file it will also give me the "failed to load main class manifest attribute" message. What is the problem?
Have you a manifest file ?
In your manifest file, you give the entry point (class) where the main(String[] args) method is.
More detail : http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html
Here a example :
Manifest-Version: 1.0
Sealed: true
Main-Class: org.example.server.Lauch
Class-Path: lib\externalLib.jar
I had the same problem and the solution for it was that I hadn't set the JRE6 (in my case) location. You must put it on the JRE tab, at the Bundled JRE Path field.

Categories