如何修复在VSCode上出现的“声明的包与预期的包不匹配”错误。

huangapple go评论65阅读模式
英文:

How do I fix a “declared package does not match the expected package” error on VSCode

问题

我有一个 Gradle 项目:

.
|-- src
|    |-- java
|    |    +-- myproj
|    |         +-- App.java
|    +-- test
|-- build.gradle
+-- ...

App.java 文件内容如下:

package myproj;
//      ^^^^^^ 错误信息

但是 VSCode 告诉我“声明的包“myproj”与预期的包“main.java.myproj”不匹配”。我知道这与文件路径有关,但是我没有找到如何进行配置。

我已经安装了 Java Extension Pack 插件,接下来我该怎么办?

附注:我不希望配置方案过于复杂,我只是在写一个非常小的程序。最好能在 setting.json 中完成。

英文:

I have a gradle project:

.
|-- src
|    |-- java
|    |    +-- myproj
|    |         +-- App.java
|    +-- test
|-- build.gradle
+-- ...

and App.java file looks like this:

package myproj;
//      ^^^^^^ error message

but VSCode tells me “The declared package "myproj" does not match the expected package "main.java.myproj"”. I know this is related to the file path, but I did not find out how to configure it.

I have installed the Java Extension Pack plug-in, what should I do next?

PS. I don’t want the configuration scheme to be too complicated, I’m just writing a very small program. It is best to be able to complete in setting.json.

答案1

得分: 1

尝试在src和java文件夹之间添加“main”目录,以匹配Gradle的文件夹结构。

来源:https://docs.gradle.org/current/userguide/organizing_gradle_projects.html

英文:

Try to add "main" directory between src and java folder to match gradle folders structure.

Source: https://docs.gradle.org/current/userguide/organizing_gradle_projects.html

huangapple
  • 本文由 发表于 2020年9月16日 22:56:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63922763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定