JAVA: Unable to run a jar file - java

I'm new to JAVA and trying unable to figure out what's wrong with the project I created.
I created a JAVA project in eclipse and exported a jar (not runnable jar).
I unchecked .project and .classpath file options while exporting. Now I'm trying to run that jar and getting NoClassDefFoundError:
I'm pasting the command and the errors below:
C:\Users\Anjali>"C:\Program Files\Java\jre7\bin\java.exe" -classpath D:\Web\Hbas
eGateway\bin;C:\Users\Anjali\Downloads\Hbase\hbase-0.94.5-security.jar;C:\Users\
Anjali\Downloads\Hbase\lib\hadoop-core-1.0.4.jar;C:\Users\Anjali\Downloads\Hbase
\py4j0.7.jar -jar D:\Web\HbaseGateway\bin\HBaseGateway.jar
Exception in thread "main" java.lang.NoClassDefFoundError: py4j/GatewayServer
at hbase.gateway.HBaseGatewayEntryPoint.main(HBaseGatewayEntryPoint.java
:22)
Caused by: java.lang.ClassNotFoundException: py4j.GatewayServer
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Now, interesting thing is I'm able to run the main class using the following command.
In the below command I removed the -jar switch and passed the main class to java.exe.
C:\Users\Anjali>"C:\Program Files\Java\jre7\bin\java.exe" -classpath D:\Web\Hbas
eGateway\bin;C:\Users\Anjali\Downloads\Hbase\hbase-0.94.5-security.jar;C:\Users\
Anjali\Downloads\Hbase\lib\hadoop-core-1.0.4.jar;C:\Users\Anjali\Downloads\Hbase
\py4j0.7.jar hbase.gateway.HBaseGatewayEntryPoint
Gateway Server Started
Why NoClassDefFoundError is being thrown? Am I missing something here?

For -jar option to work, you must add Main-class key in manifest.
Refer the java command documentation http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html
-jar option
Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name.
In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point.

When you use the -jar option, the classpath is read from the MANIFEST.mf file as well. From the docs for the -jar option:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
This means that you have to add all your dependencies in a Class-Path section of the manifest file.

Related

Problem accessing one jar file from another

I've written a Java program that uses classes from the Apache PDFBox jar application. I have my compiled classes and the PDFBox jar file in on directory. I can run this successfully:
java -cp .;pdfbox-app.jar Athena NPCGenerator -pdf
However, when I jar up my own program and try to run it in the same place in a similar fashion, this fails:
java -cp .;pdfbox-app.jar -jar Athena.jar NPCGenerator -pdf
Error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdm
odel/PDDocument
at CharacterPDF.writePDF(CharacterPDF.java:49)
at NPCGenerator.printToPDF(NPCGenerator.java:302)
at NPCGenerator.makeAllNPCs(NPCGenerator.java:278)
at NPCGenerator.main(NPCGenerator.java:316)
at Athena.runApp(Athena.java:88)
at Athena.main(Athena.java:104)
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocumen
t
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
What do I need to do to fix this?
If you use the -jar flag to java it expects a executable jar. It will then ignore the classpath on the command line and read the classpath from the jars manifest.
You can either add pdfbox-app.jar to the manifest in Athena.jar together with the main-class attribute. You can even specify a relative or absolute path to the jar in the manifest.
Main-Class: Athena
Class-Path: pdfbox-app.jar
Or you can not use -jar and add Athena.jar to your classpath via the command line. In this case you need to also specify the main class on the command line as java will not read the manifest in this case.
java -cp .;pdfbox-app.jar;Athena.jar Athena NPCGenerator -pdf

NoClassDefFoundError, in the same directory why the program can't find the jar

I write a Java program that read text from excel.So I import some jar like this: poi-3.10-beta2-20130904.jar, poi-ooxml-3.10-beta2-20130904.jar, etc., I can run the program correctly in eclipse.But when I package this program with maven to the directory(C:\workspace2\change\bin),
Then I run this program in command like this :
C:\workspace2\change\bin>java GenerateVar
it occurs this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Cell
at GenerateVar.execute(GenerateVar.java:59)
at GenerateVar.main(GenerateVar.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Cell
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
You need to specify classpath with all other jars/classes you use in your program.
E.g. Setting multiple jars in java classpath
When you run it from eclipse in the console there's a command that could be used to run the project, you could copy/paste it from the console to your command line.
In the command java GenerateVar you didn't set -cp option. With this option the command line will look like
java -cp poi-3.10-beta2-20130904.jar poi-ooxml-3.10-beta2-20130904.jar ... GenerateVar
Assumed the libraries are in the current dir.
How to use maven plugins to add dependency to your project and build jar you can find here.
See also this answer if you want to modify manifest.mf manually.
over, I change the way do what I want to do. I use the fat jar which is the eclipse plugin to package the program

Error while running jar from command prompt

I'm using NetBeans IDE to code a java program. When I run the program from NetBeans it works perfectly but when I run it from command prompt I get the following:
Exception in thread "main" java.lang.NoClassDefFoundError: javaapplication/Main
Caused by: java.lang.ClassNotFoundException: javaapplication.Main
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: javaapplication.Main. Program will exit.
I used another program (myEclipse) to build the program but the exact thing happened, I'm only able to run the program from the IDE and not command prompt.
Well, is javaapplication.Main meant to be the main class? If not, it's probably a mistake in the manifest file.
If it is meant to be the main class, look inside the jar file (e.g. by extracting it). Look for a directory called javaapplication which should have a file called Main.class in.
One of those two things is wrong, but we can't really tell what it is from here - only you can.
This normally happens when your MANIFEST.MF file is not correct. It is located in your *.jar file.
An example from one of my projects:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 14.2-b01 (Sun Microsystems Inc.)
Class-Path: lib/log4j-1.2.14.jar lib/mysql-connector-java-5.1.11-bin.jar
Main-Class: at.package.to.main.class.MainClass
For more Information see here:
http://java.sun.com/developer/Books/javaprogramming/JAR/basics/manifest.html
http://en.wikipedia.org/wiki/Manifest_file
To change your MANIFEST.MF file should fix your problem.
EDIT: in response to "Jon Skeet": I'm assuming that you have a class with the
public static void main(String args[]){ ... }
method. If you don't have a mainclass - forget my answer and create a Main class ;-)

java - missing main class

I'm trying to run one of the very first examples from the book "Head First Java";
public class MyFirstApp {
public static void main (String[] args){
System.out.println("I Rule!");
System.out.println("The Worlds!");
}
}
"javac" created a .class file from the .java file - but "java" complains about a "missing main class" when trying to run the .class file (i also tried java -cp . "..." with the same result):
C:\>java \hfj\MyFirstApp.class
Exception in thread "main" java.lang.NoClassDefFoundError: \hfj\MyFirstApp/class
Caused by: java.lang.ClassNotFoundException: \hfj\MyFirstApp.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: \hfj\MyFirstApp.class. Program will exit.
You need to run it as
javac MyFirstApp.java
java MyFirstApp
From the directory where MyFistApp.java lives.
'javac' calls the compiler - to that you need to pass your .java file.
'java' will run the compiled code - to that you pass the name of your compiled file, but without any extension: "java MyFirstApp"
Specifying the full path of the file should work when you are not in that directory. But are you in the directory that holds the javac and java programs? If not, those may also need absolute paths if you have not put them on your PATH variable.
What ts the package name? Maybe you have something like
package org.test;
in your header?

NoClassDefFound when running a jar

I built a jar using IntelliJ, setting the main class properly.
When I run "java -jar foo.jar" from the command line (Windows), I get an exception that claims the main file is missing. The main class looks something like:
package mypackage;
public class LockUtil {
public static void main(String[] args) {
...
I'm getting the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: mypackage/LockUtil
Caused by: java.lang.ClassNotFoundException: mypackage.LockUtil
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: mypackage.LockUtil. Program will exit.
The manifest file contains:
Manifest-Version: 1.0
Created-By: IntelliJ IDEA
Main-Class: mypackage.LockUtil
And the jar contains the appropriate directory structure with the .class file.
If you do jar -tf foo.jar, do you see something like this?
META-INF/
META-INF/MANIFEST.MF
mypackage/
mypackage/LockUtil.class
Could it be that there is another directory level in there somewhere?
You can be sure that Java knows the main file is there by building the jar file with something like this:
jar cfe foo.jar mypackage.LockUtil mypackage/LockUtil.class
You're trying to execute mypackage.LockUtil, but you should use mypackage.locking.LockUtil (note the package statement at the beginning of the class.).
Another possibility is that you have moved the class and forgot the update the package statement.
Does LockUtil have a dependency on another class that is not resolvable, thereby not allowing LockUtil to load?
It seems that the name of your package is mypackage.locking and not only mypackage
It appears that your main-class definition in your manifest is pointing at mypackage/LockUtil rather than mypackage/locking/LockUtil.
-Rick

Categories

Resources