Can't launch my first Java program by jar file - java

I just made my first Java program and it worked perfectly, when I launched it by 'IDEA'. Then I wanted to create an executable file to launch it separately. 'IDEA' helped me create a jar file with this content inside it:
Some classes (*.class files).
Folder META-INF with MANIFEST.MF in it. MANIFEST.MF has only 2
lines: version info and "Main-Class: ru.nikolski.2048.main".
When I double click on the jar file, nothing happens. When I launch the jar file by 'IDEA', it writes the following:
"C:\Program Files\Java\jdk1.7.0_51\bin\java" -Dfile.encoding=windows-1251 -jar C:\Users\Osn\IdeaProjects\Game2048\out\artifacts\Game2048_jar\Game2048.jar
Error: Could not find or load main class ru.nikolski.2048.main
I tried to make folder ru/nikolski/2048 inside the jar file, and put all *.class files in it, but it didnt help, I got the same error.
What am I doing wrong?

I solved the problem just correcting the line in MANIFEST.MF:
Main-Class: Main
as my class with main method has the name 'Main'.

Related

Jar file when extracted and rebuilt, isn't working

I'll try to be clear and concise on this.
i have a jar file of a project which is working perfectly. Now I need to make some changes to the application so i extracted that jar using this command.
jar xf jwire.jar
the files extracted are
META-INF (containing manifest file)
Resources (containing pictures used in application)
Jwire (containg classes and java files)
.classpath
.project
manifest.mf (another manifest file with some other path to main class)
i also added two empty folders src and bin here on somebodys suggestion.
now i'm trying to create the jar file again using all these folders.
Without making any type of changes I'm trying to rebuild this jar using the following
jar cf newjar.jar G:\Project1\project
NOTE:- I am guessing it's the right way to build the file. Please point if it's not right.
G:\Project1\project is the path to above mentioned files and folders.
The jar created using this is of size 729kb while the earlier one had 711Kb and when I run it using the following command.
java -jar newjwire.jar
I get the following message
no main manifest attribute, in newjwire.jar
Next i tried extracting this newjwire.jar file in order to see its manifest file. I got this folder META-INF containing the MANIFEST.MF file. it didn't have the path to the main class , So I edited it and put the modified file back on using the following command
jar uf newjwire.jar META-INF/MANIFEST.MF
when I run this jar, I get the following message
Error: Ivalid or corrupt jarfile newjwire.jar
What am I doing wrong? Am I building the jar file the right way? I used eclipse juno too and it gave the same message to that mainclass is missing. There is a main class consisting the main function. Is this something has to do with My orignal jar having two MANIFEST file.
I hope the problem is understandable. Clear and detailed answers will be appreciated.
you're supposed to update JAR files by jar uf jar-file input-file(s)
see this link: http://docs.oracle.com/javase/tutorial/deployment/jar/update.html
According to this page you need to EITHER have Manifest.txt present when you create or update the JAR, or use the e option to specify the main class. Probably the latter would make more sense in your case.
jar cfe newjar.jar mypackage.MainClass G:\Project1\project

.jar file do not contain Main-Class attribute in manifest.mf

I am trying to run jar file through
java -jar jts.jar
but it is unable to run because it's Manifest.mf do not contain 'Main-Class' attribute, which is necessary for a jar file to run.
I am have same problem with 9-10 .jar files.
Help me with how to find those mainclass in jar files, there is no any directly defined mainclass in jar file.
How should I find the correct mainclass and how should I add it to manifest.mf
On command line you can use following command to generate jar having existing manifest.
jar cvfm MyJarName.jar manifest.txt *.class
Refer this page for more details.
Alternatively, if you are using eclipse. Please follow steps mentioned here to export jar.
if you have your Main-Class attribute on the last line of your manifest.txt, it vanishes if you do not have a new line at the end. Very annoying, but always make sure you have a new line at the end of your manifest file.
1. You can open the jar file with winzip or winrar.
2. Open manifest file in notepad.
3. Add main-class: ClassNameContianingMainMethod.
4. Save the file and then run it

eclipse for Java NoClassDefFound error on main entry point - package, project and class are the same

Trying to create a windows executable but always get error on Exception in thread main java.lang.NoClassDefFoundError. I've read all the other responses but so far my issue remains the same.
I have a class file called testproject that has a main procedure that is public static void. My class file also have a package designator at the top of the file called testproject. My class file compiles successfully into a file called testproject.class.
The command below works but when I run testproject.jar, I get the above error:
jar cvfm testproject.jar c:\temp\manifest.txt *.class
Contents of manifest.txt:
Main-Class: testproject.testproject
I've tried many combinations of Main-Class
please add the code that you have written in your java file.Otherwise it can not be tracked.
seems like the jvm is not able to find the class file for the Main class.the possible root causes could be
The files are not generate at correct places ,Try extracting the jar file an see if the classes are there in correct package folders
The Manifest file or the jvm command line classpath or the manifest file doesn't contain the entry for path to the class file
Try using IDE for generating the JAR file , that usually helps
It seems like you are executing the command "jar" from your package "testproject":
jar -cvfm testproject.jar c:\temp\manifest.txt *.class
Try to execute it from parent folder:
jar -cvfm testproject.jar c:\temp\manifest.txt testproject/*.class
The class file will be put into the "testproject" package.
By the way, be ensure that your manifest file has a new empty line at the end.

java.lang.NoClassDefFoundError thrown when running jar file

I am trying to run my jar file on a Mac (Haven't yet tried on any other platform). When I run using java -jar MyApp.jar I get the following error
Stacktrace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at tabalchi.MyLogger.getLogger(MyLogger.java:51)
at tabalchiApp.TabalchiApp.printSystemProps(TabalchiApp.java:117)
at tabalchiApp.TabalchiApp.main(TabalchiApp.java:37)
So, as you can see the main class is recognized.
This is the manifest file.
--->
Manifest-Version: 1.0
Class-Path: . jars/log4j-1.2.16.jar jars/jfugue-4.1.0-20120125.jar jars/gervill.jar jars/AppleJavaExtensions.jar
Main-Class: tabalchiApp.TabalchiApp
SplashScreen-Image: tabalchiApp/resources/splash.png
<---
And the folder structure in the jar file as follows.
--->
META-INF/MANIFEST.MF
jars/AppleJavaExtensions.jar
jars/gervill.jar
jars/jfugue-4.1.0-20120125.jar
jars/log4j-1.2.16.jar
tabalchiApp/TabalchiApp.class <-- this is the main class
tabalchiApp/otherClasses.....class
<---
I have read many posts about running .jar file but none of the solutions have worked for me. I would really appreciate your help on this one.
I am creating the jar file by exporting from eclipse. If I export to a runnable jar file then I cannot control the making of the manifest file. I need to add the splash image in the manifest file. And I am having some other issues with the runnable jar export. Hence this path.
It seems that log4j is missing.it is expecting jars/log4j-1.2.16.jar in class path .
jars/log4j-1.2.16
should be under folder structure.
jars/log4j-1.2.16.jar
In which directory are you trying to use:
java -jar MyApp.jar
You can specify the packages/directories in the classpath to be sure they are found.
Ok, so according to this link,
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.
So, it seems I cannot include other jar files in my jar file.

Problem in executing a built jar file in netbeans

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.

Categories