简体   繁体   中英

I am having trouble importing a class I recently made

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM