The moose likes Beginning Java and the fly likes Fresh SDK Install Won't Compile? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Fresh SDK Install Won Watch "Fresh SDK Install Won New topic

Fresh SDK Install Won't Compile?

Kevin Brennan

Joined: Oct 23, 2003
Posts: 27
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 Source)
at Source)
at$100(Unknown Source)
at$ Source)
at Method)
at 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

Joined: Jul 08, 2003
Posts: 24199


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!

[Jess in Action][AskingGoodQuestions]
Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
Hi Kevin,
What you have done is you probably have your source file name as 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
class name -:Helloworldapp

Then follow the foll procedure,
javac 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.


Have the class name as ,
class Hellowaorldapp
and the same source file name

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
class Sample1

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

Now if you give as an argument to the compiler as
the compiler takes the source file...scans the classes inside 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.
I agree. Here's the link:
subject: Fresh SDK Install Won't Compile?
It's not a secret anymore!