Exception in thread main java.lang.NoClassDefFoundError:

  • Java
  • Thread starter tjaeger
  • Start date
  • Tags
    Thread
In summary, the conversation discusses an error encountered while trying to compile Java programs in Secure Shell. The error is caused by a missing class and can be resolved by correctly setting up the .bash_profile. The suggested solution is to compile with the command "javac [javafilename].java" and then run using "java -classpath . [javafilename]".
  • #1
tjaeger
8
0
Exception in thread "main" java.lang.NoClassDefFoundError:

I was getting this error trying to compile some java programs in SSH Secure Shell. Then I made a simple "Hello World" program in my home directory to see if it would compile by typing 'java HelloWorld' but all I got was:

Caused by: java.lang.ClassNotFoundException: HelloWorld
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

I looked it up on google and a website said to try compiling by typing
'java -classpath.HelloWorld' but that gave me the error:

Unrecognized option: -classpath.HelloWorld
Could not create the Java virtual machine.

I haven't used Secure Shell in a long time, any help is appreciated.
 
Technology news on Phys.org
  • #2


You have to type "javac HelloWorld.java," assuming you have your .bash_profile set up correctly. Otherwise, it's trying to run a program that doesn't exist.

Also, make sure $HOME/.bash_profile has something like this in it:

JAVADIR="j2re1.6.2_xx" (note you must ensure that the proper version and release number are coded here)
export JAVADIR
PATH=/usr/java/$JAVADIR/bin:$PATH:$HOME/bin:/sbin
export PATH
CLASSPATH=/usr/java/$JAVADIR/lib:/usr/NetRexx/runlib/NetRexxR.jar
export CLASSPATH
 
  • #3


Try this...

compile with this command

javac [javafilename].java

then after u get the classfile (the bytecode)

run using :

java -classpath . [javafilename]

this will specify that the classpath is the current directory ( . ----> refers current working directory).
 

1. What is a NoClassDefFoundError in Java?

A NoClassDefFoundError is an exception that occurs when the Java Virtual Machine (JVM) cannot find a particular class at runtime. This can happen if the class was present during compilation but is not available during execution, or if there is an error in the classpath.

2. What causes a NoClassDefFoundError in Java?

Some common causes of NoClassDefFoundError include missing or incorrect classpath settings, missing or corrupt JAR files, and changes to the classpath after compilation. It can also occur if there are errors in the code that prevent the class from being loaded.

3. How can I fix a NoClassDefFoundError in Java?

To fix a NoClassDefFoundError, you should first check your classpath settings to ensure that all necessary dependencies are included. If you are using external libraries, make sure they are properly imported and available. You can also try cleaning and rebuilding your project, as well as checking for any errors in your code.

4. Can a NoClassDefFoundError be caught and handled in Java?

No, a NoClassDefFoundError cannot be caught and handled like a regular exception. This is because it indicates a problem with the classpath or code, which cannot be resolved at runtime. The best way to address it is to fix the underlying issue that caused the error.

5. How can I prevent a NoClassDefFoundError in Java?

To prevent a NoClassDefFoundError, it is important to carefully manage your classpath and dependencies. Make sure all necessary libraries and JAR files are included and properly imported. It is also a good idea to regularly clean and rebuild your project to catch any potential errors. Additionally, ensure that your code is free of any mistakes that could prevent a class from being loaded.

Similar threads

  • Programming and Computer Science
Replies
2
Views
5K
Back
Top