java Java导入包错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17653836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:32:54  来源:igfitidea点击:

Java import package error

javapackages

提问by jimbo123

Hi I am playing around with creating java packages.

嗨,我正在尝试创建 java 包。

I created a package in a folder called admin with a file called Employee - this compiles correctly. Outside this package I have another java file that is importing this. Here is the source code.

我在名为 admin 的文件夹中创建了一个包,其中包含一个名为 Employee 的文件 - 这可以正确编译。在这个包之外,我还有另一个导入这个的 java 文件。这是源代码。

import java.util.*;
// this works --> import admin.Employee;
import admin.*; // this doesn't

public class Hello {
    public static void main(String[] args) {
        Employee h = new Employee("James", 20000);
        System.out.println(h.getName());
    }
}

The weird thing is that the second import statement works fine but with the third one I get

奇怪的是,第二个导入语句工作正常,但我得到了第三个

  • cannot access Employee
  • bad class file: ./Employee.class
  • 无法访问 Employee
  • 错误的类文件: ./Employee.class

I am just using javac Hello.java to compile

我只是使用 javac Hello.java 来编译

the employee class is in package admin. The structure is

员工类在包 admin 中。结构是

folder "admin" -> containing "Employee.class" and "Employee.java" outside this folder is the hello.java file.

文件夹“admin”-> 在此文件夹外包含“Employee.class”和“Employee.java”的是hello.java 文件。

package admin;
import java.util.*;

public class Employee
{   
private static int nextId;

private int id;
private String name = "";
private double salary;

// static initialization block

static
{
    Random generator = new Random();
    // set nextId to a random number between 0 and 9999
    nextId = generator.nextInt(10000);
}

// object initialization block
{
    id = nextId;
    nextId++;
}

// three overloaded constructors
public Employee(String n, double s)
{
    name = n;
    salary = s;
}

public Employee(double s)
{
    // calls the Employee(String, double) constructor
    this("Employee #" + nextId, s);
}

// Default constructor
public Employee()
{
    // name initialized to ""--see below
    // salary not explicityl set--initialized to 0
    // id initialized in initialization block
}


public String getName()
{
    return name;
}

public double getSalary()
{
    return salary;
}

public int getId()
{
    return id;
}
}

回答by Ilya

package admin;  

import java.util.*;

public class Employee
{  

also Employee.javashould be in directory admin. e.g

Employee.java应该在目录中admin。例如

./Hello.java  
./admin/Employee.java

回答by user1676075

Without changing the code (without adding package declarations, which is what I think the question was really asking), the basic fix would be to either:

不更改代码(不添加包声明,这是我认为问题真正要问的),基本的修复方法是:

  • Compile both classes together (javac Hello.java admin/Employee.java)
  • Include the admin folder in your classpath while compiling (-cp path to where admin folder with .class file resides, assuming that's already been compiled)
  • 一起编译这两个类(javac Hello.java admin/Employee.java)
  • 编译时将 admin 文件夹包含在您的类路径中(-cp 路径到 .class 文件所在的 admin 文件夹,假设已经编译)

When you tell it import admin.Employee, and it's in the same source folder, the compiler can infer you want that implicitly compiled. When you import admin.*, you need to include the .java file on the command-line, or include classpath to the .class file, for it to compile.

当你告诉它 import admin.Employee 并且它在同一个源文件夹中时,编译器可以推断你想要隐式编译。导入 admin.* 时,您需要在命令行中包含 .java 文件,或将类路径包含到 .class 文件中,以便编译。