Home > Net >  The declared package <package name> does not match the expected package "" in VS cod
The declared package <package name> does not match the expected package "" in VS cod

Time:11-10

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)

 Aclass.java  Bclass.java

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

 Aclass compile  Bclass compile error

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();
    }
}

CodePudding user response:

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:

enter image description here

  • Related