Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

error: java.lang.NoClassDefFoundError

  Asked By: Wayne    Date: Sep 29    Category: Java    Views: 1332
  

I'm trying to start java (again) and have had to resort to starting
over because it's been two years since I worked with it. I have a
JAVA instruction book and here is the very first app it gives

--------------
//file: HelloJava1.java

public class HelloJava1 extends javax.swing.JComponent {

public static void main(String[] args){
javax.swing.JFrame f = new javax.swing.JFrame("HelloJava1");
f.setSize(300, 300);
f.getContentPane().add(new HelloJava1());
f.setVisible(true);
}

public void paintComponent(java.awt.Graphics g){
g.drawString("Hello, Java!", 125, 95);
}

}
--------------

I know this used to work just fine for me ... but now, for some reason
EVERY TIME I try to execute this (it compiles fine) I get this error:

java.lang.NoClassDefFoundError

This is driving me absolutely nuts! I've tried compiling this using
jdk1.2, jdk1.3.1 and jdk1.4.1 and I keep getting the same error. I
downloaded each version directly from Sun so I assume I got all of the
neccessary files. My book came with a cd with all of the samples on
it and it gives me this same error whether I type out the code myself,
or if I copy it directly from the CD to my jdk directory.

ANY IDEAS? I feel like I am going to punch a hole in the wall because
I wanted to get started with this real quick just for fun tonight and
it has turned into a 4 hour hassle.

Share: 

 

12 Answers Found

 
Answer #1    Answered By: Rene Sullivan     Answered On: Sep 29

You may know this already so I'm sorry if I'm insulting your intelligence,
but the first thing that comes to my mind is to check your classpath. The
environment variable called "CLASSPATH" needs to be set, and it needs to
contain at least 1 path: the "lib" directory under your SDK install
directory. For instance, if you installed the SDK under C:\j2sdk1.4.1\,
your CLASSPATH should be set to "C:\j2sdk1.4.1\lib".

Plus, when you run your "hello java" application, you need to make sure the
classpath ALSO includes the directory where your HelloJava1.class file
exists. Rather than add this to your environment variable, you can just
set this at the command line (when you run "java") by using the -classpath
switch. For example, say you're HelloJava1.class path is in the
C:\programs\Java\ directory. You'd run java  like this:

On Windows:
java -classpath %CLASSPATH%;C:\programs\Java\ HelloJava1

If you're running Linux, of course the directory names will be different,
such as:
java -classpath $CLASSPATH:~/programs/Java/ HelloJava1

Hope this helps. Again, sorry if this is all old news to you, but that's
what I've gotten stuck on before. class  paths can be tricky (for me anyway).

 
Answer #2    Answered By: Milton Robinson     Answered On: Sep 29

I'd bet money that your class  paths are not set up right.

try adding . to your classpath (just a .)

What that does is tell your computer to look into the directory you are
currently in.

 
Answer #3    Answered By: Vinit Online     Answered On: Sep 29

I cant see why it doesnt work for you, i tried it here and it worked
fine for me. I can only think that you dont have java  installed
correctly. Only a beginner myself, so i cant help you further sorry.

 
Answer #4    Answered By: Jake Williams     Answered On: Sep 29

Donot pull hair.
Answer is simple.
include the javax  file in the path
e.g, path = %path%;c:\jdk1.2\bin;

 
Answer #5    Answered By: Muriel Dunn     Answered On: Sep 29

I tried executing it in jdk1.4.1 and it is working successfully.
Since you have tried with the souce code given in CD the only viable option is
the class  name and the file name doesn't match.
Just try checking the name you have given while saving the java  file.
Java is case sensitive and I have come across this error  when the
Java class file is created with some name other than the name of the
public class declared inside it.

 
Answer #6    Answered By: Trae Thompson     Answered On: Sep 29

i mean swing  and all what u think is needed and try
it a gain by the way it worked at my machine fine
without importing.

 
Answer #7    Answered By: Felix Gray     Answered On: Sep 29

I cut-n-paste your code into Eclipse 2.1 in a directory with
default or no package using jdk 1.4.1_03 and it ran just fine. Are you doing
your development from the command line?

 
Answer #8    Answered By: Sultana Tabassum     Answered On: Sep 29

The compilation command is
javac HelloJava1.java

Try this command
java -cp . HelloJava1

 
Answer #9    Answered By: Hollie Hughes     Answered On: Sep 29

java -cp . HelloJava1 worked! What does it mean?

 
Answer #10    Answered By: Jackson Williams     Answered On: Sep 29

"-cp ." means you are setting or making your classpath point to the current
directory your in or where you are invoking the command.

 
Answer #11    Answered By: Ethan Evans     Answered On: Sep 29

is
there a way so I don't need to type "-cp . " everytime I want to
practice running my code? Right now I am using jdk1.3.1 and ran the
install into folder c:\jdk\

 
Answer #12    Answered By: Komal Mohammad     Answered On: Sep 29

well, you can include "." to your CLASSPATH environment variable..

 
Didn't find what you were looking for? Find more on error: java.lang.NoClassDefFoundError Or get search suggestion and latest updates.




Tagged: