• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code examples from "Head first: Java" does not work for me  RSS feed

 
Fredrik Wee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I'm so glad I started taking Head First: Java too myself and I'm at that point were actual code is supposed to be entered and compiled.
I know the book got some years on it but none of the examples works for me when I'm trying to compile them.
The simplest code I'm using is.
public class MyFirstApp {
public static void main (String[] args) {
System.out.println("I Rule!");
System.out.println("The World");
}
}

And the errors I get trying to "javac myfirstapp.java":
hello-world.java:9: error: class MyFirstApp is public, should be declared in a f
ile named MyFirstApp.java
public class MyFirstApp {
^
1 error


Are there anything fundamental that isn't included in the book that I need to change up in order to compile the applications? Btw, I'm using JDK SE 8u5.

Thank for any anwers replied and really thrilled to get my coding in Java started!

Br,
t00nz
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I am not sure if the book describes it or not, but the error message tells you the problem. All public classes must be in a file with the same name as the class. So you have a class named MyFirstApp, and so the file must be named MyFirstApp.java. Note that Java is case sensitive, so myfirstapp is not the same as MyFirstApp (which is probably different than Windows which doesn't really care about the case of file names and will treat the two the same).
 
Fredrik Wee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fredrik Wee wrote:Hello everyone,
I'm so glad I started taking Head First: Java too myself and I'm at that point were actual code is supposed to be entered and compiled.
I know the book got some years on it but none of the examples works for me when I'm trying to compile them.
The simplest code I'm using is.
public class MyFirstApp {
public static void main (String[] args) {
System.out.println("I Rule!");
System.out.println("The World");
}
}

And the errors I get trying to "javac myfirstapp.java":
hello-world.java:9: error: class MyFirstApp is public, should be declared in a f
ile named MyFirstApp.java
public class MyFirstApp {
^
1 error


Are there anything fundamental that isn't included in the book that I need to change up in order to compile the applications? Btw, I'm using JDK SE 8u5.

Thank for any anwers replied and really thrilled to get my coding in Java started!

Br,
t00nz



Lol! It too easy I just changed the name to MyFirstApp.java and it worked. Sorry for even bothering you with this.

Thanks for the informative answer as well Steve Luke. Now its ON!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!