简体   繁体   中英

VS Code: The declared package "controller" does not match the expected package ""

So, I have checked some other threads that are about this topic, such as the following:

Eclipse says package declaration does not match expected package ""

The declared package test does not match the expected package

libgdx: The declared package does not match the expected package ""

But they all deal with Eclipse (or Eclipse + Maven), and I am just using VS Code without all the fancy capabilities of an IDE or a package manager.

As the title suggests, I am getting the message: [Java] The declared package "controller" does not match the expected package ""

I get this message for every single file that I attempt to give a package using the syntax:

package myPackageName;

even if the file in question is defiantly in the right folder. This problem only occurs as a visual, ie everything compiles fine using javac. I have been dealing with it ever since I started using VS Code for my Java Projects.

It drives me nutty, I can't quickly find the files that actually have errors in my project because everything is marked red except the Driver.

Here is a picture: picture

[edit] Got rid of the unrelated compile time errors So you can see it compiles.

All classes in the package should be open on VSCode (on different tabs). Same thing happens when you want to use other public classes in same directory: it should be visible but when you run the program, you will get a compile-time error. Then again the solution is to have all classes open on different tabs. Hope it helps

A bit late to it, but here is my finding:

Your parent folder is proto9 , so in all your child folders, ie controller , model and view . You should write package proto9.controller , package proto9.model etc. You can compile the .java file just fine in the correct directory, but not 100% sure why we cannot run it while in the directory of the file is at, java.lang.NoClassDefFoundError is returned when attempted so.

To successfully run it, in the terminal (assuming your files are compiled ):

# cd one level out of your parent directory

PS C:\Users\Issac\Dropbox\Apps\Celerious Text\proto9> cd..

PS C:\Users\Issac\Dropbox\Apps\Celerious Text> java proto9.controller.MainController

...

PS C:\Users\Issac\Dropbox\Apps\Celerious Text> java proto9.view.MainView

etc.

Hope this works for you as well!!

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