Error while running jar file on system - java

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').

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.

can not execute jar file from cmd

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

How to run a jar file in hadoop?

I have created a jar file using the java file from this blog using following statements
javac -classpath /usr/local/hadoop/hadoop-core-1.0.3.jar -d /home/hduser/dir Dictionary.java
/usr/lib/jvm/jdk1.7.0_07/bin/jar cf Dictionary.jar /home/hduser/dir
Now i have tried running this jar in hadoop by hit and trial of various commands
1hduser#ubuntu:~$ /usr/local/hadoop/bin/hadoop jar Dictionary.jar
Output:
Warning: $HADOOP_HOME is deprecated.
RunJar jarFile [mainClass] args...
2.hduser#ubuntu:~$ /usr/local/hadoop/bin/hadoop jar Dictionary.jar Dictionary
Output:
Warning: $HADOOP_HOME is deprecated.
Exception in thread "main" java.lang.ClassNotFoundException: Dictionary
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.apache.hadoop.util.RunJar.main(RunJar.java:149)
How can i run the jar in hadoop? I have the right DFS Locations as per needed by my program.
I was able to reproduce your problem. The problem is where you are creating the jar.
Basically, the directory that you are packaging into the jar is confusing the jar file in locating the main class file. Instead if you try doing :
/usr/lib/jvm/jdk1.7.0_07/bin/jar cf Dictionary.jar /home/hduser/dir/Dictionary.class
i.e. package the class file specifically into the jar and then run:
/usr/local/hadoop/bin/hadoop jar Dictionary.jar Dictionary
It just works fine provided that you have a main function in your class called Dictionary.
The problem is when you package a full directory inside a jar then the jar also needs to be aware of the directory structure to locate the class file. For this, we need to have a well defined package hierarchy to define the class location. So, when you are packaging /home/hduser/dir/ into the jar, the jar is not aware of the location of the class file which is located deep inside this directory structure. For this you need to add a package name to your .java file according to the directory structure , for example home.hduser.dir and while running the hadoop jar command specify the class name with the package structure, for example home.hduser.dir.Dictionary.
Use the below command to run the hadoop jar file from the CLI.
hadoop jar <jarFileName> <mainClassname> <AnyCommandLineArguements>
I also ran into the same problem and the console does not show much information but just
RunJar jarFile [mainClass] args...
Please check the jar for the package folders location, as a straightforward approach please try is the package starts with com.company...
The "com" folder should be the first level folder when the jar is unpacked

link GetOpt to main jar

I have a folder gnu\getopt contains Getopt.class which is need by ChatDemo.jar for parser argument parameters, when I ran with java -cp xSocket.jar;. ChatDemo.jar it produce:
Exception in thread "main" java.lang.NoClassDefFoundError: gnu/getopt/Getopt
at ChatDemo.main(ChatDemo.java:24)
Caused by: java.lang.ClassNotFoundException: gnu.getopt.Getopt
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 1 more
ChatDemo.java:
import gnu.getopt.Getopt;
ChatDemo Manifest:
Manifest-Version: 1.0
Created-By: 1.6.0_22 (Sun Microsystems Inc.)
Main-Class: ChatDemo
Class-Path: xSocket.jar
Run it with java -classpath "xSocket.jar;.;ChatDemo.jar ChatDemo (no .jar after the name of the main class).
Note that you must add the folder which contains "gnu" to the classpath. Another solution is to put the getopt classes in a JAR and add that to the classpath. Make sure that the folder structure (gnu\getopt) is preserved.
It looks like you are mixing up the two ways of running java. You should either supply a classpath (e.g. using the -cp argument), or launch from a JAR file. You cannot do both.
If you launch using a JAR file, the -cp argument is ignored. In that case, you must specify the complete classpath in the JAR file's manifest.
The manifest in the ChatDemo.jar file apparently doesn't include. You can remedy this in two ways:
Update the manifest classpath to be "Class-path: xSocket.jar ." ... note we use a space to separate the entries here, not ; or :.
Add Getopt.class to the JAR file as gnu/getopt/Getopt.class.
However, if you launch using a JAR file you have to use the -jar option. If you try to your application like this:
java -cp xSocket.jar;. ChatDemo.jar
it will fail telling you that it cannot find a class called ChatDemo.jar ; i.e. it will misinterpret the JAR filename as a qualified classname.
Add Main-Class - as fully qualified (I mean as per package structure) class name of your class which contains main method.

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