Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradle compiles but vscode-java is getting compilation errors #797

Open
jeusdi opened this issue Feb 14, 2019 · 22 comments
Open

Gradle compiles but vscode-java is getting compilation errors #797

jeusdi opened this issue Feb 14, 2019 · 22 comments

Comments

@jeusdi
Copy link

jeusdi commented Feb 14, 2019

I've pushed related code on https://github.com/jeusdi/gradle-vscode.git

gradle compileJava runs correctly, but vscode-java is getting me compilation errors.

Environment
  • Operating System: Ubuntu 18.04
  • JDK version: JDK 11
  • Visual Studio Code version: 1.31.0
  • Java extension version: 0.38.0
Steps To Reproduce
  1. git clone https://github.com/jeusdi/gradle-vscode.git
  2. Open vscode

[attach a sample project reproducing the error]
attach logs

Current Result

imagen

Expected Result

gradle command ends succesfully:

imagen

Additional Informations
@jeusdi jeusdi changed the title Gradle compiles but vscode-java is getting me compilation errors Gradle compiles but vscode-java is getting compilation errors Feb 14, 2019
@fbricon
Copy link
Collaborator

fbricon commented Feb 15, 2019

That one is really weird. Same project builds fine in Eclipse. The classpath seems to be set correctly in vscode, I can see the jars in the Dependency view, but then the types can't be resolved. Also those resolution problems don't show until a file is opened, they should appear as soon as the project builds in the background. I'll keep looking.

@snjeza
Copy link
Contributor

snjeza commented Feb 15, 2019

@jeusdi There are two javax.ws.rs.core.Application classes in your project.
A workaround is to add the following to build.gradle:

configurations {
    compile {
        exclude group: 'jakarta.ws.rs'
        exclude module: 'jakarta.ws.rs-api'
    }
}

@wtsiamruk
Copy link

Facing same issues in gradle project, figured out that after deletion of bin/ folder compile issue is gone.

@tmh2211
Copy link

tmh2211 commented Feb 19, 2019

I have the same issue, but only when a module-info.java file is present. If I remove it, no errors are shown in vscode.

@snjeza
Copy link
Contributor

snjeza commented Feb 19, 2019

@wtsiamruk @tmh2211 could you attach a project example?

@tmh2211
Copy link

tmh2211 commented Feb 19, 2019

@snjeza I can when I come home tomorrow. It happens with the default project from gradle:
gradle init --type java-application
with groovy as dsl and junit for testing.
Add a module-info.java and the junit imports in the test folder can not be resolved anymore. ./gradlew assemble from command line, however, doesn't complain.
Gradle version 5.2, VSCode 1.31.1, LS for Java 0.38.0

@fbricon
Copy link
Collaborator

fbricon commented Feb 19, 2019

most likely this is caused by Gradle/Buildship not adding the test flag to the test source folder in .classpath.

You should have something like:

        <classpathentry kind="src" output="target/test-classes" path="src/test/java">
		<attributes>
			<attribute name="test" value="true"/>
		</attributes>
	</classpathentry>

You may need to tweak your build.gradle file so that it generates that flag automatically.

@fbricon
Copy link
Collaborator

fbricon commented Feb 19, 2019

See eclipse/buildship#689

and a partial workaround (for sources) in build.gradle : eclipse/buildship#827 (comment)

@tmh2211
Copy link

tmh2211 commented Feb 20, 2019

@snjeza @fbricon Here is a small javafx example, that produces the error in src/main/java if the module-info.java is present. I still can auto-import stuff and auto completion works as well.
./gradlew assemble and ./gradlew jlink work without a problem.
test.zip

@snjeza
Copy link
Contributor

snjeza commented Mar 12, 2019

@tmh2211 could you try the following project:

test2.zip

The build.gradle and module-info.java files have been changed.
Requirements: gradle >= 5.0, JDK 11.0.2

@wlmr
Copy link

wlmr commented Apr 19, 2019

For me everything had to do with the file .setttings/org.eclipse.buildship.core.prefs. Remove everything but the following two lines from it.

connection.project.dir=
eclipse.preferences.version=1

Then run the 'update project configuration' by pressing Shift+Alt+U while having your build.gradle open in the editor.

@SupinePandora43
Copy link

SupinePandora43 commented May 3, 2019

same issue, i got dependency in classpath, but vscode in java file says "The import net cannot be resolved Java(268435846)". .\gradlew build, works.
EDIT:
i got errors in all code editors (vscode, idea)!!!
and no errors .\gradlew build
it's my curse, i cant write mcforge mods(

@Shivakishore14
Copy link

Facing the same issue, is there any known workarounds that I could try ?

@snjeza
Copy link
Contributor

snjeza commented Nov 20, 2019

@Shivakishore14 could you provide a project example?

@javachaos
Copy link

//Kludge to deal with VSCODE/BuildShip/EclipsePlugin issue https://github.com/eclipse/buildship/issues/658
eclipse {
	classpath {
		file {
			whenMerged {
				//Define a module as being either a library or a project dependency.
				//Test sources are excluded because eclipse wants them on the classpath for some reason (1)
				entries.findAll { (it instanceof org.gradle.plugins.ide.eclipse.model.Library || it instanceof org.gradle.plugins.ide.eclipse.model.ProjectDependency) && !it.entryAttributes.get('gradle_used_by_scope').equals('test') }.each {
					it.entryAttributes['module'] = 'true'
				}
				
				//Test-scoped stuff should set the appropriate flag
				entries.findAll { (it.properties.kind.equals('src') || it.properties.kind.equals('lib')) && it.entryAttributes.get('gradle_used_by_scope').equals('test') }.each {
					it.entryAttributes['test'] = 'true'
				}
			}
		}
	}
}

Worked for me with Java15 big thanks to user - HoldYourWaffle

@IndikaMaligaspe
Copy link

IndikaMaligaspe commented Jul 7, 2021

I had the same issue and this solved the issue. If the files are already in the correct directory, press F1 and type in "Clean the Java Language Server Workspace"

@CsCherrYY
Copy link
Contributor

I just tried the sample project with the newest extension (1.4.0) and found this issue has been solved. As @IndikaMaligaspe said, for those you had open this workspace before, you can clean the java language server workspace and reload VS Code, it will work.

@RLashofRegas
Copy link

Another example of this issue is the flink training repo: https://github.com/apache/flink-training/
After cloning the repo all of the gradlew commands work as do the build and test tasks within the Gradle for Java extension. However, if I either try to run tests through test explorer or try to build from the "Java Projects" pane I get errors importing org.apache.flink.training.exercises.testing from the common package in the repo.

@snjeza
Copy link
Contributor

snjeza commented Nov 12, 2022

@RLashofRegas You can try the following patch:

diff --git a/common/build.gradle b/common/build.gradle
index d2ba20e..4135ae9 100644
--- a/common/build.gradle
+++ b/common/build.gradle
@@ -17,6 +17,7 @@
 
 plugins {
     id 'java-library'
+    id 'eclipse'
 }
 
 java {
@@ -34,3 +35,11 @@ dependencies {
     testApi "org.apache.flink:flink-test-utils:${flinkVersion}"
     testApi 'org.assertj:assertj-core:3.20.2'
 }
+
+eclipse.classpath.file.whenMerged {
+        def test = entries.find { it.path == 'src/test/java' }
+        if (test) {
+            test.entryAttributes['gradle_used_by_scope'] = 'main, test'
+            test.entryAttributes['test'] = 'false'
+        }
+    }

@RLashofRegas
Copy link

@snjeza Thanks for the quick response. That worked, thanks!

@ghost
Copy link

ghost commented Feb 7, 2023

For me, issue got resolved after cleaning the Java Language Server Workspace in Vscodium.

@reckenreb
Copy link

reckenreb commented Mar 3, 2023

For me, issue got resolved after cleaning the Java Language Server Workspace in Vscodium.

Same for me, but this doesn't seem like a proper fix. I'm developing a large spring boot application and cleaning the Java Language Server Workspace takes about 2 minutes, 5-10 minutes on older machines.

Has anyone found out why exactly this happens? Because for me it seems quite random when I encounter these errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests