Executing a class in jar file - java

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.

Related

java run class using jawin

I'm trying to run a class that I wrote that is importing the jawin library.
My command line is as below:
java -cp C:\class_path ClassName
I'm getting:
Exception in thread 'main' java.lang.NoClassDefFoundError org/jawin/funcptr
What am I doing wrong?
Doesn't the JAR that I'm adding to the build path get into the binary file that I've compiled?
If not, then when I publish my class file how will it know to find the external JAR? What if it doesn't exist on the target computer?

Specify Java classpath

I am on a Linux and have a WAR directory with WEB-INF/lib and WEB-INF/classes directories. I further have a class with a main method.
I want to make java execute the main method and have all libraries on the claspath, so I call
java -cp /path/to/WAR/WEB-INF/lib/*:/path/to/WAR/WEB-INF/classes/* this.is.my.package.Main
I get a
Error: Could not find or load main class this.is.my.package.Main
However, when I call from inside WEB-INF/classes directory
java this.is.my.package.Main
I get the Exception because a library class is missing
Exception in thread "main" java.lang.NoClassDefFoundError: ...
What am I doing wrong?
The classes directory itself must be in the classpath. Not all its files:
java -cp /path/to/WAR/WEB-INF/lib/*:/path/to/WAR/WEB-INF/classes

.jar file only works when used in Eclipse

I'm using a .jar file which I imported to Eclipse, and when I used Eclipse to run the application is works just fine.
But when I try running it from the command line with just the command java ClassName I get a NoClassDefFoundError message.
The .jar file is in the same directory as the main class I need to run.
I tried using the classpath command in the terminal
java -classpath pack.jar ClassName
but I got the same error but this time it was in the main thread
Exception in thread "main" java.lang.NoClassDefFoundError
Why does my program work in Eclipse but not on its own?
I guess you'll need to use: java -classpath .:pack.jar ClassName.
NoClassDefFoundError will come if a class was present during compile time but not available in java classpath during runtime.
Its Not Even Close To Being equal to ClassNotFoundException n u mustn't confuse them together, so u need to include the whoe path of the .jar file,
use the previously explained technique to do so, or if it was a jar file that want use frequently without using the call to java -classpath .. thing try importing the file into the OS class path directory.

Failing to execute the jar file using java -jar command

I have created a jar file usign maven2 build. I am trying to run that jar file using the command:
java -jar sample.jar com.app.Test
Test being the class which is having the main method. But i am getting this exception:
Exception in thread "main" java.lang.NullPointerException
at sun.launcher.LauncherHelper.getMainClassFromJar(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Can any one help me to solve this exception and run the jar file?
Thanks in advance.
If you want to run the Test class, you should use
java -cp sample.jar com.app.Test
This way, you add the jar to the classpath and then run the specified main class.
What java -jar does is that it executes a runnable jar file (which defines its own main class in the manifest file). Any parameters after that would not be used to specify the class, but end up in the String array passed to the main method.
So if you have a properly constructed runnable jar file, it should just be
java -jar sample.jar
If you are using Maven, you may need to use maven's install command. You can find the format here
http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
Looks like you do need a manifest and a Main-Class attribute, if you don't have one.
If you are using java 7 a bug is filed here http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7067922

Setting Java Classpath to Load a Class File

I'm new to Java, and I'm unsure how to access a class file located in a specific directory from a separate program jar.
For example, I have an third party jar file located in /, which is supposed to load MyClass located in /mylib/MyClass.class, so I tried running:
java -jar mainprog.jar -classpath "/mylib" MyClass
but I'm getting the error:
Exception in thread "main" java.lang.NoClassDefFoundError: MyClass
Caused by: java.lang.ClassNotFoundException: MyClass
at java.net.URLClassLoader$1.run(URLClassLoader.java:221)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:209)
at java.lang.ClassLoader.loadClass(ClassLoader.java:324)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:269)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:337)
What am I doing wrong?
When you use "-jar" then only the Class-Path attribute defined in the META-INF/MANIFEST.MF file inside the jar file will influence the classpath.
It will also ignore the MyClass argument (or more specifically: interpret it as an argument to the main-class defined in the MANIFEST.MF).
If you simply want to call a class from that jar call it like this:
java -cp mainprog.jar:/mylib MyClass
// or using this one on windows:
java -cp mainprog.jar;/mylib MyClass
In your command line you are trying to run MyClass as a program, which based on your description is not what you want.
You need to figure out what the main class is that is used to execute the program in the jar. You could unpack the jar file with jar -xf mainprog.jar and look at the META-INF/MANIFEST.MF file. It should have an entry that indicates that the main class of the jar is (I can't remember the name of the entry right now).
After than change your command line to something like this:
java -classpath /mainprog.jar:/mylib package.name.to.jar.MainClass

Categories