Java 错误 - 错误的源文件:文件不包含类 x 。请删除或确保它出现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24831183/
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
Java error - bad source file: file does not contain class x . Please remove or make sure it appears
提问by user3210872
Recently started studying Java for an exam.
最近为了考试开始学习Java。
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();
}
}
I had both of these files in a directory called 'JavaTest' (on Windows 7),
and first compiled the A.java using the command javac -d . A.java
我将这两个文件都放在一个名为“JavaTest”(在 Windows 7 上)的目录中,并首先使用以下命令编译了 A.java javac -d . A.java
And then, while attempting to compile App.java, I got the following error message:
然后,在尝试编译 App.java 时,我收到以下错误消息:
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,
但是,问题似乎可以通过两种方式解决,
- Deleting the Source file A.java
- Changing the import statement from
import com.test.helpers.*;
toimport com.test.helpers.A
in the file, 'App.java'.
- 删除源文件 A.java
- 从改变import语句
import com.test.helpers.*;
到import com.test.helpers.A
文件,“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.
如果您能解释这里发生的事情,我将不胜感激。或者我可能犯了一个愚蠢的人为错误或语法错误。
Here's the link to the source files
Thank you very much
非常感谢
回答by jambriz
the file A.java should be in the path JavaTest\com\test\helpers\A.java
文件 A.java 应该在路径 JavaTest\com\test\helpers\A.java 中
and don't forget to compile it like this: javac -d . com\test\helpers\A.java
并且不要忘记像这样编译它: javac -d 。com\test\helpers\A.java
回答by Chandru
move the A.java under folder JavaTest to com/test/helpers. the error you are seeing is for the compiler complaining that A.java is in a folder that does not match its package declaration. Remember, you cannot access A from App without A being in a package.
将文件夹 JavaTest 下的 A.java 移动到 com/test/helpers。您看到的错误是编译器抱怨 A.java 位于与其包声明不匹配的文件夹中。请记住,如果 A 不在包中,您将无法从 App 访问 A。
from under src driectory run the following command to compile your classes
从 src 目录下运行以下命令来编译您的类
src> javac ./*.java ./com/test/helpers/*.java
then from under src folder
然后从 src 文件夹下
src>java App
that should run your program.
那应该运行你的程序。
回答by Shiva
I have the same problem finally,
我终于遇到了同样的问题
I was solved.
我被解决了。
//Use like this
import com.test.helpers.A;
If u have a more than class in com.test.helpersthen u can use import com.test.helpers.*;
如果您在com.test.helpers 中有多个类,那么您可以使用import com.test.helpers.*;
回答by Shiva
every thing is in right place, java does not ask to put your A.java file, in the helpers folder
一切都在正确的位置,java 不会要求将您的 A.java 文件放在 helpers 文件夹中
the whole reason why your code ran with the A.java file removed and not otherwise is: when your app.java (or the main java file which is importing other classes in it, -->) is in a default package then while importing the classes it gives priority to the same directory and not to the package directory(if there exist any file that has the same name as the class file -> and thats why it gives error bad source file A.java -> as it wanted A.class)
您的代码在删除 A.java 文件而不是其他文件的情况下运行的全部原因是:当您的 app.java(或在其中导入其他类的主 java 文件,-->)位于默认包中时,则在导入时它优先考虑相同目录而不是包目录的类(如果存在任何与类文件同名的文件 -> 这就是为什么它给出错误错误的源文件 A.java -> 因为它想要 A 。班级)
And because of that there is a rule in java : That never place a .java file (of the same name)in parallel to the packages
正因为如此,java 中有一条规则:永远不要将 .java 文件(同名)与包平行放置
so to solve this problem you have to either remove the file, A.java or rename it (to any other name that does not present in the package) or you can use fully qualified import statement
所以要解决这个问题,你必须删除文件 A.java 或重命名它(包中不存在的任何其他名称),或者你可以使用完全限定的导入语句
回答by mohitR0_0
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).
嗨,这里的问题是 JVM 由于ambiguous
目录(JavaTest
以及com.test.helpers
目录)中的类文件名而混淆了类文件。
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
当你这样做时javac -d . A.java
,编译器会在目录中创建一个类文件com.test.helpers
,现在它会将它与其中的源文件混淆JavaTest
Deleting the Source file A.java
Deleting the Source file A.java
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.
当您A.java
从 中删除源文件时JavaTest
,JVM 现在知道com.test....
要使用中的类文件,歧义消失了。
Changing the import statement from 'import com.test.helpers.*;' to 'import com.test.helpers.A' in the file, 'App.java'.
Changing the import statement from 'import com.test.helpers.*;' to 'import com.test.helpers.A' in the file, 'App.java'.
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
在这里您指定的是你告诉编译器使用的文件类的实现要使用的特定文件A.java
从com.test...
,而不是从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
现在,这种歧义的解决方案对您来说永远不会成为问题,您必须使用 import 语句 ie 导入特定文件,import com.test.helpers.A;
或者如果您想这样做,import com.test.helpers.*;
那么您必须专门使用com.test.helpers.A
代替A
当前类实现中的任何地方来告诉编译器不要将其与来源混淆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!
我知道这个特定答案为时已晚,但我想与即将到来的读者分享我的观点,如果它可以以任何方式帮助他们,那就太好了。谢谢!