Reputation: 21
I recently installed the last version of Eclipse on my pc running on Windows 10. Every time I try to export the simplest Java project in a runnable jar file, the jar file doesn't work.
Example:
import javax.swing.JOptionPane;
public class Try {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Some message", "title!!", JOptionPane.INFORMATION_MESSAGE);
}
}
File> Export> Java > Runnable Jar File All names/ Folders been set OK Package required libraries into generated JAR (but I tried every other switch )> Finish The jar file is created but doesn't make anything at all. Any suggestions?
Upvotes: 2
Views: 1285
Reputation: 4464
Not sure if this is your issue, but your jar needs to contain a manifest file to indicate which class within the jar is the application's entry point. So it may be worth checking whether Eclipse is adding that correctly. Looks like some manual steps may be involved. The file (META-INF/MANIFEST.MF) should contain contents such as:
Manifest-Version: 1.0
Created-By: 1.8.0_112 (Oracle Corporation)
Main-Class: MyClass
For reference, some guidance on adding the manifest (outside of Eclipse) can be found here.
FYI, if you do end up adding a manifest manually, be sure to follow this advice from that page to add a new line at the end of the file. Missing it, as I have, can lead to much frustration:
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.
You would create a file, say Manifest.txt
, containing this (plus a "new line"):
Main-Class: Try
Then create the jar:
jar cfm anyname.jar Manifest.txt Try.class
And then run the jar by double clicking it or by running this:
java -jar anyname.jar
Upvotes: 1