jar does not generate correct manifest file - java

I've test simple scala program and compiled it with scalac
object Test {
def main(args: Array[String]) {
println("hongseok yoon")
}
}
If I run that 'java -cp .;scala-library.jar Test' and it works okay.
YES, scala-library.jar filee is in same directory.
This is my manifest file manifest.txt
Class-Path: scala-library.jar
Main-Class: Test
and I make jar with 'jar cfm Test.jar manifest.txt *.class'
If I unzip generated Test.jar file and open manifest file, it does not contain Main-Class field. So, It cannot be run with 'java -jar Test.jar'
I can edit it manually and Test.jar runs well.
Why does jar miss Main-Class field? and how to fix this?
(If I switch order of fields, only first field is shown)

ONE more empty line is needed at the end of manifest file.
What a stupid restriction! :(

Related

Unable to execute jar file due to main manifest attribute missing

I have below project structure:
->bin
->lib
->resources
->src
->DemoFramework
->FirstDemo.java
In lib folder, I have an external jar that I need in my application. Its name is ext.jar. In resources, I have Manifest.txt file, whose content is given below.
Manifest-Version: 1.0
Class-Path: . lib/ext.jar
Main-Class: DemoFramework.FirstDemo
I am using below command to generate jar:
javac -cp ".;./lib/ext.jar" src/DemoFramework/*.java -d bin
Basically I am putting all class files into bin folder so that in final jar file, source code is not visible.
Then I am issuing below command:
jar cmf resources/Manifest.txt project.jar bin lib
The jar file is successfully created but when I run it, it says:
no main manifest attribute, in project.jar
I am confused about this error, no idea why it is happening.
Can you guys help me to sort it out?
Thanks.

"Could find or load main class" while running executable jar file in Java code

After converting my java program to executable jar file using commands in command prompt in windows 10,while executing jar file I am getting error:
Could find or load main class Combine.class" caused
by:java.lang.ClassNotFoundException:Combine.class
My jdk-11.0.1 has javamail api and excelapi.While executing I have set my classpath as:
classpath=%classpath%;C:\Program Files\Java\jdk-11.0.1\javamail_api\javax.mail-1.6.2.jar;C:\Program Files\Java\jdk-11.0.1\javamail_api\activation.jar;C:\Program Files\Java\jdk-11.0.1\jexcelapi\jxl.jar;.;
It was compiling and executing properly but after converting to executable jar file it is not running and giving above error.
Any help would be appreciated.
Thank you
The clue is in the exception message. It is trying to load a class with the name Combine.class. But the classes real name is Combine.
You have created the JAR file incorrectly.
echo Main-Class: Combine.class > manifest.txt
jar cmf manifest.txt FinalExecutable.jar Combine.class
If Combine is in the default package (i.e. it doesn't have a package statement) then the above should be:
echo Main-Class: Combine > manifest.txt
jar cmf manifest.txt FinalExecutable.jar Combine.class
If Combine is declared in package foo.bar, then the above should be.
echo Main-Class: foo.bar.Combine > manifest.txt
jar cmf manifest.txt FinalExecutable.jar foo/bar/Combine.class
and you need to be in the directory above the foo directory.
NB: the "Main-Class" attribute in the manifest must be a Java fully qualified class name, NOT a filename or file pathname.
It also should be noted that the CLASSPATH environment variable and the -cp argument will be ignored when you run a JAR using java -jar .... If your executable JAR depends on other JAR files, you should either combine them (to create a shaded JAR) or you should add a "Class-Path" attribute to the manifest; see https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Finally, my advice would be to use a build tool (e.g. Maven) to compile your code, create the executable JAR file, etc rather than doing it by hand.

java compiling with jars

I'm trying to figure out how an existing Java program (I did not make myself ofcourse) was compiled with existing jars
I have Test.java (original source file):
package Demo;
//import classes from jars here etc...
public class Test {
public static void main(String args[]) {
etc...
}
}
Now I have two other jars:
file1.jar
file2.jar
Demo.jar
There is a batch script to run it:
#echo off
set CLASSPATH="file1.jar";"file2.jar";"Demo.jar"
java -cp %CLASSPATH% Demo.Test
This WORKS, but now I need to change the source file Test.java, recompile and run with the jars class dependencies. (sorry if I'm not making sense)
Now, I have tried to recompile this to reproduce same results with no luck:
javac -cp file1.jar;file2.jar;Demo.jar Test.java
defined manifest:
manifest.mf
Main-class: Demo.Test
Created directory "store" for class files and moved class files there
Ran:
jar -cmf manifest.mf Demo.jar store
Which created the "Demo.jar"
Then I ran the run the batch script above but not the same results (doesn't work at all)
Any help would be appreciated. Thank you!
It is difficult to create true executable jars as soon as you rely on external jars.
The only solution here is to :
put all jars in the same folder : yours and its dependencies
add a classpath entry inside your manifest
launch the jar using java -jar Demo.jar
The manifest will have to look like :
manifest.mf
Main-class: Demo.Test
Class-Path: file1.jar file2.jar

Creating a JAR File from the Command Line Doesn't Work

I've edited this question, and now it is exactly what I did:
I want to create a .jar file from some .class files in the command line.
I worked on Eclipse, and created:
myProject project, and in it: myPackage package, and in it: myClass class.
Then I wrote in the command line:
jar -cfv myJar.jar myPackage\myClass.class
And I got this:
added manifest
adding: myPackage/myClass.class(in = 745) (out= 473)(deflated 36%)
This really created the myJar.jar file in my current directory. Now, I wanted to check if the process was done successfully, so I extracted the class from the jar thus:
jar xfv myJar.jar
And I got this:
created: META-INF/
inflated: META-INF/MANIFEST.MF
inflated: myPackage/myClass.class
And this created for me just the META-INF folder, with the MANIFEST.MF in it, but I don't see any .class file here!!
It seems like something in the packing to jar process is incorrect.
Anybody has an idea??
Any answer is appreciated!
According to the output you gave, there is no myClass.class file in the directory where you execute
jar -cf myJar.jar myClass.class
So obviously, the command can't add it to the jar: it doesn't exist. If you want to add the myClass directory, recursively, to the jar file, then use
jar -cf myJar.jar myClass
EDIT:
Just look at the output:
inflated: myPackage/myClass.class
The myClass.class file is there in the jar file. There is no problem at all.

[TestNG][Jar] Set Classpath in Manifest (Class-Path) -- classpath is ignored?

I'm trying to run a simple TestNG test case residing within a jar file, which
contains the test and manifest:
ex.) Test.jar contains:
{
META-INF\
META-INF\MANIFEST.MF
tests\
tests\Test01.class
}
I am trying to run it using the command: java org.testng.TestNG -
testjar Test.jar testng.xml
Where a folder contains (all in the same directory):
Test.jar
testng.xml
testng-6.1.1.jar
And Manifest contains (w/ a line-break at the end):
Manifest-Version: 1.0
Created-By: Willie Owens
Class-Path: testng-6.1.1.jar Test.jar .
And I get a NoClassDefFoundError: org/testng/TestNG. Could not find
main class.
If I specify the classpath using -cp after "java" (Ex. java -cp
testng-6.1.1.jar;Test.jar org.testng.TestNG -testjar Test.jar
testng.xml) it works, but I want this information in the manifest.
What am I doing wrong here?
Also, I've tried every variation I could think of when typing in the Class-Path, such as: ./testng-6.1.1.jar .\testng-6.1.1.jar ...etc..
HELP
Executing test-jar is not that complex with testNg please refer [1] for more detail.
lease let me know if you find any pitfalls with it .
[1] http://dharshanaw.blogspot.com/2012/10/how-to-execute-testng-tests-in-side.html

Categories

Resources