无法在Java中导入子包。

huangapple go评论61阅读模式
英文:

Can't import a sub package in java

问题

以下是您要翻译的内容:

"Can someone help me with this problem?:

I can't import a sub package, when I try javac Pixel.java I get:

Pixel.java:2: error: package image.color does not exist.

I also tried import image.* and import image.color.*;.

Pixel.java:

package image;

import image.color.GrayColor;

public class Pixel{
}

GrayColor.java:

package image.color;

public class GrayColor{
}

My tree:
└── image
├── Pixel.java
├── color
├── GrayColor.class
└── GrayColor.java"

英文:

Can someone help me with this problem?:

I can't import a sub package, when I try javac Pixel.java I get:
> Pixel.java:2: error: package image.color does not exist.

I also tried import image.* and import image.color.*;.

Pixel.java:

package image;

import image.color.GrayColor;

public class Pixel{
}

GrayColor.java:

package image.color;

public class GrayColor{
}

My tree:
└── image
├── Pixel.java
├── color
   ├── GrayColor.class
   └── GrayColor.java

答案1

得分: 2

很可能,您并未从项目的根目录编译Pixel.java,而是从该文件所在的目录(image/)中进行编译。在这种情况下,与您的包声明image/color匹配的目录结构在image/中不可用,因为您没有image/image/color这样的结构。

包声明必须与其声明所在的目录结构相匹配,相对于根目录,因为它们是相对于项目的根目录进行查找的。例如,如果您正在编译A.java,该文件依赖于位于package a.b;下的B.java(A导入B),那么B.java将在相应的a/b/文件夹中查找,如果您在从中查找a/b/不可用的文件夹中,编译器将抱怨找不到包/目录a/b/

在使用javac时,您有两种解决方法:

  • 最佳方法是从根目录编译您的文件。这样,所有的包声明将与目录结构匹配(假设您的包名正确,并且它们与目录名匹配),相对于根目录;
  • 如果您从项目的根目录编译文件,您可以使用-cp标志,告诉Java编译器在哪里查找其他源代码。请注意,同样需要提供项目根目录的路径,因为类的包将只在从项目的根目录查找时与文件夹结构匹配。
英文:

Most likely, you are not compiling Pixel.java from the root directory of the project, and you compile it from within the directory where that file is located (image/). In this case, directory structure matching your package declaration image/color is not available from image/, as you do not have image/image/color.

Package declarations must match the directory structure they are declared in, relatively to the root directory, as they are looked up relatively to the project's root directory. E.g. if you are compiling A.java, which has a dependency B.java (A imports B) defined under the package a.b;, then the B.java will be looked up in the corresponding a/b/ folder, and if you are in the folder, from where, a/b/ is not available, compiler will complain, that it cannot find the package/directory a/b/.

You have two ways to solve this when using javac:

  • Best is to compile your file(s) from the root directory. This way, all the package declarations will match the directory structure (assuming your package names are correct and they match directory names) relatively from your root directory;
  • You can use -cp flag, to tell to the Java compiler where to look for other sources, in case you are not compiling your file(s) from the root directory of your project. Note, that here, as well, you should provide a path of the root directory of your project, as the class(es) package(s) will only match the folder structure, when looked up from the root directory of the project.

huangapple
  • 本文由 发表于 2020年10月13日 05:38:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64325661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定