create exe using launc4j java - 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.

Related

JRE not using classpath specified by manifest file of runnable jar

First post, so sorry for my poor formatting. I have a java program that I developed in eclipse. I exported the program as a jar (myJar.jar), and then I put all of the external jars that my program depends on into a folder called lib that lives in the same location as myJar.jar. In order to set my classpath I have a manifest file with the following format:
Main-Class: exe.myMain
Class-Path: lib/jar_1.jar lib/jar_2.jar ... lib/jar_n.jar
Manifest-Version: 1.0
However, when I attempt to run the program using "java -jar myJar.jar" the classes from the jars that live in lib are not being loaded (I'm getting a ClassNotFoundException) . I used the following code in my program to print the classpath:
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url:urls){
System.out.println(url.getFile());
}
And when I run this code the classpath is simply "myJar.jar".
I have two questions:
1.) Does the above code actually give me the classpath for the JRE at run time, or am I simply being given the address of my main class?
2.) Given the above code does indeed give me the classpath for the JRE at run time, am I doing anything wrong?
Please feel free to ask for more information, and I will happily provide what you need.
What you are doing sounds correct.
For some reason the Class-Path entry in the manifest does not show up when inspecting the classpath (e.g. here and here; those examples use the property "java.class.path" but my testing shows that ClassLoader.getURLs() behaves the same). It should still get searched for classes though. I don't know how to obtain the true classpath that includes the Class-Path entry from the manifest.
The first thing I'd like to check is that the file META-INF/MANIFEST.MF inside myJar.jar matches the manifest that you created. You can open myJar.jar by renaming it to have a .zip file extension.
I tried to replicate your problem but the classes in lib/jar_1.jar were loaded for me, so if META-INF/MANIFEST.MF is correct I'll describe what I did in detail so you can find what we are doing differently.
Edit:
Here are the steps I used:
Create a new directory called "experiment". The following steps are all to be done in that directory.
Create new directories called "jar_1", "lib", and "exe"
Create a file called "ClassInJar1.java" in directory "jar_1" with the following content:
package jar_1;
public class ClassInJar1 {
public static void method() {
System.out.println("Hello from ClassInJar1");
}
}
Run javac jar_1/ClassInJar1.java
Run jar cf lib/jar_1.jar jar_1/ClassInJar1.class
Create a file called "myMain.java" in directory "exe" with the following content:
package exe;
import java.net.*;
import jar_1.ClassInJar1;
public class myMain {
public static void main(String[] args) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader) cl).getURLs();
for (URL url : urls) {
System.out.println(url.getFile());
}
ClassInJar1.method();
}
}
Run javac exe/myMain.java
Create a file called "manifest" in the "experiment" directory with the following content:
Main-Class: exe.myMain
Class-Path: lib/jar_1.jar lib/jar_2.jar
Manifest-Version: 1.0
Run jar cfm myJar.jar manifest exe/myMain.class
Run java -jar myJar.jar
Output:
/.../experiment/myJar.jar
Hello from ClassInJar1
Your manifest file is seem to be wrong. You have Main-Class: exe.myMain => java but have type of exe? Recommend for you is you should using eclipse to export your jar file it will auto create all things for you, you don't have to manually create manifest file so you can make mistake. See the detail as follow:
I have tested by created a java project with eclipse => using export function of eclipse to export a runnable JAR => using the 3rd option (copy required libraries into a sub-folder...). After exported I got the jar with name myJarName.jar and a folder with name myJarName_lib. Open the jar with 7zip program => under the META-INF => open the MANIFEST.MF => and here is the structure of it:
Manifest-Version: 1.0
Class-Path: . myJarName_lib/gson-2.8.0.jar myJarName_lib/jsoup-1.8.3.jar myJarName_lib/commons-cli-1.2.jar myJarName_lib/jackson-core-2.8.8.jar
Main-Class: upwork.Main

Cannot run jar file: Could not find or load main class Hello

I create jar file in IDEA Build>Build Artifacts. But can't run it with java -jar jarname.jar - Error: Could not find or load main class Hello. MANIFEST.MF file is in the /resources/META-INF/ folder. And here is the launcher class:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
MANIFEST.MF:
Manifest-Version: 1.0
Main-Class: Hello
EDIT: Added artifacts setting screenshot
You go to project structure then choose "Artifacts" from the left tab. Add a new artifact and as you see here although I have a manifest selected and a Main class selected, on the left side it shows what it will add to the jar. On the right side it shows what's available(un-added). However, since I haven't added anything into my jar yet, it will only add the Manifest and none of the actual code.
You need to specify that you want to add the compile output to the jar or else it will only have the manifest and not your actual classes. You can do that by just double clicking on it. If you specify the directory above the compiled output, it will add the src as well I believe.
Update: Add external dependencies
i believe your manifest file must say what the main class is if you want it to auto execute.
Main-Class: Hello
otherwise you need to specify it on the command line when attempting to execute the jar. As far as how to do that with IntelliJ, I can't help you there.
java -cp hello.jar Hello
Note that the reference to the class with the main method is the fully qualified location (package.classname) but since your class has the default package, its not necessary.
If your jar file build correctly.
try java -jar hello.jar Hello

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) :-).

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

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

Categories