Reputation:
Recently I started to learn Java. When I try to do the following code:
public class Application {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("");
sb.append("a");
}
}
I get the following errors:
The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
And
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
at Application.main(Application.java:8)
Upvotes: 0
Views: 5700
Reputation: 1
Simple step is delete the project from Eclipse and import it again. Works for me!!
Upvotes: 0
Reputation: 9507
My guess is that you try to a Java 8 JVM in an old version of Eclipse that does not support it. When the Java version of the JVM is not recognized, I think that the Eclipse behavior is not predictable.
java.lang.CharSequence
was introduced with Java 1.4, but my guess is that a too old version of Eclipse doesn’t recognize the CharSequence
contained in the Java 8 JVM.
Java 8 supports default methods in interfaces. And in JDK 8 a lot of old interfaces now have new default methods. For example, now in CharSequence we have chars and codePoints methods
When using JDK 8 and an IDE with its own compiler, like Eclipse, you have to update the IDE to a version with Java 8 support, even if you are not using the newer Java 8 features.
You have 2 solutions, you can:
The first Eclipse version supporting Java 8 was Eclipse Kepler SR2 (4.3.2) with an additional patch that can be downloaded from the Marketplace.
In my opinion you should udpate to the latest Eclipse Version.
For example, with the current stable version (Luna SR2; Version 4.4.2) no additional patch is necessary.
Go to Menu
> Help
> About Eclipse
. In the Dialog, click on the Installation Details
Button and switch to the Configuration
Tab.
The interesting lines are:
eclipse.buildId=4.5.0.I20150203-1300
...
java.version=1.7.0_45
On the First About Eclipse
Window, you have already the Eclipse Version and the Name of the distribution (Mars-M5) on the screenshot.
Be aware that the JRE used by Eclipse itself is not necessary the JRE used by your java projects.
Using Windows
> Preferences
and Java
> Installed JREs
in the page tree, you will define all available JREs.
With the Add…
button, you can add as many JRE you like.
The checked JRE is the one that will be the “Workspace default JRE”.
If this is not the case you can add a new JRE with the add button.
Eclipse IDE defines an additional concept to “Installed JREs”: “Execution Environment”. A list of Environments is defined. For example: JavaSE-1.6
, JavaSE-1.7
, JavaSE-1.8
... This list is hardcoded in the IDE and depends on the installed version.
In the preference page: Java
> Installed JREs
> Execution Environment
, you can define which JRE will correspond to each specific environment.
Using this additional abstraction that is useful when you work in a team; You cannot guarantee that everyone will have the same installed JREs and will use the same id for each JRE will be used across a the team members. Execution Environment is a much stable denominator that can be shared in a team.
In the Package Explorer, you should see the Java version:
If this is not correct you should open the context menu the JRE System Library
item and select the Properties
menu entry.
You can select:
At the root of your project you have a .classpath
file (The file might be filtered out from the Package Explorer View
, change the filter configuration or use the Navigator View
to open the file). It should looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>
In this case, the project uses the JavaSE-1.8
execution environment.
Maven also defines what the Java version should be. You need to synchronize the Eclipse settings with what is defined in the project pom.xml
file.
You can achieve this with you can select Maven
> Update project configuration
from the Context Menu available on your project.
Sometimes Eclipse is confused (in particular if you just changed some settings). Using the clean function can help. Open the menu: Project
> Clean
...
Select your project from the list or "Clean all projects" and check "Start a build immediately" if this is relevant.
Related answers that helped me:
Upvotes: 3