简体   繁体   English

声明的 package "a" 与预期的 package "" 不匹配

[英]The declared package "a" does not match the expected package ""

Having this file schema:具有此文件架构:

folder "a"
        |
        |_____ MyClass.java

Where my ".java" file has this code:我的“.java”文件中有这段代码:

package a;

public class MyClass {
    public static void main(String[] args) {
        System.out.println("Hello world");

    }
}

I was wondering the reason why I get this error (I'm using visual studio, I say it in case it is related to my error):我想知道我收到此错误的原因(我使用的是 visual studio,我说它以防它与我的错误有关):

The declared package "a" does not match the expected package "".

I've noticed that sometimes when I reopen my folder It suddenly works, however other times it doesn't.我注意到,有时当我重新打开我的文件夹时,它会突然起作用,但有时却不起作用。

I don't know why this happens, I believe the reason it's related to visual studio but it goes further of it, since when I try to compile it, as said, sometimes It works, others don't.我不知道为什么会发生这种情况,我相信它与 visual studio 相关的原因,但它更进一步,因为当我尝试编译它时,如前所述,有时它有效,而其他人则没有。

Telling me this error:告诉我这个错误:

Error: Could not find or load main class MyClass
Caused by: java.lang.NoClassDefFoundError: a/MyClass (wrong name: MyClass)
package a;

public class MyClass {
    public static void main(String[] args) {
        System.out.println("Hello world");

    }
}

It depends from where you are referencing the class. If a is a subfolder and you are trying to compile MyClass.java from within a it won't work (unless you specify the correct folder like ./.. or similar. Packages are relative to some root folder or other package.这取决于您引用 class 的位置。如果a是一个子文件夹,并且您试图从a中编译MyClass.java它将不起作用(除非您指定正确的文件夹,如./..或类似的文件夹。包是相对的到某个根文件夹或其他 package。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM