Java 错误 - 源文件错误:文件不包含类 x .请删除或确保它出现 [英] Java error - bad source file: file does not contain class x . Please remove or make sure it appears

查看:18
本文介绍了Java 错误 - 源文件错误:文件不包含类 x .请删除或确保它出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近开始为考试学习 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,

  1. 删除源文件 A.java
  2. 将导入语句从 import com.test.helpers.*; 更改为import com.test.helpers.A 在文件'App.java'中.
  1. Deleting the Source file A.java
  2. Changing the import statement from 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

  1. 删除源文件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.

  1. 从 '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 .请删除或确保它出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆