eclipse can't import java.util? - java

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.

Related

Where do I find the source code for packages that I import in Java?

When I import a package, say e.g. import java.awt.Graphics, where can I find the source code for the Graphics class in my JDK installation on my Mac? I know the path to my installation is /Library/Java/JavaVirtualMachines, however I don't really understand exactly where the class is being generated from in the installation.
Thanks!
Assuming you are using a modern IDE, such as IntelliJ, along with a build tool such as Maven or Gradle, you should actually be able to just CTRL + CLICK on any method in the java.awt.Graphics package to view the decompiled source code. Developing using an IDE with something like Maven is recommended in most cases, so if you are not already doing so, now might be a good time to upgrade.

Trouble with importing MathParser external library in IntelliJ

as above, I cannot get access to classes of external library MathParser imported with Maven in IntelliJ. I've configured pom.xml file and enabled auto-download, so (as seen on the bottom of Project files section) it got downloaded, but when I try to use classes/import with import org.mariuszgromada.math.mxparser.*; - auto and manual import do not work.
Here's a screenshot of current situation
Any ideas?
Created new project for this test purposes, but same thing is happening. In ,,External Libraries" (in project files on the left in the screenshot) the lib appears , nevertheless no way to access to it's classes.
Update:
1) I've deleted jdk, and downloaded the newest 10'th - no results.
2) Reinstalled IntelliJ IDE couple times, same. Also I've tried to set up IntelliJ on Windows in VirtualBox (I need to finish my short project), same with jdk 10, and finally it worked. Nevertheless, I need to make it work without externalising from macOS (also, friend of mine with Mac, didn't have any similar problems). Totally same procedure, but on the macOS side on my Macbook I cannot import anything from this exterbal library (what is possible on virtualised Windows side).
3) I was trying to clean my Mac out of IntelliJ folders, then uninstall it, then imported settings from ,,healthy mac's IntelliJ" - still nothing new.
Solution:
I've reinstalled macOS, and finally that's how I've solved the problem.
Everything seems to be correct. You can try to
directly import of mxParser by changing import to import org.mariuszgromada.math.mxparser.mxParser or
check which classes you have in that package by pressing Ctrl + Space after mXparser package in import statement.

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

Netbeans "Import Classes" Error Message

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.

How can I import org.eclipse.pde.internal with sources?

The problem is, that this package and its children are not seen in the list of packages of "import". When I import all pde packages, org.eclipse.pde.internal somehow becomes usable anyway. But I don't see its sources. How can I manage this package?
Here are some rules from the Eclipse community that might help:
Package names should match plug-in names. If they don't strip from the right. The code you seek should be in the plug-in org.eclipse.pde.internal (doesn't exist). Strip internal and you get an existing plug-in.
OSGi allows to hide packages in plug-in X from other plugins. This feature isn't really used. You can still import the classes but Eclipse don't show them in code completion and you will get warnings. There is an option for the OSGi/bundle classloader to throw NoClassDefFoundError but that option is usually disabled. You can find the exports list in the MANIFEST.MF of the plug-in that you try to import from.
Most of the non-exported packages are in below a package internal, so this is a hint: Don't use this code. It's not part of the official API. It can change in the next version of Eclipse or the plug-in and nobody will care if your code breaks (unlike with the official API).
Of course, the world isn't that simple. Which is why using internal code is frowned upon but the classloader option is disabled (= Eclipse could enforce this behavior but they don't).
If you think some internal API would be great to use, file a bug report at https://bugs.eclipse.org/bugs/
The problem was solved by my colleague.
org.eclipse.pde.internal is in org.eclipse.pde.ui.
Eclipse 3.7.2 simply haven't the PDE sources. Appropriate directories in repository are empty. I downshifted to 3.7.0 and it is OK now.

Categories