Lead Vaxeral
Lead Vaxeral

Reputation: 323

Visual Studio Code not recognizing java project

VS Code is not recognizing my java project. The project is template code for a minecraft mod found here. Ive installed the plugins

I also get an error related to 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

{
    "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. 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. 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.

{
    "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",
        },
    ]
}

Upvotes: 18

Views: 48963

Answers (5)

luca.vercelli
luca.vercelli

Reputation: 1048

I had a similar problem. I opened a multi-module Maven project in vscode. vscode recognized it as a Java project (I see all modules in "Java Projects" and "Maven" tabs), however nothing was compiled.

I noticed that there was no ".project" or ".classpath" file in the folder.

The solution I found: I opened the same project with Eclipse. Eclipse created the ".project" and ".classpath" files. Than I opened again the project in vscode, and now vscode compiles everything correctly! (Not a beautiful solution, I know).

Upvotes: 0

meiyao er
meiyao er

Reputation: 11

I tried all the methods mentioned above, but unfortunately, none of them worked. I have an existing Java project and I've installed the "Extension Pack for Java", but VSCode still couldn't recognize this Java project.

Eventually, I found a solution. I closed the current Java project, opened a new VSCode window, and directly opened the folder of the Java project. After doing this, everything went back to normal!

If you're facing a similar issue, you might want to give this method a try.

Upvotes: 1

sayingu
sayingu

Reputation: 2431

Run Java: Clean Java Language Server Workspace on command palette

  • In my case, I have no settings files like .project, ./settings/...files

Upvotes: 20

MrE
MrE

Reputation: 20818

In my case, I solved the problem by creating a new Java Project, and comparing the files that were generated. In particular I found missing entries in .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.

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

Upvotes: 1

Lead Vaxeral
Lead Vaxeral

Reputation: 323

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. That should fix the java language level. The gradle error you can ignore but you must use gradle runClient instead of the run configuration that vscode provides. Happy modding :D

Upvotes: 1

Related Questions