简体   繁体   English

不同的包导入错误

[英]Different package import error

I have a java project that has two packages 'a' and 'b'. 我有一个Java项目,其中有两个软件包“ a”和“ b”。 A class in 'a' depends on a class in 'b', I compiled the class in 'b' (using javac classname.java) but when I try to compile my class in 'a' the package 'b' isn't recognized. “ a”中的类依赖于“ b”中的类,我在b中编译了该类(使用javac classname.java),但是当我尝试在“ a”中编译我的类时,包“ b”却没有认可。 I explicitly import it using a line like this: 我使用像这样的行显式导入它:

import b.*;

I read online that the full package name is to be given, and that's what I think I'm doing in my import statement given the fact that both my packages are directly under the src folder. 我在线阅读了完整的软件包名称,考虑到两个软件包都直接位于src文件夹下,这就是我在import语句中所做的事情。

Would anyone have an idea on how to fix this problem ? 有人对如何解决这个问题有想法吗? Thanks in advance 提前致谢

What you may be doing is compiling from the package folder itself. 您可能正在从package文件夹本身进行编译。 If so, then you will need to exit directory so that you are instead inside the source directory, and then compile with the following command: 如果是这样,那么您将需要退出目录,以使您位于源目录中,然后使用以下命令进行编译:

javac a/ClassInA.java

Where "ClassInA" is the name of the class in the "a" package. 其中“ ClassInA”是“ a”包中类的名称。

The below would have been the error you would have got . 以下将是您将得到的错误。

C:\Users\id831496\Desktop\New folder\a>javac ClassA.java
ClassA.java:3: package b does not exist
import b.*;
^
ClassA.java:5: cannot find symbol
symbol  : class ClassB
location: class a.ClassA
ClassB classB = null;
^
2 errors

What needs to be done is add an classpath argument like below 需要做的是添加一个classpath参数,如下所示

C:\Users\id831496\Desktop\New folder\a>javac -cp ..\b\* ClassA.java

C:\Users\id831496\Desktop\New folder\a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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