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

Writing Java in VS CODE shows way too much warnings! #1657

Closed
lakshits11 opened this issue Oct 15, 2020 · 18 comments · Fixed by eclipse-jdtls/eclipse.jdt.ls#1579 or #1847
Closed

Writing Java in VS CODE shows way too much warnings! #1657

lakshits11 opened this issue Oct 15, 2020 · 18 comments · Fixed by eclipse-jdtls/eclipse.jdt.ls#1579 or #1847
Assignees

Comments

@lakshits11
Copy link

Java Code in vs code shows way too much warnings. I mean these are really unnecessary warnings ! Even IDEs like IntelliJ don't show these unnecessary warnings. I want to disable such warnings, but not all the warnings. Is there a way to do so, or can you please do something about it ?

Environment
  • Operating System: Windows 10 Pro
  • JDK version:
    java 14.0.2 2020-07-14
    Java(TM) SE Runtime Environment (build 14.0.2+12-46)
    Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)
  • Visual Studio Code version: 1.50.1
  • Java extension version: 0.69.0
Steps To Reproduce

Download the folder below and open in vs code

assignmentSix.zip

Here we see that under MonthException we see a squiggle line with warning:

a

Warning Given:
The serializable class MonthException does not declare a static final serialVersionUID field of type longJava(536871008)

Even IDE like IntelliJ dont show these useless warnings...
b

So is there any way to disable these warnings _(NOT ALL WARNINGS)_ I mean can I only select what warnings I want to see and what I don't?

@snjeza
Copy link
Contributor

snjeza commented Oct 15, 2020

@lakshits11 You can try to add

org.eclipse.jdt.core.compiler.problem.missingSerialVersion=ignore

to your your_project/.settings/org.eclipse.jdt.core.prefs

@lakshits11
Copy link
Author

I did that but still it is showing me squiggle line under there !

@snjeza
Copy link
Contributor

snjeza commented Oct 15, 2020

The .settings/org.eclipse.jdt.core.prefs file have to be placed to your project directory as follows:

your_project
  - .project
  - .classpath
... your other files...
  - .settings
    - org.eclipse.jdt.core.prefs

Could you attach a project example?

@lakshits11
Copy link
Author

Yes, see the image ..
aa

@snjeza
Copy link
Contributor

snjeza commented Oct 16, 2020

@lakshits11 Could you, please, attach a project example?

@lakshits11
Copy link
Author

You mean the zip file of my project ? If I am wrong, please help me to understand.

@snjeza
Copy link
Contributor

snjeza commented Oct 16, 2020

You mean the zip file of my project?

Right.

@lakshits11
Copy link
Author

assignmentSix.zip

@0dinD
Copy link
Contributor

0dinD commented Oct 16, 2020

@snjeza I have an idea for why the Eclipse preference doesn't seem to be working. It seems that the language server won't recognize any Eclipse preferences under .settings in "invisible projects" or whatever they're called. For me, it works in any normal maven/gradle/eclipse project, but not in a simple "invisible project" like this one. Is there a reason behind this or should it be considered a bug? (And can you confirm?)

@lakshits11 An alternative way to get rid of warnings is with the @SuppressWarnings annotation:

package Exceptions;

@SuppressWarnings("serial")
public class MonthException extends Exception {

    public MonthException(String s) {
        super(s);
    }

}

The annotation is a way to explicitly ignore certain warnings from within your code, which means that even other people working on the same piece of code won't see the error, regardless of their editor settings. FYI the missingSerialVersion warning is not specific to this extension, it's also present in the Eclipse IDE. The drawback with this solution is that you need to write this annotation on every class that will generate the warning, which can be tedious.

If I'm right about why the Eclipse preference to disable the warning doesn't work, I have another workaround you can try which is to add some files to initialize your project as an Eclipse project instead of an "invisible project". This way, the extension should be able to recognize .settings/org.eclipse.jdt.core.prefs. Try the following:

Add a file called .project to your root project folder with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
	<name>assignmentSix</name>
	<comment></comment>
	<projects>
	</projects>
	<buildSpec>
		<buildCommand>
			<name>org.eclipse.jdt.core.javabuilder</name>
			<arguments>
			</arguments>
		</buildCommand>
	</buildSpec>
	<natures>
		<nature>org.eclipse.jdt.core.javanature</nature>
	</natures>
</projectDescription>

Also add a file called .classpath to your root project folder with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
	<classpathentry kind="src" path="src"/>
	<classpathentry kind="output" path="bin"/>
</classpath>

Finally, after adding these two files, you should also restart VS Code. Did it work?

@snjeza
Copy link
Contributor

snjeza commented Oct 16, 2020

If I'm right about why the Eclipse preference to disable the warning doesn't work,

@0dinD You are right. We should link the .settings directory to an "invisible project" too.

@lakshits11
Copy link
Author

@0dinD After adding @SuppressWarnings("serial") it had gone but when I removed this and tried the other workaround, nothing happened , the warning line again came. Isn't there a way without changing the source code ?

@0dinD
Copy link
Contributor

0dinD commented Oct 17, 2020

@lakshits11 Yes, you should be able to ignore the warning without adding the @SuppressWarnings annotation. This used to work by just adding the related preference to .settings/org.eclipse.jdt.core.prefs, but as we've unfortunately found out this is currently broken for the so-called "invisible projects". This feature with the invisible projects was added a while ago to reduce the amount of unnecessary files in your project directory.

I'm sure we'll find a fix for this in a future update, but in the meantime your best option is to convert the project into an Eclipse project, by following the instructions in my previous comment. If my instructions didn't work, I just realized there could be another action you'll have to take, which is to make sure the Java Language Server is running in "standard" mode. Please try the following:

  1. Check that you have the following project structure:
  • assignmentSix - your project folder
    • .settings - a folder
      • org.eclipse.jdt.core.prefs - a file with the contents @snjeza gave you
    • .project - a file with the contents I gave you
    • .classpath - a file with the contents I gave you
    • ... - the rest of your project files
  1. Restart VS Code, and then open any Java file.

  2. Make sure that the Java Language server is running in standard mode by checking the icon in the bottom right corner. There should be a thumbs-up icon, otherwise the language server is either not started yet or running in lightweight mode. If it's running in lightweight mode, you can start standard mode by pressing the rocket ship icon. See the gif below for which icons I mean and how to activate standard mode:
    disable-warning

  3. If you want standard mode to be enabled all the time, change the VS Code setting called java.server.launchMode to "Hybrid" or "Standard".

Let me know if this works, I've based the solution on the assumption that you've enabled lightweight mode, since it's the most obvious reason I see for why my previous instruction didn't work.

@snjeza Shouln't we also be able to detect Eclipse preferences when running in lightweight mode? It seems like an annoyance to have to switch to standard mode just because you want to ignore a warning or something similar. Also, should we track these problems with detecting Eclipse preferences in this issue (maybe we can rename it?), or should I create a new one for that?

@snjeza snjeza self-assigned this Oct 17, 2020
@lakshits11
Copy link
Author

@0dinD I am sorry to say but it didn't worked 😥 . I am attaching a screenshot for reference.

Screenshot (41)

@0dinD
Copy link
Contributor

0dinD commented Oct 18, 2020

@lakshits11 The screenshot tells me that the Java Language Server is now running in standard mode, so it's strange to me that the warning still shows up. However, I just came to realize I forgot one small step in my instructions when I retraced my steps using the project you attached.

Can you try running the command Java: Clean the Java language server workspace from the command palette (press Ctrl + Shift + P to open)? You will be prompted to confirm the action, press the "Restart and delete" button.
clean-workspace-prompt

This command will clean up workspace settings for the Java Language Server, which seems to be necessary after converting the project into an Eclipse project, in order for the .settings/org.eclipse.jdt.core.prefs to take effect.

After doing this, you should see the warning disappear (fingers crossed). Sorry it had to be this complicated, this is definitely a bug with invisible projects (and lightweight mode).

@lakshits11
Copy link
Author

@0dinD After I did what you told me to do , the curvy warning lines were gone, BUT ...wait wait wait, now when I run my App.java file using java test runner extension which comes in java extension pack, it was not able to recognize my MonthException and YearException java files and Build was failed in which error in the LOG file was MonthException and YearExceptioin cannot be ressolved to a type ! ALTHOUGH , the Date Exception was recognizable and it also created a DateException.class file as output ! I mean what the .... ? How funny and disgusting is it at the same time !! It is not showing any warning / curvy line now but I am pretty sure it all happened after clearing WorkSpace cache...before it, the code was running but the warning curvy lines were still there. I am attaching the screenshots for the same.

** 1. Here we can see that it's not showing any error/warning etc.**
Screenshot (42)

** 2. But the build was failed after I run the code. **
Screenshot (43)

** 3. ERROR Message in LOG file**
Screenshot (44)

@snjeza
Copy link
Contributor

snjeza commented Oct 18, 2020

@lakshits11 could you attach your current project?

@lakshits11
Copy link
Author

assignmentSix.zip

@snjeza
Copy link
Contributor

snjeza commented Oct 19, 2020

@lakshits11 I can't reproduce the issue.
You can try to run the Java: Clean the Java language server workspace command.

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