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

查看:42
本文介绍了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.*; 更改为在文件'App.java'中导入com.test.helpers.A.

如果您能解释这里发生的事情,我将不胜感激.或者我可能犯了一个愚蠢的人为错误或语法错误.

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 混淆了类文件,因为目录(ambiguouscode>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"中导入 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 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天全站免登陆