简体   繁体   中英

The declared package <package name> does not match the expected package "" in VS code

This is the error I get in VS code when I try to make a package in a simple program

The declared package "mypack" does not match the expected package "" Java(536871240)

类.java 类

Aclass java file gets compiled. But after compiling Bclass java file it gives this compile-time error

A类编译 B类编译错误

This is Aclass java file code

package mypack;

class A {
    void MethodA() {
        System.out.println("This is class A");
    }
}

class Aclass {
    public static void main(String args[]) {
        A a = new A();
        a.MethodA();
    }
}

This is Bclass java file code

import mypack.*;

class B {
    void MethodB() {
        System.out.println("This is class B");
    }
}

class Bclass {
    public static void main(String args[]) {
        B b = new B();
        b.MethodB();
        A a = new A();
        a.MethodA();
    }
}

The declared package "mypack" does not match the expected package "" Java(536871240)

It's because you didn't put .java files in the folder mypack .

The second error is there're B , A in Bclass.java , but the compiling command didn't include them, so you should compile multiple java files by the command

javac -d ./ mypack\*.java

Then execute it by javac mypack.Bclass , you can get right result:

在此处输入图片说明

You just need to right click on below Files in the Explorer Tab in Visual Studio code and then just Select Remove Folder from java source path.

It was helpful for me and now there are no issues.

Again remember, right click > remove folder from java source path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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