简体   繁体   English

声明的包“”与预期的包“(文件名)”不匹配

[英]The declared package “” does not match the expected package “(file name)”

I'm new to Java and I'm using Visual Studio Code.我是 Java 新手,正在使用 Visual Studio Code。 When I run the code it works fine, but every program in java (even a basic Hello World program) gives me this error.当我运行代码时,它工作正常,但是 java 中的每个程序(甚至是基本的 Hello World 程序)都会给我这个错误。

The declared package "" does not match the expected package "helloWorld"

I didn't find any solutions on internet and I don't know how to fix this.我没有在互联网上找到任何解决方案,我不知道如何解决这个问题。

here is the code:这是代码:

public class helloworld {
    public static void main(String[] args) {
        
        System.out.println("hello world");
        
    }
}

Based on your compiler error and the code you showed, you have the following folder/package structure in the project you have created:根据您的编译器错误和您显示的代码,您在创建的项目中有以下文件夹/包结构:

src
|
|-helloWorld
       |
       | helloworld.java

If that's the case, the "expected package" is the full package definition your java file is defined in, in this case, "helloWorld"如果是这种情况,“预期包”是您的 java 文件在其中定义的完整包定义,在本例中为“helloWorld”

If you had this other folder structure:如果您有其他文件夹结构:

src
|
|-helloWorld (folder/package)
       |
       | subfolder (subfolder/package)
                 |
                 | helloworld.java

then your expected package for the class helloworld would be helloWorld.subfolder那么你对 helloworld 类的预期包将是 helloWorld.subfolder

To actually define that package in the .java file, you must write the following as the first code line of the file (this is mandatory):要在 .java 文件中实际定义该包,您必须将以下内容写入文件的第一行代码(这是必需的):

package fullpackage;

In your example, this would be:在您的示例中,这将是:

package helloWorld;

The full code example as I can see it, would be for you something like...我所看到的完整代码示例对您来说是这样的......

package helloWorld;
public class helloworld {
   // the rest of your main method and code here
}

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

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