NoClassDefFoundError in a java program - java

I compiled the program given below and it was fine. Both class files and the java file are on my Desktop folder. But when I run it using the command "java B" on my terminal from the Desktop, I get the exception as given below. Can anyone please tell me what is wrong with it?
import java.util.*;
public class a<X> {
//code
}
class b{
//code
}
Exception in thread "main" java.lang.NoClassDefFoundError: a
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError indicates that the classloader, which is responsible for dynamically loading classes, cannot find the .class file for the class that you're trying to use. It probably indicates that you haven't set the classpath option when executing your code
for more help see this link

Related

Unable to run a simple hello world

I have this simple app:
package default1;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World333!");
}
}
Compiling and running:
$ default1$ javac HelloWorld.java
$ default1$ java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: default1/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
And this doesn't work either:
$ default1$ java default1.HelloWorld
Error: Could not find or load main class default1.HelloWorld
How to fix it? I've tried to do different thing and couldn't find the way to fix it.
javac and java commands care about directory structure; package hierarchy and directory structure need to correspond to each other. In your case, change directory to the one that contains default1 directory and run:
javac default1/HelloWorld.java
java default1.HelloWorld
Try making the package name the same as the name of the main class.
In this case, you can change package default1; to package helloworld;

Where to install library for Java on Ubuntu command line

I'm trying to get the Charva tutorial to work, but I don't think I'm installing it correctly. I added the .jar files to /jre/lib/ext, but I get this error when I try to run the tutorial file:
Exception in thread "main" java.lang.NoClassDefFoundError: Tutorial (wrong name: tutorial/java/Tutorial)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Am I supposed to be running something else when I compile or am I installing the library the wrong way?
You can add the .jar files to the classpath using:
java -cp "Charva.jar" main.Class
If you need to add multiple jars, or you want it to be better structured in folders, put the jars under lib which is the common thing to do, and use:
java -cp "lib/*.jar" main.Class
If you're using and IDE, tag the question with the specific IDE to get specific instructions for your IDE.
I suggest you build and run your program in your IDE.
Once you have a working program you can build a JAR and when you specify it on the command line with
java -cp {path-to-my-jar}.jar mypackage.MyClass
you can put the JAR where ever you like.

NoClassDefFoundError: WordCount with hadoop-2.2.0 on ubuntu-12.04

I executed wordcount program hadoop-2.2.0 in ubuntu-12.04 desktop and getting this error
Exception in thread "main" java.lang.NoClassDefFoundError: WordCount (wrong name: org/myorg/WordCount)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
at org.apache.hadoop.util.RunJar.main(RunJar.java:205)
I guess that you didn't run your wordcount class correctly
NoClassDefFoundError : This exception indicates that the JVM looked in its internal class definition data structure for the definition of a class and did not find it. This is different than saying that it could not be loaded from the classpath. Usually this indicates that we previously attempted to load a class from the classpath, but it failed for some reason - now we're trying to use the class again (and thus need to load it, since it failed last time), but we're not even going to try to load it, because we failed loading it earlier (and reasonably suspect that we would fail again). The earlier failure could be a ClassNotFoundException or an ExceptionInInitializerError (indicating a failure in the static initialization block) or any number of other problems. The point is, a NoClassDefFoundError is not necessarily a classpath problem.
please follow this link it explains you how to run a MapReduce program correctly

Exception in thread "main" java.lang.NoClassDefFoundError: Huffman (wrong name: p3/Huffman)

I am not sure how to fix this but I think it's rather a classpath error. I need to know where to run the following command if this is my file structure:
monas-mbp:p3 mona$ java Huffman c p3code.txt p3input.txt p3output.tx
Exception in thread "main" java.lang.NoClassDefFoundError: Huffman (wrong name: p3/Huffman)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Well, I already copied the Huffman.class from the following path actually to the current directory which is /Users/mona/java/p3/src/p3 using
cp /Users/mona/java/p3/bin/p3/Huffman.class Huffman.class
so here's what I have in Eclipse. I created project p3 and then it automatically created package p3 while previously it used to create default package.
Any idea what's the fix? Also why it didn't create the default package automatically like always?
[UPDATE]Well, I didn't expect to receive such confusing answers specially because both project name as well as the automatically package name is p3. Can you please be more elaborate and detailed about your answers? Here's the structure created by Eclipse in my computer:
Also when I run the command in bin/p3 I get this result:
monas-mbp:p3 mona$ pwd
/Users/mona/java/p3/bin/p3
monas-mbp:p3 mona$ java Huffman c p3code.txt p3input.txt p3output.tx
Exception in thread "main" java.lang.NoClassDefFoundError: Huffman (wrong name: p3/Huffman)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
I already copied the Huffman.class from the following path actually to the current directory which is /Users/mona/java/p3/src/p3 using
cp /Users/mona/java/p3/bin/p3/Huffman.class Huffman.class
now execute java from parent directory of p3 instead from p3, if you have copied it to current directory then execute following set of commands
mkdir p3
mv Huffman.class p3/
java p3.Huffman c p3code.txt p3input.txt p3output.tx
so structure should be relative to current directory
p3
\
|
|
------Huffman.class
note: you are using eclipse to write code why not launch from it as well
it automatically created package p3 while previously it used to
create default package.
Huffman class must have a package declaration as p3. So when you have copied the file in eclipse project, eclipse either had asked you to move the file to p3 package or remove the package declaration from file.
Depending whether other files(Heap,HeapSandBox etc) in your project need to be p3 project or not, you may have to move the files.
To run your Huffman class, you need to use package.classname.
java p3.Huffman -cp p3code.txt p3input.txt p3output.tx

Class file not being read

When I compile a java file, it gets compiled and I am able to see the class file in the same location. But when I try to run it using the java command, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: UDPServer (wrong name: com/prasanna/udp/UDPServer)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:787)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:447)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
How should I correct this error? How should I make the java program know the path of this class file?
PS: I am very new to command line execution of a Java file.
Assuming the class is in the package com.prasanna.udp and is named UDPServer. Assuming the com/prasanna/udp directory tree, containing the .class files, is located in /home/Prasanna/java/classes, the command line should be:
java -cp /home/Prasanna/java/classes com.prasanna.udp.UDPServer
If your class depends on a jar file located in /foo/bar/somelib.jar, iy should also be placed in the classpath:
java -cp /home/Prasanna/java/classes:/foo/bar/somelib.jar com.prasanna.udp.UDPServer

Categories