• Post Reply Bookmark Topic Watch Topic
  • New Topic

Windows 'Party' Code Run Error  RSS feed

 
Rick Deckard
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just bought "Head First Java" and entered the first assignment called "Party". It took a while to figure out which SDK off of Sun's site to use, but I think I got the right one because after I completed entering the code sample, it compiled with no error message. I found a new module call "Party.class" in my directory. However, when I try to run it (java C:\appl\bin\Party) from the command prompt I get this error message:

Exception in thread "main" java.lang.NoClassDefFoundError c:\appl\bin\Party

I compiled the Party.java file in a command prompt window:
javac c:\appl\bin\Party.java and it seemed to complete with no error messages.

I'm running this in a Windows XP environment. Should I be using the Command prompt window to do this? Can anyone explain what the error message means and what I'm doing wrong?

Thanks for any help!
 
Rick Deckard
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found an explanation in the FAQ (should've looked there first) for the Class message. So I set my PATH variable to find the Class. However, now I get the message:

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

In the FAQ it states that the code is missing a "main" statement. Shouldn't that have been included in the code sample in the book?

Anyway, since I can't close this post myself, could an administrator please close this thread?

Thank You
 
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 Rick,

Welcome to JavaRanch!

The argument to "javac" is the path to a *.java file, which is what you've provided; perfect. The argument to "java", however, is the name of a Java class, not the path to a class file, or anything like that. You can supply the path separately, if needed. So, for example, you might use

java Party

(if c:\appl\bin is your current directory,) or

java -classpath c:\appl\bin Party

otherwise.
 
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
Originally posted by Rick Deckard:

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

In the FAQ it states that the code is missing a "main" statement. Shouldn't that have been included in the code sample in the book?


I don't have the book, so I can't say; but maybe the Party class is intended as part of a larger application, or maybe it's just an example of part of a class, with "main" implied but omitted from the text. In the grand scheme of things, very few Java classes have a main; a typical large application can have tens of thousands of classes, and only one needs to have "main" in it!

About closing the thread: we generally don't close answered threads here. Somebody might want to come along and comment.
 
Rick Deckard
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code from the book:
__________________________________________________

import java.awt.*;
import java.awt.event.*;
class Party {
public void buildInvite() {
Frame f = new Frame();
Label l = new Label("Party at Tim's");
Button b = new Button("You bet");
Button c = new Button("Shoot me");
Panel p = new Panel();
p.add(l);
} // more code here ...
}
________________________________________________

In the book, it gives a simple 4 step display of what happens. Step 1 - Enter the source code into a text editor and save it as Party.java. Step 2 - compile it using the command: javac Party.java (it took a while to figure out how to do this since there's not too much explanation in the book and resolve the error messages appeared). Step 3 - a display of the bytecode output. Step 4 - execution of the bytecode using the command: java Party. Step 4 also shows a pretty display of a window that shows the heading "Party at Tim's" with two buttons (captioned "You bet" and "Shoot me").

After all the hoops to get it to compile and the gyrations to get it to execute, I get absolutely NOTHING. Mind you I'm running this in Windows. So from the command prompt I enter "java Party". The command prompt window hesitates for a second or two and then returns with a new prompt. I see no other windows opening, no discernible processes starting, nothing. Is there something wrong with my code or is there a driver or module that's not being linked to?

Thanks
 
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
This looks like some kind of hypothetical example, where they don't intend you to actually follow the steps. This buildInvite() method isn't going to do anything as is; it's missing some really important stuff, like making the Frame visible, and actually adding all those components into it.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
This looks like some kind of hypothetical example, where they don't intend you to actually follow the steps...

I don't have the book in front of me, but I think EFH is correct. This is just a "sketch" to give you an overview of what a program might do. It's not complete. I think there's a little blurb of text saying that.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I have the book in front of me, and here's the blurb at the bottom of page 3...
...this is not meant to be a tutorial... you'll be writing real code in a moment, bur for now, we just want you to get a feel for how it all fits together.

So this is just an overview of the pieces: Source, compiler, code, and virtual machines.
 
Rick Deckard
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the disclaimer now. I took the text in the diagrams as instructions, since the previous page gave me an overview of the process.
 
Rick Deckard
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, forgot to add my thanks for your time explaining to me my ignorance.

Thanks to you both, Ernest and marc!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!