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

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

Related

Create executable jar including class files, manifest and external jars

I have two class files: Generator.class(Main) and Approach.class(Helper), which are created when I compile Generator.java code. The code is dependent on two external jars - argparse4j-0.7.0.jar and org.apache.commons.io.jar.
In normal scenario, if I need to execute the code, I would have to run below :
java -cp .:argparse4j-0.7.0.jar:org.apache.commons.io.jar Generator
I want to create a jar file so that it is easy to share a single file.
One approach that I found was, to include below in the jar file (Main.jar) -
1. Generator.class
2. Approach.class
3. manifest.txt
Manifest file : manifest.txt
Main-Class: Generator
Class-Path: argparse4j-0.7.0.jar org.apache.commons.io.jar
In this case, I need to share below 3 with the user -
1. Main.jar
2. org.apache.commons.io.jar
3. argparse4j-0.7.0.jar
To run the jar :- java -jar Main.jar
Is it possible to create a single jar file (Main.jar) with below files -
1. Generator.class
2. Approach.class
3. org.apache.commons.io.jar
4. argparse4j-0.7.0.jar
5. manifest.txt
Then only Main.jar needs to be shared with the user and it can be run using : java -jar Main.jar
I gave this a try with the same manifest file as mentioned above but the java command fails to run the jar.

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

Executing stand alone project with main method through batch file

I have a project with main method.I have to execute this project through batch prcessing.
I have exported it to jar file and created .bat file. I have used start javaw -jar JarFile.jar command in .bat file to execute the jar file. But it is not working. Please help me.
And also i have a doubt, if we export the stand alone project to jar and execute it through batch file, how batch file will be aware of in which class main method is available and execute it.?
Thank you.
You'll need a manifest file to tell where the main class is.
The manifest file will contain a Main-Class, for ex -
Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)
Main-Class: MyPackage.MyClass
Have a look at this link - https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
You can use the commandline by passing explicitly the class containing the main method:
java -cp <yourJarFile.jar> com.example.MainClass
You can also define a main class into a manifest file in the jar. Doing that, the main class will be executed by default when you call:
java -jar yourJarFile.jar
You can try using following command from Java, where you should loop through all jars you have.
Runtime.getRuntime().exec("java -jar yourJarFile.jar")

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

jar does not generate correct manifest file

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! :(

Categories

Resources