Executing stand alone project with main method through batch file - java

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

Related

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

No main manifest attribute, jar file, Applet

When trying to open my exported project (jar file) I get an error message
No main manifest attribute, in file.jar.
My project is basically a simple class extended by Applet, but the file won't open.
My java is up to date and I've tried using the line in cmd
java -jar file.jar
in order to open it, but it still doesn't work.
What is the reason that the main class isn't recognized?
Thanks for the help!
Create a manifest file like this (manifest.mf)
Main-class: start.HelloWorld
Class-Path: /pathToYourExternalJars/XXXXX.jar /pathToYourExternalJars/XXXXX.jar
Here 'Main-class' is the starting point of your application (simply class which contains main method ) & 'Class-path' is for external jars which are included in your application (Optional)

Using JAR Files

So I'm still a noob in Java and I'm experimenting around with a few things.
I recently created a .jar file for my class using jar cvf <name>.jar <source files> and then used that jar to compile my driver class (javac -cp <name>.jar Driver.java) though how do I now run that class using the jar?
I've tried the following 2 commands:
java Driver and,
java -cp <name>.jar Driver.
The first gives me a NoClassDefFoundError for the class used, whereas the latter just gave me a single line error.
Error: Could not find or load man class Driver
What am I doing wrong? Is it possible I'm confusing this for something else?
I'm trying to do as much as I can without the use of any IDE.
You should put jar file and compiler output into classpath and specify main class:
java -classpath "<name.jar>;classes" Driver
EDIT (thanks to Kayaman):
If you are running command from linux/unix you have to use ":" as separator (in Windows works ";"). "classes" is a path to folder containing compiler output.
When creating an executable jar ( jar which contain a class with the main method) you should tell the jar which is the mainClass to be executed and for that you should create a file called 'Manifest.mf'.
The file should contain this:
Main-Class: MyPackage.MyClass
And when creating the jar you should use this to include your manifest:
jar cfm MyJar.jar Manifest.mf MyPackage/*.class
And for launching your jar :
java -jar MyJar.jar

running jar file from my class file in the IDE netbeans

I use a jar file called korat.jar. I executed with the command line:
java -cp $CLASSPATH korat.Korat --visualize --class test.Add --args 3
The classpath contains the path of the jar and also the Add.class file.
I want to execute this jar in my own program java in netbeans IDE. I think I would use:
String []s={test.Add.class.getName(),"--visualize","--class","test.Add","--args","3"};
Korat.main(s);
I get this exception: java.lang.NoClassDefFoundError
What do you mean "to execute the jar in my own program"? The jar contains some classes, if they are in your class path, you can instantiate the classes themselves and invoke some methods. In that case, you should use test.Add class. But it seems like the class is not in your classpath - java.lang.NoClassDefFoundError.

[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