can not execute jar file from cmd - java

i created an executable jar file using the command jar cmf <text-file-points-to-main-class> <archive-name.jar> as a result an executable jar file is produced, but when i attempted to open that executable jar using java -jar file-name.jar the following exception is raised
Exception in thread "main" java.lang.NullPointerException
at sun.launcher.LauncherHelper.getMainClassFromJar(LauncherHelper.java:399)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:463)
the <text-file-points-to-main-class> actually contains--->Main-Class:hello.java also i tried(hello without .java)
i can't point what exactly is the problem ? note that the class file works properly

Main-Class:hello.java
The Main-Class value should be the fully qualified class name, not the file name, e.g. for
package com.example;
public class hello {
you need Main-Class: com.example.hello

Related

Executing a class in jar file

I have created an application that uses selenium-server-standalone-2.47.1.jar and javax.mail.jar. The code works on eclipse, but I would like to run the same from command line. So I exported the project to a runnable jar file which contains both selenium and javax.mail.jar.
My code contains RTC.java which has the Main function and another Ex.java.
Both the class files are generated in com folder.
My App1.jar file is located in C:\installers.
I used the command:
c:\installers> java -cp App1.jar com.RTC
It says:
Exception in thread "main" java.lang.NoClassDefFoundError: org.openqa.selenium.WebDriver
Further I used:
java -cp .App1.jar com.RTC
Then it says could not find or load main class com.RTC.
What am I doing wrong?
I used the command:
c:\installers> java -cp App1.jar com.RTC
It says:
Exception in thread "main" java.lang.NoClassDefFoundError:
org.openqa.selenium.WebDriver
That exception usually means that a .class file was found but it didn't contain the right class. Check how you're putting it into the JAR. It's directory and file name must match its package and class name.
It sometimes also seems to mean that a secondary class wasn't found. Normally secondary JAR files are mentioned in the class-path entry of the manifest of the main JAR file, along with the main-classname, so you can use
java -jar App1.jar
Further I used:
java -cp .App1.jar com.RTC
Then it says could not find or load main class com.RTC.
I'm not surprised. If the first command got as far as it did, the JAR filename doesn't start with a dot. Cannot imagine why you tried this. It is nonsense.
I found the solution. I need not create a jar file which has the reference jars. I have to mention my jar which has my code and the reference jars that I am using in the class path.
My code is in App.jar.The reference jars are
selenium-server-standalone-2.47.1.jar and
javax.mail.jar.
so I used
c:\installers> java -cp selenium-server-standalone-2.47.1.jar;javax.mail.jar;App.jar com.RTC
Thank You.

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.

Error while running jar file on system

I am having a java file like the following:
import org.xBaseJ.DBF;
import org.xBaseJ.fields.CharField;
import org.xBaseJ.fields.NumField;
import org.apache.log4j.*;
public class Example2 {
public static void main(String args[]){
..........
}
}
I have created the this to 'Example2.jar' file to run by following the steps below:
1) javac Example2.java
2) java Example2
3) This will produce a .class file needed for the JAR file.
4) Next create a manifest file (saved using the extension .txt) using the text editor and input the following
Main-Class: Example2
or whatever your file's name is.
5) Next create the JAR file using this code:
jar cfe Example2.jar Example2 Example2.class
After step 5, I got a jar file named 'Example2.jar' . I tried to run the jar file using the following command:
java -jar HelloWorld.jar
But I am getting the following Errors:
Exception in thread "main" java.lang.NoClassDefFoundError: org/xBaseJ/DBF at Example2.main(Example2.java:14) Caused by: java.lang.ClassNotFoundException: org.xBaseJ.DBF at java.net.URLClassLoader$1.run(URLClassLoader.java:372) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more
I did not understand, what is the reason? Please guide me?
Well one thing is that when u are creating Jar just look this menifest first that is what can help for the including the External Libraries.
http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
While creating and giving chages to the menifest just have to run that class with the configuration
http://docs.oracle.com/javase/tutorial/deployment/jar/modman.html
so seeing both of that link.
just look this Example.
run your java and get class file
javac Test.java
and if u having some other external libraries working then do like this.
javac -classpath xxx.jar Test.java
and see the menifest configuration and make that file with external changes like this.
menifest.txt
Main-Class: Test
Class-Path: xxx.jar xxxx.jar
then u need to make jar file like this.
run this command
jar cfm jarName.jar manifest.txt Test.class
and hence we done u can have jarfile in that same path.
The error you're seeing is caused by having an incorrect classpath. I'm assuming that when you compiled the class, you somehow provided a classpath (either by passing the '-classpath' arg or by setting the 'CLASSPATH' environment variable). The problem is that the compilation classpath is separate from the runtime classpath. So, you just need to ensure that all of the dependencies (other jar files, most likely) that were on the classpath when you compiled the class are also added to the classpath when running the jar. For a jar file, this is typically done by adding a 'Class-Path'header to the manifest.
An alternative method would be to specify the classpath using either the command line arg or the environment variable and include your Example2.jar file in that classpath, and run java Example2 (without the '-jar').

Executing Main class in Jar using batch file

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.

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.

Categories