简体   繁体   中英

Can't import a class in java. Code refuses to believe it exists

Okay so I made code that wants to find the area of a circle, triangle and square. There are two java files in my package folder, Mung. For some reason I keep getting the error Shapes.java:2: error: package Mung does not exist

Here's my code.

import java.io.*; import java.util.*;
import Mung.*;
public class Shapes
{
   public static void main(String args[])
   {
      System.out.println(areaSquare());
      System.out.println(areaCircle());
      System.out.println(areaTriangle());
   }
}

and

import java.io.*; import java.util.*;
public class ShapesAssignment
{
   public ShapesAssignment (double r,double h,double w)
  {
  double radius = r;
  double height = h;
  double width = w;
  }
   public double areaCircle()
   {
      double cArea = Math.PI * radius;
      return cArea;
   }
   public double areaTriangle()
   {
      double tArea = (height*width)/2;
      return tArea;

    }
    public double areaSquare()
   {
      double sArea = (height*width);
      return sArea;

   }
   public double radius;
   public double height;
   public double width;
}

Could you please take a screenshot of your project tree in eclipse? If Mung is a class, which it looks like it from here, then you do not import it like that. You would import the package in which Mung class is found. If I am misunderstanding it, then that is because you have named your packages incorrectly. It should look something like this:

import com.company.app.<name>

What is Ming package and do you really need it ? Lets try this:

import java.io.*;
import java.util.*;
public class Shapes {
  public static void main(String args[]) {
     ShapesAssignment sp = new ShapesAssignment(2, 3, 4);
     System.out.println(sp.areaSquare());
     System.out.println(sp.areaCircle());
     System.out.println(sp.areaTriangle());
  }
}

Putting two Java files in the same folder doesn't mean they are in the same package.

Maybe there is really no need to import anything - check, if both of these files (source file of Mung and file in which you want to use Mung ) are in the default package (none of these files start with key word package ...), you can simply use Mung in other files without specifying any import)

If at least one of these files starts with package key word, these files are in other packages. You need to make sure that the file you need to import from starts with word package (I suppose that Mung is not the package but the class - package names start with lower case letter, I think Mung is rather a class that you need a method to import from). Check file that contains class you need to import from, if it starts with something like package mypackage.xyz; .

If it doesn't start with word package , you are not able to import anything from this file with import keyword, as everything in this file is in default package (Java doesn't allow to import anything contained in default package with the use of import key word).

If it is in any other package than default , import it with the full name of the package and with the name of the class, for example:

import mypackage.xyz.Mung.*;

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