最近开始为考试学习 Java.
Recently started studying Java for an exam.
在学习包时,尝试了这个并收到错误消息.我所做的是,
While learning packages, tried this and got an error message. What I did was,
//Creating class A (Within package the package: com.test.helpers)
package com.test.helpers;
public class A {
public void sayHello(){
System.out.println("Hello World");
}
}
<小时>
//And then the class App utilizing the class A
import com.test.helpers.*;
public class App{
public static void main(String args[]){
A a = new A();
a.sayHello();
}
}
<小时>
我将这两个文件放在一个名为JavaTest"的目录中(在 Windows 7 上),并首先使用命令 javac -d 编译 A.java.A.java
然后,在尝试编译 App.java 时,我收到以下错误消息:
And then, while attempting to compile App.java, I got the following error message:
App.java:5: error: cannot access A
A a = new A();
^
bad source file: .A.java
file does not contain class A
Please remove or make sure it appears in the correct subdirectory of the source path.
1 error
<小时>
不过,问题似乎可以通过两种方式解决,
However, the problem seems to resolve in two ways,
import com.test.helpers.*; 更改为import com.test.helpers.A 在文件'App.java'中.import com.test.helpers.*; to
import com.test.helpers.A in the file, 'App.java'.如果您能解释一下这里发生了什么,我将不胜感激.或者我可能犯了一个愚蠢的人为错误或语法错误.
I'd be highly grateful if you can explain what happens here. Or I might be making a goofy human mistake or a syntax error.
这里是源文件的链接
非常感谢
您好,这里的问题是JVM混淆了class文件,原因是两个目录中的ambiguous类文件名(JavaTest 以及 com.test.helpers 目录).
Hi the problem here is that the JVM confuses the class file due to the ambiguous class file name in both the directory (the JavaTest as well as the com.test.helpers directory).
当您执行 javac -d 时.A.java 编译器在 com.test.helpers 目录中创建了一个类文件,现在它与 JavaTest
when you do javac -d . A.java the compiler makes a class file in the directory com.test.helpers and now it confuses it with the sourcefile there in JavaTest
删除源文件A.java当你从 JavaTest 中删除源文件 A.java 时,JVM 现在知道 com.test.... 被使用,歧义消失了.
When you delete the source file A.java from JavaTest, JVM knows now that the class file in com.test.... is to be used, ambiguity goes away.
从 'import com.test.helpers.*;' 更改导入语句在文件'App.java'中'import com.test.helpers.A'.在这里,您指定在类实现中使用哪个特定文件,即告诉编译器使用 com.test...A.java> 而不是来自 JavaTest 包
Here you are specifying which particular file to use in your class implementation that is you are telling the compiler to use the file A.java from com.test... and not from JavaTest package
现在,这种歧义的解决方案对您来说永远不会成为问题,您必须使用 import 语句导入特定文件,即 import com.test.helpers.A; 或者如果您想做 import com.test.helpers.*; 那么你必须在你的所有地方专门使用 com.test.helpers.A 代替 A当前的类实现告诉编译器不要将它与 JavaTest
Now, the solution for this ambiguity to not ever be a problem for you, you must import the specific files with import statement i.e. import com.test.helpers.A; or if you want to do import com.test.helpers.*; then you must specifically use com.test.helpers.A in place of A everywhere in your current class implementation to tell the compiler not to confuse it with the source at JavaTest
我知道这个特定的答案已经很晚了,但我想为即将到来的读者分享我的观点,如果它可以以任何方式帮助他们,那就太好了.谢谢!
I know it's a lot late for this particular answer, but I wanted to share my views for the upcoming readers, if it could help them in any way, it would be great. Thanks!
这篇关于Java 错误 - 源文件错误:文件不包含类 x .请删除或确保它出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何检测 32 位 int 上的整数溢出?How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
return 语句之前的局部变量,这有关系吗?Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
如何将整数转换为整数?How to convert Integer to int?(如何将整数转换为整数?)
如何在给定范围内创建一个随机打乱数字的 intHow do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
java的行为不一致==Inconsistent behavior on java#39;s ==(java的行为不一致==)
为什么 Java 能够将 0xff000000 存储为 int?Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)