Netbeans "Import Classes" Error Message - java

When I copy codes from one file and paste it to another, a message dialogue opens as "Import Classes" says, "Pasted code refers to the following not imported elements, resolve imports to include them?"
After "OK" then IDE gives message, as in following images (since one does not need to import java.lang classes),
and,
IDE otherwise compiles and runs correctly with no problem.
Environment : NetBeans IDE 8.0 running Java programs on Windows 8.1, Java (JRE, JDK) 8.xx

In a Java file, the java.lang package is implicitly imported, so the suggestion that you should add imports for any java.lang class is not a good suggestion.
If you see that, you should ignore it.
It looks like there's a bug report on this issue.

There is no so serious problem. Hint and importing classes on paste are unrelated, simply importing classess functionality not checks from where it imports classes.

I could ignore if the warning was not persistent or had a check box for not showing the message anymore.
Already, unrelated to this warning, I needed to upgrade Java JDK. So I uninstalled NetBeans 8.0 and Java JDK all together. I installed the latest JDK and installed NetBeans 8.0.2. Now it works fine.
I suppose copying automatic code lines created by NetBeans IDE visual forms and paste them in another file may cause such a problem. At the moment I have no such issue.

Related

Why does any collection object not recognized? (VSCode) [duplicate]

Just starting working on an existing project at work and wanted to use Visual Studio Code as my IDE (I have used it for a recent Rails project and loved it, so wanted to try with Java).
However, whenever I try to open one of the projects I receive a ton of different errors including:
When importing java.io, java.util, or anything similar, the error:
"The import of java.io (or java.util) cannot be resolved"
Existing classes have an error:
"The implicit super constructor is undefined for default constructor. Must define explicit constructor"
Other random "cannot resolve to a type" errors.
All of these seem to stem from some sort of setting error I have with VS Code but can seem to find what it is. I have already uninstalled and reinstalled the RedHat plug-in that enables the Java language for VS Code.
I have tried setting the java_home setting in the extension to the direct location of the install but that didn't work. Tried uninstalling and reinstalling java and that also didn't work.
My operating specs are as follows:
OS: macOS Sierra
VS Code version 1.15.1
JDK version 1.8.0.144
Any idea on what may be causing this? Do you think it may be an error in how I have VS Code set up or if it's an error (or rather incompatibility) with how the existing project is set up?
I ran into a similar issue. The solution was to remove everything from VS Code's workspace storage directory, which was located at $HOME/Library/Application Support/Code/User/workspaceStorage/.
I found this solution here: https://github.com/redhat-developer/vscode-java/wiki/Troubleshooting#clean-the-workspace-directory
Update: This can now be done from within VS Code as of Language Support for Java(TM) by Red Hat Version 0.33.0. Open the command palette and type "java clean" (see official description in link).
As already mentioned previously, you require to clean the project, but that is a bit difficult thing because every folder is a Guid, and you do not know which one to clear, thus requiring you to delete everything. Starting with 0.33.0 version of the plugin you can automatically do that from within the IDE as well, use CTRL + Shift + P and type, java clean, and IDE will show you the suggestion tip for, Java: Clean the Java language server workspace. Upon selection, agree and restart the IDE. It will clean the language server workspace for you.
Another approach can be, the Maven tools within the IDE. If you have this plugin installed, you can use the side bar and utilize the Maven project helper options to perform actions like, clean, install, and package etc. For example, here is the project I am having and the options this shows,
That can be used, graphically, to manage your Maven-based projects. Also, this would work with the Java Extension Pack, not sure yet as to how it would behave with other extensions.
For me: CMD + Shift + P
Then type "Java: Clean Java language Server Workspace"
Note: This will reload/restart vscode as well.
Update:
This appears to not fix it anymore for me. In my case I am using a gradle project, and needed to set the rootProject.name in the settings.gradle to be the same as the folder name that the project is in.
I faced this issue after creating a whole Java project in one computer and then trying to run it on another computer.
After doing everything said in the other answers, what really made VS Code compile was to open each single project java file in VS Code and save it (a simple Ctrl + S). Maybe there is a simpler way of doing it, but that is what worked for me and I hope this helps anyone stuck in this issue.
Press ctrl+shift+p
then search 'java clean' and click 'java: clean java language server workspace' then click restart IDE.
I found another simple trick at least to get rid of "cannot be resolved to a type" errors which were coming from older workspaces and wrong project files I guess?. I just ran an empty main(), with the body commented out, while still keeping my local package / import commands at the start - no errors. After commenting in again, the project compiled without errors. Perhaps this refreshing effect might also help in this context?
I had to clean this folder to get it working on Windows
%APPDATA%\code\Local Storage

Why is Eclipse giving me "groovy.lang.GroovyObject cannot be resolved"?

Specs: Linux Mint 18.3, Eclipse 2019-06, Groovy 2.5.8, Java 11
I started a Groovy project, and created a package "test" under the "source folder". Under test I put a file, "test.groovy". This file looks like this:
package test
It's got a horrid white-cross-in-a-red-box. The error message is:
Multiple markers at this line:
- The type groovy.lang.MetaClass cannot be resolved. It is indirectly referenced from required .class files
- The type groovy.lang.GroovyObject cannot be resolved. It is indirectly referenced from required .class files
I have tried multiple strategies: restarting Eclipse, cleaning the project, removing and putting back the JRM for the project. Nothing doing.
There are a few cases of this error when I Google, but mostly they appear to be from years and years ago, on an active bug list, so I presume resolved.
Any ideas what's going wrong?
NB according to my notes, I have had this problem before a couple of times with Gradle projects, and the problem was resolved by going Gradle --> Refresh for the project. But this is a (pure) Groovy project, not Gradle, and the "Refresh" option is not available.
By the way, when I look at this problem in the "Problems" the error type is said to be "Java Problem".
later
It's been suggested that the cause might be Java 11. This is an interesting idea. However, Groovy 2.5.x in itself is not incompatible with Java 11. Firstly, I have no problem running this at the CLI, and no problem with Eclipse-based Gradle projects configured with Java 11 and Groovy 2.5.x, using Groovy as the language of the app and testing files. Crucially, of course, the Gradle wrapper chooses its own Groovy version, and in such circs the user chooses their own Groovy version for their executables as a dependency in build.gradle. So Eclipse Groovy-handling is mercifully "bypassed".
I am thus beginning to suspect that the Groovy-Eclipse add-in may be the culprit. Amazingly enough, and I was surprised by this, I downloaded and set up the 2019-12 Eclipse package, completely separately, and tried to create a Groovy project in it. Even after several attempts to install both the "release" and "snapshot" versions of Groovy-Eclipse for this version of Eclipse (i.e. here), I cannot start the simplest of Groovy projects in that IDE.
So my suspicions are circling around this theory that Groovy-Eclipse is unable to work properly with Java 11. Only problem is that you might expect to find such a bug as one of the issues, but there is nothing.
Add "requires org.codehaus.groovy;" to your module-info.java

Visual Studio Code - Java - Import Errors and More

Just starting working on an existing project at work and wanted to use Visual Studio Code as my IDE (I have used it for a recent Rails project and loved it, so wanted to try with Java).
However, whenever I try to open one of the projects I receive a ton of different errors including:
When importing java.io, java.util, or anything similar, the error:
"The import of java.io (or java.util) cannot be resolved"
Existing classes have an error:
"The implicit super constructor is undefined for default constructor. Must define explicit constructor"
Other random "cannot resolve to a type" errors.
All of these seem to stem from some sort of setting error I have with VS Code but can seem to find what it is. I have already uninstalled and reinstalled the RedHat plug-in that enables the Java language for VS Code.
I have tried setting the java_home setting in the extension to the direct location of the install but that didn't work. Tried uninstalling and reinstalling java and that also didn't work.
My operating specs are as follows:
OS: macOS Sierra
VS Code version 1.15.1
JDK version 1.8.0.144
Any idea on what may be causing this? Do you think it may be an error in how I have VS Code set up or if it's an error (or rather incompatibility) with how the existing project is set up?
I ran into a similar issue. The solution was to remove everything from VS Code's workspace storage directory, which was located at $HOME/Library/Application Support/Code/User/workspaceStorage/.
I found this solution here: https://github.com/redhat-developer/vscode-java/wiki/Troubleshooting#clean-the-workspace-directory
Update: This can now be done from within VS Code as of Language Support for Java(TM) by Red Hat Version 0.33.0. Open the command palette and type "java clean" (see official description in link).
As already mentioned previously, you require to clean the project, but that is a bit difficult thing because every folder is a Guid, and you do not know which one to clear, thus requiring you to delete everything. Starting with 0.33.0 version of the plugin you can automatically do that from within the IDE as well, use CTRL + Shift + P and type, java clean, and IDE will show you the suggestion tip for, Java: Clean the Java language server workspace. Upon selection, agree and restart the IDE. It will clean the language server workspace for you.
Another approach can be, the Maven tools within the IDE. If you have this plugin installed, you can use the side bar and utilize the Maven project helper options to perform actions like, clean, install, and package etc. For example, here is the project I am having and the options this shows,
That can be used, graphically, to manage your Maven-based projects. Also, this would work with the Java Extension Pack, not sure yet as to how it would behave with other extensions.
For me: CMD + Shift + P
Then type "Java: Clean Java language Server Workspace"
Note: This will reload/restart vscode as well.
Update:
This appears to not fix it anymore for me. In my case I am using a gradle project, and needed to set the rootProject.name in the settings.gradle to be the same as the folder name that the project is in.
I faced this issue after creating a whole Java project in one computer and then trying to run it on another computer.
After doing everything said in the other answers, what really made VS Code compile was to open each single project java file in VS Code and save it (a simple Ctrl + S). Maybe there is a simpler way of doing it, but that is what worked for me and I hope this helps anyone stuck in this issue.
Press ctrl+shift+p
then search 'java clean' and click 'java: clean java language server workspace' then click restart IDE.
I found another simple trick at least to get rid of "cannot be resolved to a type" errors which were coming from older workspaces and wrong project files I guess?. I just ran an empty main(), with the body commented out, while still keeping my local package / import commands at the start - no errors. After commenting in again, the project compiled without errors. Perhaps this refreshing effect might also help in this context?
I had to clean this folder to get it working on Windows
%APPDATA%\code\Local Storage

eclipse can't import java.util?

I recently installed Eclipse 4.3 (Build id: 20130919-0819) onto my Windows 7 x64 machine and imported my old workspace, but all of them had some kind of strange problem (they had red exclamation marks on the project names instead of error x's). So I re-imported one, which got rid of the exclamation mark, but there is exactly one .java source file with errors now.
It says it can't resolve "Import java.util", and of course everything using that also has an error too.
What the heck? Isn't java.util one of the base libraries? Also, I can't seem to find where to download/install packages. Does eclipse have a separate program for that? I seem to remember using a package manager to install a few packages or SDKs or something back when I got eclipse on my Windows XP machine.
BTW yes I selected the x64 windows version, not the x86 one. And if it matters at all, when I tried to unzip it with 7zip, it gave me many errors and wouldn't work, so I tried unzipping with the default windows zip thing and that seemed to work just fine.
import must be spelt with a lower-case "i". Try this!
...additionally, you might be wanting to say "import java.util.*;" because util is a package that contains many objects. The * tells it to import all of those objects!
java.util is the collections framework package name. you need to specify what classes you want to import so you can refer to them using there unqualified names, makes code more readable.
if you want to import all classes in the package use import java.util.*;
if you need to add third party packages to your project, you can use a dependency management tool like maven and this can be easily integrated with eclipse.

Java Error when i press button [duplicate]

This error is just bizarre, my code compiles fine, I can see there are no problems with it, yet this error has just popped up. I have tried re-starting NetBeans and there is no additional exception information.
What can cause this?
If it is Netbeans, try to uncheck "Compile on save" setting in the project properties (Build -> Compiling). This is the only thing which helped me in a similar situation.
I guess you are using an IDE (like Netbeans) which allows you to run the code even if certain classes are not compilable. During the application's runtime, if you access this class it would lead to this exception.
It's caused by NetBeans retaining some of the old source and/or compiled code in its cache and not noticing that e.g. some of the code's dependencies (i.e. referenced packages) have changed, and that a proper refresh/recompile of the file would be in order.
The solution is to force that refresh by either:
a) locating & editing the offending source file to force its recompilation (e.g. add a dummy line, save, remove it, save again),
b) doing a clean build (sometimes will work, sometimes won't),
c) disabling "Compile on save" (not recommended, since it can make using the IDE a royal PITA), or
d) simply remove NetBeans cache by hand, forcing the recompilation.
As to how to remove the cache:
If you're using an old version of NetBeans:
delete everything related to your project in .netbeans/6.9/var/cache/index/ (replace 6.9 with your version).
If you're using a newer one:
delete everything related to your project in AppData/Local/NetBeans/Cache/8.1/index/ (replace 8.1 with your version).
The paths may vary a little e.g. on different platforms, but the idea is still the same.
I also got the same error and I did clean build and it worked.
Add selenium-server-standalone-3.4.0.jar. It works to me.
Download Link
Recheck the package declarations in all your classes!
This behaviour has been observed in NetBeans, when the package declaration in one of the classes of the package refers to a non-existent or wrong package. NetBeans normally detects and highlights this error but has been known to fail and misleadingly report the package as free of errors when this is not the case.
I had the same issue with one of my netbeans project.
Check whether you have correctly put the package name on all the classes. I got the same error message because i forgot to put the package name of a certain class (which was copied from another project).
Disable Deploy on Save in the Project's Properties/Run screen. That's what worked for me finally. Why the hell NetBeans screws this up is beyond me.
Note: I was able to compile the file it was complaining about using right-click in NetBeans. Apparently it wasn't really compiling it when I used Build & Compile since that gave no errors at all. But then after that, the errors just moved to another java class file. I couldn't compile then since it was grayed out. I also tried deleting the build and dist directories in my NetBeans project files but that didn't help either.
Organize your code as a maven module.
Once done run the command from terminal
$mvn installl
to check if your code builds fine.
Finally import the project in netbeans or eclipse as maven project.
change the package of classes, your files are probably in the wrong package, happened to me when I copied the code from a friend, it was the default package and mine was another, hence the netbeans could not compile because of it.
I had this problem with NetBeans 8.0.1. Messages about problem in project deleted class. Deleting the ~/.netbeans didn't work. Also I looked for ANY reference to the deleted class in ALL my projects, nothing found. I deleted the build classes, everything. Then, when I started Netbeans again, compile and magically appears the message in Run and into the mother compiled class. I tried the uncheck "Compile on save" Dime solution, and works, but it's not practical.
Finally, my solution was edit and force recompile of the mother class. This way the new .class doesn't contains the message and Run works OK.
Just check the packaging, the simplest answer I can provide is that your package has been mislabeled (within a class).
Also, you may have some weird characters. Try white-flushing the code in a Notepad (or Gedit) and then pasting it into a newly created class with your IDE.
If you are using Netbeans, try to hit the Clean and Build button, let it do the thing and try again. Worked for me!
I had the same problem. My error was the packaging. So I would suggest you first check the package name and if the class is in the correct package.
Implementing my own functional interfaces resolved this for me (so instead of using java.util.function.* just create your own single-method interface with the parameters and return-type you want).

Categories