Executing Main class in Jar using batch file - java

I have a runnable jar with main class located at /DataValidation/src/package/class
i am using the following commands in batch file to run the main class but i am getting the error as could not find or load main class.
#echo off
javac -cp C:\Users\500603\Desktop\Excel_files\ivg\sample.jar; package.class.java
java -cp C:\Users\500603\Desktop\Excel_files\ivg\sample.jar; .package.class
pause
Please help me how can i execute the main class present in jar file

Create MANIFEST.MF file. Add line:
Main-Class: package.class
in jar, create META-INF folder and put manifest file into it.
java -jar C:\Users\500603\Desktop\Excel_files\ivg\sample.jar should work now.
P.S. package and class are very confusing names. Use something different.

Related

Could not find or load main class with a Jar File.The cause is a bcprov-jdk15-133

Execute jar shows Could not find or load main class with a Jar File.
Before I add some 3rd jar to project, it's ok. Then I add and delete repeatedly, finally confirm It's bcprov-jdk15-133.jar.
Why can't jar execute after I add this package?
e
As shown in the figure, the left's can execute. The main different is the 3rd package. When I delete the BCKEY.SF BCKEY.DSA and it could execute.So it's the jar sign problem. But I don't know how to deal it.
JAR files contains manifest file that specifies the main class to execute with the command:
java -jar jarfile
Mainfest file entry:
Main-Class: com.test.something.Executable
The JAR file is an executable without specifying main class to run, as manifest entry would be picked by default.
But in case this is not specified and their are multiple main classes in the same archive, execution would fail. So,
Update the manifest to add the main-class entry
or
Execute JAR file with command to exeucte the main method in this class
java -cp jarfile com.test.something.Executable

Running JAR file from terminal (Runtime.getRuntime().exec) with both native and .jar dependencies

I am having trouble running a JAR file from terminal which has both native and .jar dependencies. Okay, my goal isn't to run it from the terminal, but to run it as a separate process with Java's Runtime.getRuntime().exec function, but if I can't run it from the terminal, then I also can't run it via. The JAR file I am trying to run depends on a number of other jar files as well as a number of .so libraries. I'm trying to put put all the .jar dependencies and .so dependencies in their own folders and then run the jar file with:
java -cp /home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/* -Djava.library.path=/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64/* -jar /home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar
Where "/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/" contains all the JAR files and "/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64/" contains all the .so files and the main JAR file to run is "/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar", but I keep getting this error:
Error: Could not find or load main class
.home.johnmichaelreed.Desktop.Dropbox.Libjitsi_linux_64.some-compressed-jar-file.jar
Where some-compressed-jar-file.jar is one of the .jar files that my application is supposed to use.
Here's my Java JAR dependencies folder:
And here's my native libraries dependencies folder:
UPDATE:
Okay, this is the solution:
java -Djava.library.path=/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64 -cp '/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar:/home/j‌​ohnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/*' Main
With attempt at command line args:
java -Djava.library.path=/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64 -cp '/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar:/home/j‌​ohnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/*' Main "arg"
You can't use -jar and -cp at the same time.
What you can do, is adding your jar to the classpath and then specify your Main class to run. You can also specify the jar dependencies within the manifest of your jar.
Please have a look here for more details.
Assuming your Main class is in called Main and in the package foo.bar, then a possible call may look like:
java -cp "/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/*;/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar" -Djava.library.path="/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64/*" foo.bar.Main

Why I obtain this "Unable to access jarfile" error when I try to execute my jar?

I have the following problem trying to execute a jar file (created from my project) into the Windows shell (the DOS prompt).
I have a file named Main.jar. If I unzip this file it contains a mainPkg folder (that is the name of the package containing the Main class that itself contains the main() method).
So into the mainPkg folder there is the Main.class file that contains the main() method.
Ok so, from the shell, I go into the directory that contains the Main.jar and I perform:
C:\Projects\edi-sta\build\jar>java -jar mainPkg.Main.jar
Error: Unable to access jarfile mainPkg.Main.jar
But as you can see I obtain the Unable to access jarfile mainPkg.Main.jar. Why? What am I missing? How can I solve this issue and execute my Main.jar file?
Basically you've two types of JARs
Normal JAR - to package your classes into a single archive
Runnable JAR - This is similar to Normal jar except that you can run this with java -jar command like this java -jar RunnableMain.jar.
In this one we already configure the class having main(),so no need to pass the class name in jar command
Assuming that yours is a normal JAR, you can execute your class of interest like this
C:\Users\arkantos\work>java -classpath C:\Project\Main.jar mainPkg.Main
Notice that i've mentioned the absolute path of the JAR to add it to classpath, because I'm in a different directory, if not you can cd to that dir containing your Main.jar and then invoke your Main class like this
C:\Project>java -classpath Main.jar mainPkg.Main
Here Main.jar is inside Project directory so no need to give absolute path
The syntax for executing a class containing a main method in a jar is:
java -classpath <jarFile> <class>
In your case:
java -classpath Main.jar mainPkg.Main
If you want to execute the jar using java -jar you must create an executable jar file. That can be done in different ways depending on which build tools you use.

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.

Error when executing a JAR file

I've been learning about JAR files and wanted to try and create and run one myself. I carried out the following steps:
Created a project folder with a 'source' subfolder and a 'classes' subfolder
I wrote 2 source files, one with a main method which creates an instance of the other class and runs a simple method in it.
Compiled these to the 'classes' subfolder. I checked to see if they would run. They did
I created a manifest.txt file and filled in the Main-Class: xxxx and hit the return key. I saved this in the sources subfolder
Created a jar file in the classes subfolder by writing
jar -cvmf manifest.txt zzz.jar *.class
Tried to execute the jar file by typing
java -jar zzz.jar
This gives a ClassNotFound exception. If I try to execute the jar by double clicking on it in windows I get an errorbox saying "Could not find the main class xxxx"
I've double checked the spelling of the class inside the manifest file and it's correct.
Possibly important: I have to compile my programs using java -cp . xyz as there is an issue with my classpath. Does this mean that I need to execute jars in a different way as well? I tried
java -cp . -jar zzz.jar
but ended up with the same exception.
Edit: I ended up starting from scratch and now it runs (with the basic -jar zzz.jar command). Frustrating that I don't know what I was doing wrong but glad that it is working!
Shouldn't number 5. be run in the classes subfolder, where all your class files are? And if your classes are in packages, which they should be, you'll likely want to use * instead of *.class..?
To check what your jar file contains you can run:
jar tf zzz.jar
You will probably have to supply the entire path of the .class file you wish to execute after the classpath. ie java -cp xxx.jar classes.mainProgram.class. Where classes is the name of the folder which contains your class files.

Categories