-
Starting with Java: getting weird error msg
Hi,
I just got the book Teach Yourself Java 2 Platfrom in 21 days, Professional Reference Edition. I typed in and compiled the following code in Notepad:
Code:
class Jabberwock {
String color;
String sex;
boolean hungry;
void feedJabberwock() {
if (hungry == true) {
System.out.println("Yum -- a peasant!");
hungry = false;
} else
System.out.println("No, thanks -- already ate.");
}
void showAttributes() {
System.out.println("This is a " + sex + " " + color + " jabberwock.");
if (hungry == true)
System.out.println("The jabberwock is hungry.");
else
System.out.println("The jabberwock is full.");
}
public static void main (String arguments[]) {
Jabberwock j = new Jabberwock();
j.color = "orange";
j.sex = "male";
j.hungry = true;
System.out.println("Calling showAttributes ...");
j.showAttributes();
System.out.println("-----");
System.out.println("Feeding the jabberwock ...");
j.feedJabberwock();
System.out.println("-----");
System.out.println("Calling showAttributes ...");
j.showAttributes();
System.out.println("-----");
System.out.println("Feeding the jabberwock ...");
j.feedJabberwock();
}
}
It seems straightforward enough. Except that I get the following error message after typing java Jabberwock:
Exception in thread "main" java.lang.NoClassDefFoundError: Jabberwock
The code compiles without an error message with javac Jabberwock.java
Any ideas as to what's causing the message to appear?
Thanks.
P.S.
I've checked the books web site and it does not mention that the error should turn up. Much less how to fix it.
-
Ok, this is even weirder.
I downloaded and installed JPad.
Opened the Jabberwock.java and compiled and ran it with JPad.
No error messages turned up in the Interactive window.
-
it's a simple problem when you know the way java works, so ill put it as easy as i can:
your java needs to know where to find its class files in order to run things. WHen you install java it sets up a thing called the CLASSPATH. when running any java program, java looks at all the entries on that path, and uses them as base directories in order to try and find class files. mine looks like this:
CLASSPATH=C:\j2sdk1.4.2\jre\lib\rt.jar;c:\javamail-1.3.1\mail.jar;c:\jaf-1.0.2\activation.jar;.
if there are any spaces; the bulletin board software added them, not me
so when i run a java class, and it uses String, and other things, java starts looking.. when it looks in rt.jar, it finds most things.. but before it even starts looking for the classes i use in my program, it starts looking for the class itself.
when i ran your program it started looking for Jabberwock.class,
it looked in C:\j2sdk1.4.2\jre\lib\rt.jar, didnt find it..
it looked in c:\javamail-1.3.1\mail.jar, didnt find it..
and so on, until it got to the part of the classpath that said:
.
that single . means "the current directory"
so when i'm in d:\javawork\newfolder1\ and i type java Jabberwock
it looks for d:\javawork\newfolder1\Jabberwock.class
on my system it worked great.. on yours, your classpath probably doesnt have a . on the end.. so java finished looking in all the places it was supposed to (as ordered by the CLASSPATH) and didnt find anything called Jabberwock, so it gave you a "no class definition found error"
to resolve:
close all dos windows
right click My COmputer
choose properties
choose advanced
choose environment variables
in the second pane, look for CLASSPATH, and edit the variable to include a . at the end. NOTE that the classpath entries are separated by semicolons, so you must edit your classpath from <whatever> to <whatever>;.
OK all wndows, open a new dos box and try again.. let em know if it dont work
(ps; jpad automatically changed the classpath temporarily, and thats why it worked in jpad)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks