简体   繁体   English

Visual Studio Code 无法识别 java 项目

[英]Visual Studio Code not recognizing java project

VS Code is not recognizing my java project. VS Code 无法识别我的 java 项目。 The project is template code for a minecraft mod found here .该项目是在这里找到的 minecraft mod 的模板代码。 Ive installed the plugins我安装了插件

  • "Debugger for Java" “Java 调试器”
  • "Java Extension Pack" “Java 扩展包”
  • "Language Support for Java by Redhat" “Redhat 对 Java 的语言支持”
  • "Java Test Runner" “Java 测试运行程序”
  • "Project Manager for Java" “Java 项目经理”

I also get an error related to gradle.我还收到与 gradle 相关的错误。

Could not run phased build action using connection to Gradle distribution 'https://services.gradle.org/distributions/gradle-7.0.2-bin.zip'.
java.io.StreamCorruptedException: invalid type code: 00
invalid type code: 00

And here is my settings.json file这是我的 settings.json 文件

{
    "files.exclude": {
        "**/.classpath": true,
        "**/.project": true,
        "**/.settings": true,
        "**/.factorypath": true
    },
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "java.configuration.updateBuildConfiguration": "interactive",
    "java.home": "/usr/lib/jvm/java-16-openjdk-amd64",
    "java.configuration.runtimes": [
        {
          "name": "JavaSE-1.8",
          "path": "/usr/lib/jvm/java-8-openjdk-amd64",
          "default": true
        },
    ]
}

I have no code completion for my Java files.我的 Java 文件没有代码完成。 Before it was working and in the lower right corner of visual studio it displayed the java version i was using but now it just says Java. I'm assuming its because there is a problem with gradle and thus cannot figure out my java version.在它工作之前,在 visual studio 的右下角,它显示了我正在使用的 java 版本,但现在它只显示 Java。我假设它是因为 gradle 有问题,因此无法弄清楚我的 java 版本。 Thank you for your time.感谢您的时间。

Update: This seems to work and give me auto completion but i stil get a gradle related error mention above.更新:这似乎有效并给我自动完成,但我仍然收到上面提到的 gradle 相关错误。

{
    "files.exclude": {
        "**/.classpath": true,
        "**/.project": true,
        "**/.settings": true,
        "**/.factorypath": true
    },
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "java.configuration.updateBuildConfiguration": "interactive",
    "java.home": "/usr/lib/jvm/java-16-openjdk-amd64",
    "java.configuration.runtimes": [
        {
          "name": "JavaSE-1.8",
          "path": "/usr/lib/jvm/java-8-openjdk-amd64",
          "default": true
        },
        {
            "name": "JavaSE-16",
            "path": "/usr/lib/jvm/java-16-openjdk-amd64",
        },
    ]
}

For anyone who is stuck with the same problem as me make sure your settings.json file looks similar to mine including both java 1.8 run config and the java 11+ run config same as the one specified as the home path.对于遇到与我相同问题的任何人,请确保您的 settings.json 文件看起来与我的相似,包括 java 1.8 运行配置和 java 11+ 运行配置,与指定为主路径的运行配置相同。 That should fix the java language level.那应该修复 java 语言级别。 The gradle error you can ignore but you must use gradle runClient instead of the run configuration that vscode provides. gradle 错误可以忽略,但必须使用 gradle runClient 而不是 vscode 提供的运行配置。 Happy modding:D快乐改装:D

In my case, I solved the problem by creating a new Java Project, and comparing the files that were generated.就我而言,我通过创建一个新的 Java 项目并比较生成的文件解决了这个问题。 In particular I found missing entries in .project特别是我在.project中发现了缺失的条目

<buildSpec>
        <buildCommand>          <!-- was missing -->
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.jdt.core.javanature</nature> <!-- was missing -->
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
    </natures>

also some settings in .settings/org.eclipse.jdt.core.prefs were missing but they did not immediately fixed the problem until I added the above. .settings/org.eclipse.jdt.core.prefs中的一些设置也丢失了,但在我添加上述内容之前,它们并没有立即解决问题。

Of course, you need to clean the workspace everytime you make a change (with the ... -> clean workspace or the command palette Java > clean Java Language server workspace当然,每次进行更改时都需要清理工作区(使用... -> clean workspace或命令面板Java > clean Java Language server workspace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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