0

The class I just made is as follows:

package rectangle;

public class Rectangle {
    private double length,width;

    public void setLength(double length) {
        this.length=length;
    }

    public void setWidth(double width)   {
        this.width=width;
    } 

    public double getLength() {
        return length;
    }

    public double getWidth() {
        return width;
    }

    public double area() {
        return length*width;
    }
}

It is in the package rectangle. I understand now that I am supposed to import the class when I am going to use it outside a package that it is in. So:

/*Testing out the rectangle class*/

package rectangleclasstest;

import java.util.Scanner;
import rectangle.Rectangle;                     //Here I try to import the class

public class RectangleClassTest {

    static void main(String[] args) 
    {
        Scanner keyboard= new Scanner(System.in);
        Rectangle rec=new Rectangle();

          //get length
        System.out.println("Please enter the length");
        rec.setLength(keyboard.nextInt());

    }

}

I am now having trouble because the program is telling me that the package rectangle does not exist. Why would it be saying this? I am using Netbeans.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Chevyb
  • 55
  • 3
  • Note that if you're seeing a compiler error and are asking a question about it, you should post the full error message. Is the rectangle package in your classpath? – Hovercraft Full Of Eels Jul 14 '14 at 01:28
  • The error says: package rectangle does not exist. I am not sure if it is in my classpath because I do not fully understand what my classpath is. – Chevyb Jul 14 '14 at 01:46
  • your classpath is a set of `folders` where java look for classes to use. If a folder is not in the classpath then java can not find it. Can you post the full tree of files of your project? – Alejandro Vera Jul 14 '14 at 02:00

1 Answers1

2

Your code is correct as far as I can tell, your issue is with class paths.

Class paths are basically where the file is on your computer. For example the program is may be looking for documents/folder_name/rectangle/Rectangle.class, but really it's in desktop/foo/rectangle/Rectangle.class (these paths are arbitrary and have no meaning). What you should do is check that the classes are in the similar locations and NetBeans can access them.

Here is some reading:

http://en.wikipedia.org/wiki/Classpath_(Java)

How to setup classpath in Netbeans?

You may be able to import the package when you create the class like you can in eclipse, but I'm not positive with netbeans

Community
  • 1
  • 1
dcruwys
  • 107
  • 2