• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Fresh SDK Install Won't Compile?

 
Kevin Brennan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a super-new beginner. My basic source is this code:

* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}


It compiles with no error, but when I try to run it, I got:

F:\Learning Java>java helloworldapp
Exception in thread "main" java.lang.NoClassDefFoundError: helloworldapp (wrong
name: HelloWorldApp)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(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)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to the Ranch!

Even though Windows isn't case-sensitive, Java is. This funny message comes from the fact that Windows can't tell the difference between files named helloworldapp.class and HelloWorldApp.class . In your source file, you named the class HelloWorldApp, so when you run it, that's the name you must use, or Java thinks something is corrupt.

java HelloWorldApp

will work fine!
 
Archies Gomes
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kevin,
What you have done is you probably have your source file name as helloworldapp.java and the class in it as
class Helloworldapp
{
}
Its not cumpolsory that you keep them same ...if your class name and the source file name differs as you have done here as
source file name:-helloworldapp.java
class name -:Helloworldapp


Then follow the foll procedure,
javac helloworldapp.java//This is source file's name

not the code is compiled and you get Helloworld.class file in your current folder ...Now to run it give
java Helloworldapp//This is the class's file's name which you want to run(it must have main())


Here what happens is For compiling you give the source file's name .....and then for running you provide the name of the class where your main() method is present.



OR

Have the class name as ,
class Hellowaorldapp
{
}
and the same source file name Helloworldapp.java.




What basically java does is ,
For compilation..It takes the file name as the argument and then scans for all the classes inside it and then builds their class files.eg-:

Test.java
class Sample1
{


}
class sample2
{
public static void main(String ag[]){}
}
class sample3
{
}
//All three classes in one file Test.java


Now if you give Test.java as an argument to the compiler as
javac Test.java
the compiler takes the source file...scans the classes inside it...here there are three classes sample1,sample2,sample3.
So it builds sample1.class,sample2.class sample3.class
Now you can provide sample2 as an argument to the interpreter (JVM) as ,

java sample2

Here sample2.class contains the compiled code which includes the main method and thus the execution begins....


I hope it clears atleast some of your doubt regarding the java.lang.NoClassDefFoundError(class defination not found) error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic