Isaac Yates
Isaac Yates

Reputation: 83

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, i.e. everything compiles fine using javac. I have been dealing with it ever since I started using VS Code for my Java Projects.

How do I make the red squigglies and the red lettering in the file explore tab go away in the case of perceived package errors?? 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.

Upvotes: 8

Views: 6041

Answers (2)

Amir47
Amir47

Reputation: 21

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

Upvotes: 2

Yao
Yao

Reputation: 449

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

Your parent folder is proto9, so in all your child folders, i.e. 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!!

Upvotes: 0

Related Questions