• Post Reply Bookmark Topic Watch Topic
  • New Topic

New Beginner-error at complie stage-please help  RSS feed

 
Andrew Wilcock
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning java from the "Head First " book, and have downloaded the J2SDK with netbeans from the Sun web site. I am trying to run the MyFirstApp on page 9, but keep getting two error messages

1) The code is as follows

public class MyFirstApp {

public static void main (String [] args) {
System.out.println("I rule!");
System.out.println("The World");
}

}
This generates an error message saying that class MyfirstApp is public, should be declared in a file named MyFirstApp.java. The code was typed out in m/s notepad and saved as myfirstapp.java. The book does not give any other code to declare the file. What am I doing wrong?

2) If I remove the public word, I am able to compile the programme, but then have an error message that says "Exception in thread "main" java.lang.NoclassdefFoundError: myfirstapp". This statement appears for all applications I try to run. The command I am using at the command line is "java myfirstapp" or whatever name I have used for the file.

I am using windows xp. All files are stored in the bin directory of the jdk15.0_02 directory. All files are compiled and launched from here as well.

I would be very grateful if someone could help, as I can't get past page 9 until I sort this out. I have tried looking on the sun web site for the answer, I have downloaded numerous documents from their web site to sort this out, and so far not had much luck.

Thanking you in anticipation.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew

Welcome to JavaRanch!

Java is case-sensitive, even if Windows is not. A public class needs to be in a file named with exactly the same capitalization, which would fix your first error. And when you run a class, you must use exactly the capitalization of the class name that appears in the *.java file, which would fix your second error.
 
Simon Birch
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew, welcome to JavaRanch.

Java is case sensitive. All you need to do is make the file name 'MyFirstApp.java' rather than 'myfirstapp.java'.

Incidentally, this is also the reason you get the Exception statement when you remove 'public' from the program. You are trying to use a class called 'myfirstapp', but its actual name is 'MyFirstApp'.

Hope that helps.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All files are stored in the bin directory of the jdk15.0_02 directory.

just my two cents:
i guess that's not a good idea: keep you files apart, in a especific folder (ex: MyJavas). The way you'r doing it you may delete something unwanted...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!