Google Answers Logo
View Question
 
Q: Error running java program ( No Answer,   2 Comments )
Question  
Subject: Error running java program
Category: Computers > Programming
Asked by: nick685-ga
List Price: $12.50
Posted: 13 Nov 2005 18:41 PST
Expires: 13 Dec 2005 18:41 PST
Question ID: 592633
I am an intro to computer science student and need to write a class. 
When I write the class I am able to compile it but when i try to run
it i receive this error message:
Exception in thread "main" java.lang.NoClassDefFoundError:
How do I correct this error?

Request for Question Clarification by secret901-ga on 13 Nov 2005 18:59 PST
Perhaps it'd be more helpful if you post the source code of your
class, if it's not too long.

secret901-ga
Google Answers Researcher

Clarification of Question by nick685-ga on 13 Nov 2005 20:52 PST
public class Point {

    // Instance variables/fields/slots/member variables

    private int x;
    private int y;
    private String color;


    // Constructors
    public Point() {
	x = 0;
	y = 0;
	color = "black";
    }

    public Point(int initX, int initY, String initColor) {
	x = initX;
	y = initY;
	color = initColor;
    }

    // Methods/functions/bahavior
    public int getX () {
	return x;
    }

    public int getY () {
	return y;
    }

    public String getColor () {
	return color;
    }

    public void setX (int newX) {
	x = newX;
    }

    public void setY (int new_y) {
	y = new_y;
    }

    public void setColor (String newColor) {
	color = newColor;
    }

}

Request for Question Clarification by secret901-ga on 13 Nov 2005 22:00 PST
Hi nick685,
Your class is fine, but a Java program requires a method called "main"
to run.  You can add that method to your class to test the methods
that you created, or create a new class with the main method that
utilizes this class.  Would you like to be shown how to create a
"main" method?  What functionalities of this class would you like to
test?

secret901-ga
Google Answers Researcher
Answer  
There is no answer at this time.

Comments  
Subject: Re: Error running java program
From: derelict-ga on 14 Nov 2005 13:19 PST
 
Like Nick said , the main() method of your class is the one that's
called when, on a command line (like DOS or bash) you type "java
myClass". If you create a main() method you make that class runnable;
however, it's usually more correct to create a separate class
(something like RunPoint) to do this. For the sake of simplicity, you
can start by creating the main() method directly on that class, just
to try it out. Here are a few examples, you can copy and paste it into
your current code so you can test it:

public static void main(String[] args){

//create a new Point by calling it's constructor method (Point())
Point myPoint = new Point();

//let's see what it's current coordinates and color are
//for that we'll call the getX(), getY() and getColor() methods
System.out.println("Coordinates: "+myPoint.getX()+" "+myPoint.getY());
System.out.println("Color: "+myPoint.getColor());

//let's change it's attributes (coordinates and color)
myPoint.setX(10);
myPoint.setY(20);
myPoint.setColor("blue");

//now let's verify the changes
System.out.println("New coordinates: "+myPoint.getX()+" "+myPoint.getY());
System.out.println("New color: "+myPoint.getColor());

//let's use the other constructor to create a new point
Point myOtherPoint = new Point(5,15,"red");

//let's see what the new point looks like
System.out.println("Other point's coordinates: "+myOtherPoint.getX()+"
"+myOtherPoint.getY());
System.out.println("Other point's color: "+myOtherPoint.getColor());

}


Hope it helped, try them out and post your doubts :)
Subject: Re: Error running java program
From: derelict-ga on 14 Nov 2005 13:22 PST
 
Like secret901 said, you can also create a blank file, call it
something like "TestPoint.java" and put the main() function there.
Then, you'll have to compile them both and run it by typing "java
TestPoint" :)

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy