Java错误 - 错误的源文件:文件不包含类x。请删除或确保它出现 [英] Java error - bad source file: file does not contain class x . Please remove or make sure it appears
问题描述
最近开始学习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
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"
然后,在尝试编译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,
- 删除源文件A.java
- 更改导入声明从'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.
非常感谢
推荐答案
移动文件夹JavaTest下的A.java到com / test / helpers。您看到的错误是编译器抱怨A.java位于与其包声明不匹配的文件夹中。请记住,如果没有A在一个包中,你就无法从App访问A.
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.
从src driectory下运行以下命令来编译你的类
from under src driectory run the following command to compile your classes
src> javac ./*.java ./com/test/helpers/*.java
然后从src下文件夹
src>java App
应该运行你的程序。
这篇关于Java错误 - 错误的源文件:文件不包含类x。请删除或确保它出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!