• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having problem w/ Head First Java (the very first example given on page 3)  RSS feed

 
woojae eum
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's my first time learning java. I decided to learn from this book but this book is giving me a headache. I have spent more than 10 hours to figure out how to run this thing. I have succeeded in compliling it by typing javac Party.java on command prompt, but there's an error when I try to run it by typing java Party. The error message is

Exception in thread "main" java.lang.NoSuchMethodError: main

I did have look at replies on other topics that explained why the message 'NoSuchMethod Error: main' appeared for many different reasons, but I've tried what they said, it didn't work. The source code looks like this:
-------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
class Party{
public void buildlnvite(){
Frame f = new Frame();
Label l = new Label("Party at Tim's");
Button b = new Button("You bet");
Button c = new Button("Schoot me");
Panel p = new Panel();
p.add(l);
}
}
------------------------------------------------------------
and then I saved it as Party.java.
I know that this source code is missing 'void main', but it is supposed to work because the author says so. I've tried other source codes that had 'void main' in them and they worked, but I really should know how to run this thing when it doesn't have 'void main' because many examples given in this book are written that way.

I think the reason why it doesn't work is because I did not set up java properly. Head First Java says setting up java requires the following:

#1. a 1.5 or greater Java 2 Standard Edition SDK
#2. API documentation since it is not included in SDK (according to the book.)
#3. text editor
#4. Adding an entry to your PATH environment variable that points to the /bin directory inside the main Java directory.

The way I followed it was like this:
I downloaded J2SE v 1.4.2_08 SDK from java.sun.com and installed
C:\j2sdk1.4.2_08
here.

I also downloaded J2SE 1.4.2 Documentation which I think is the API documentation and extracted
C:\Documents and Settings\admin\My Documents\downloaded files\j2sdk-1_4_2-doc\docs
here. This file has files in it called api,images,guide,relnotes,install-notes,tooldocs. (I have no idea what these are)

I didn't do anything about #3 since I had notepad.

For #4, I chose start, setting, control panel, double-click system,advanced tab, environment variable, and then I edited the already existing environment variable PATH(on the bottom, not user variable) and gave this directory C:\j2sdk1.4.2_08\bin
and selected ok.

After failing to run it on Command prompt, since the book says to run the program by starting the Java Virtual Machine with the Party.class file, I tried to find JVM in C:\j2sdk1.4.2_08, but I couldn't find it. Neither way worked.

What would I possibly doing wrong? setting up JAVA?
Thanx in advance.
 
madhup narain
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I havent read the book that you have mentioned , but AWT seems way too far to start learning as a beginner.
I suggest that you try simpler programs like the hello world program.
Alos if possible try getting som java tutorials from the WEB.

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

compile this as javac HelloWorld.java
and then run this as java HelloWorld

i suggest that you read the first few chapters before you dive deep into writing code.
 
madhup narain
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this should run.. apparently.. if it gives some problems then your class path is not set.

import java.awt.*;
import java.awt.event.*;
class Party{
public void buildlnvite(){
Frame f = new Frame();
Label l = new Label("Party at Tim's");
Button b = new Button("You bet");
Button c = new Button("Schoot me");
Panel p = new Panel();
f.setLayout(new FlowLayout());
f.add(l);
f.add(b);
f.add(c);
f.setVisible(true);
f.pack();

}
public static void main(String args[]){
Party oParty=new Party();
oParty.buildlnvite();
}
}
 
woojae eum
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your help. I'll read first few chapters and visit tutorials as you've said.
 
Marilyn de Queiroz
Sheriff
Posts: 9081
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by woojae eum:
I think the reason why it doesn't work is because I did not set up java properly. Head First Java says setting up java requires the following:

#1. a 1.5 or greater Java 2 Standard Edition SDK
#2. API documentation since it is not included in SDK (according to the book.)
#3. text editor
#4. Adding an entry to your PATH environment variable that points to the /bin directory inside the main Java directory.

The way I followed it was like this:
I downloaded J2SE v 1.4.2_08 SDK from java.sun.com and installed
C:\j2sdk1.4.2_08
here.


J2SE v 1.4.2_06 is not the same as "a 1.5 or greater Java 2 Standard Edition SDK"

I also downloaded J2SE 1.4.2 Documentation which I think is the API documentation and extracted
C:\Documents and Settings\admin\My Documents\downloaded files\j2sdk-1_4_2-doc\docs
here. This file has files in it called api,images,guide,relnotes,install-notes,tooldocs. (I have no idea what these are)


The api folder contains the JavaDoc you'll see mentioned.

After failing to run it on Command prompt, since the book says to run the program by starting the Java Virtual Machine with the Party.class file, I tried to find JVM in C:\j2sdk1.4.2_08, but I couldn't find it. Neither way worked.

What would I possibly doing wrong? setting up JAVA?


Even though you have used a different JDK than what you were told to use, as far as I know, the code should run ... but! you will need a public static void main method.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the error message closely. It tells you the exact problem encountered. In English, the JVM cannot find a main() method in that class. I think that either you didn't type in the whole example (does it go on to the next page?) or there is another class (also perhaps on the next page) that contains the main() method. I haven't read this book, so I don't know exactly what the problem may be. These suggestions are just my own educated guesses.

Don't get too discouraged. Keep reading your book and let us know how it goes. We will be happy to continue answering your questions.

Good luck and welcome to the Ranch!

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!