编译器无法读取导入的类。

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

Compiler couldn't read imported class

问题

我一直在尝试通过手动创建包,在CMD中进行编译。我尝试了以下命令:

javac -d . Nameinfo.java

javac -d . Returndata.java

其中,Nameinfo 包含了计算部分,Returndata 包含了主函数。这些命令都能正常工作。现在,我在我的 "java 程序" 文件夹中创建了两个不同的包,即 packxpackypackx 包含了 Nameinfopacky 包含了 Returndata。但是,当我尝试编译这两个包时,Nameinfo.java 能够成功编译,但是 Returndata.java 无法读取/识别在 Returndata.java 中导入的 Nameinfo

错误信息如下:

E:\java programmes>cd packx

E:\java programmes>cd packx

E:\java programmes\packx>javac Nameinfo.java

E:\java programmes\packx>cd..

E:\java programmes>cd packy

E:\java programmes\packy>javac Returndata.java
Returndata.java:2: error: package packx does not exist
import packx.Nameinfo;
            ^
Returndata.java:7: error: cannot find symbol
Nameinfo data = new Nameinfo();
^
symbol:   class Nameinfo
location: class Returndata
Returndata.java:7: error: cannot find symbol
Nameinfo data = new Nameinfo();
                    ^
symbol:   class Nameinfo
location: class Returndata
3 errors
英文:

I have been trying to compile through CMD by creating packages manually. I tried the command javac -d . Nameinfo.java and javac -d . Returndata.java, where Nameinfo contains the calculations and Returndata contains main function, and this command worked absolutely fine. Now I made 2 different packages in my "java programmes" folder i.e packx and packy, where packx contains Nameinfo and packy contains Returndata. Now, when I compile these two, Nameinfo.java got compiled but Returndata could not read/recognize the imported Nameinfo in Returndata.java.

errors:

E:\java programmes>cd packx

E:\java programmes>cd packx

E:\java programmes\packx>javac Nameinfo.java

E:\java programmes\packx>cd..

E:\java programmes>cd packy

E:\java programmes\packy>javac Returndata.java
Returndata.java:2: error: package packx does not exist
import packx.Nameinfo;
            ^
Returndata.java:7: error: cannot find symbol
 Nameinfo data = new Nameinfo();
 ^
  symbol:   class Nameinfo
  location: class Returndata
Returndata.java:7: error: cannot find symbol
 Nameinfo data = new Nameinfo();
                     ^
  symbol:   class Nameinfo
  location: class Returndata
3 errors

答案1

得分: 0

如果 Java 或者 javac 需要使用 packx.Returndata 作为一种类型来执行某些操作,它会在类路径和/或源路径中搜索名为 packx 的目录,在该目录中搜索 Returndata.java 或者 Returndata.class,具体取决于工具和路径。

所以,假设你位于 E:\java programmes\packy 目录,并且正在编译一些导入了 packx 的代码。因此,javac 会在类路径中查找 packx/Returndata.class,在源路径中查找 packx/Returndata.java,但是它找不到你想要的内容,因为 E:\java programmes 不在源路径中;通常它们默认为 .(即当前目录)。

在整个过程中,都保持在 E:\java programmes 文件夹中进行操作:

E:
cd "Java programmes"
javac packx/Nameinfo.java
javac packy/Returndata.java

或者更好的做法是意识到手动编译多包 Java 应用程序是不明智的。使用 mavengradle 或其他能够为你处理所有这些问题的构建工具吧 编译器无法读取导入的类。

英文:

if java or javac needs, say, packx.Returndata as a type to do something, it will scour a classpath and/or sourcepath for a directory named packx, and within that, Returndata.java or Returndata.class depending on the tool and path we're talking about.

So, you're in E:\java programmes\packy, and you're compiling some code that imports packx. Therefore, javac will look in the classpath for packx/Returndata.class and in the sourcepath for packx/Returndata.java and it doesn't find what you want because E:\java programmes isn't on the source path; generally they default to . (i.e. the current directory).

Stay in the E:\java programmes folder for all this work:

E:
cd "\Java programmes"
javac packx/Nameinfo.java
javac packy/Returndata.java

Or better yet, realize that manually compiling a multipackage java application is crazy. Use maven, gradle or some other build tool that takes care of all this for you 编译器无法读取导入的类。

huangapple
  • 本文由 发表于 2020年9月18日 22:53:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63958086.html
匿名

发表评论

匿名网友

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

确定