Creating Executable jar from CMD prompt - java

I have following class files,jar and manifest in a directory
Dot.class
Bridge.class
jsch.jar
MANIFEST.MD
Manifest file contains following two lines
Manifest-Version: 1.0
Main-Class:Dot
command used for creating jar file
jar cfm dot.jar MANIFEST.MD *
While executing generated jar it throws error saying no main manifest attribute
While seeing inside the generated jar META-INF folder contains auto generated manifest file, it doesn't have content for my main class.
I couldn't find successful steps ,Please correct me.

Had the same issue a few days ago and couldn't resolve it with the manifest file so I put the main class as a build parameter like this:
jar cfe Main.jar MainClass *.class

Add a space after ':' as in
Main-Class: Dot
Add a new line after the last line, in your case after Main-Class entry:
Manifest-Version: 1.0
Main-Class: Dot
The reason for 2. ist documented in https://docs.oracle.com/javase/tutorial/deployment/jar/modman.html in detail.

I tried below command and it works with the jar produced which i post.
java -cp "jsch.jar;." Dot

Related

Package structure and jar file with manifest

Let's say I have the following "project" structure.
*Root/
- manifest.txt
+ com/
+ mypackage/
+ example/
- MyClass.java
- MyClass.class
I know from the docs.oracle.com documentation that the manifest file must be written in the UTF-8 character set and that there must be a space after each colon, and a carriage return ('\r') or a new line ('\n') before save file.
/* ** MANIFEST.TXT ** */
Manifest-Version: 1.0
Class-Path: .
Main-Class: com.mypackage.example.MyClass
/* ** END OF MANIFEST ** */
Creating an executable .jar file using cmd from the root directory.
CMD:
jar -cvfm out.jar manifest.txt com/mypackage/example/*.class
OUTPUT from CMD:
added manifest
adding: com/mypackage/example/MyClass.class(in = 648) (out= 445)(deflated 31%)
Now I'm trying to run the newly created jar file from the root directory:
java -jar out.jar
OUTPUT from CMD:
Error: Could not find or load main class com.mypackage.example.MyClass
I can't get to the goal despite the last problem. What am I doing wrong??? Thank You.
Here is a screenshot of the screen and cmd.:
As you described it, you will get a running jar, and therefore, what you described is not an accurate representation of what you did.
A few steps to check:
Did you paste the complete error message? For example, maybe the error message includes the note Caused by: java.lang.NoClassDefFoundError: MyClass (wrong name: com/mypackage/MyClass) or something along that vein. Then the problem is: Your MyClass.java needs to start with package com.mypackage.example;, it needs to contain class MyClass with that exact casing, and it needs to be called MyClass.java with that exact casing.
Did you actually do all the steps exactly as you laid out in your question? I did, and it works fine. This isn't one of those 'works on one machine and may not work on another' kind of scenarios, which is why I conclude you didn't. For example, if you include just MyClass.class and not com/mypackage/example/MyClass.class (i.e. you run the jar command from something that isn't the directory containing the com directory), you'd get this problem.
NB: 99% of folks building java jars use a build system to do so, such as maven or gradle.
Class-Path: com.mypackage.examples
Main-Class:My class
On Unix don't forget to set the .jar executable permission.
And alternate with no manifest.
Java -cp thejar.jar com.mypackage.examples
Don't forget the main class must have a
public static void main(String args[])
method to start the class.

Can't launch my first Java program by jar file

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'.

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 ?

.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

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