0

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)

Jmini
  • 9,189
  • 2
  • 55
  • 77
  • 1
    Do you have any import clauses at the top of the source file? – joval Jul 05 '14 at 21:12
  • 1
    What JRE version are you using (Preferences -> Java -> Installed JREs)? And what compiler version are you using (Project properties -> Java -> Compiler)? java.lang.CharSequence was added in Java 1.4 – mihi Jul 05 '14 at 21:15
  • 1
    @joval no imports needed to use java.lang.StringBuilder – mihi Jul 05 '14 at 21:16
  • Ok, not the obvious one, sorry. When you open project properties -> java build path -> order and export, you should have two entries (your source folder and jre8) of which your source folder is checked and jre8 is not. Right? – mihi Jul 05 '14 at 21:24
  • 1.8 should be ok....sounds stupid but do you tried to restart eclipse? do you using the current eclipse version (maybe a java 8 plugin is needed)? – Aruscher Jul 05 '14 at 21:25
  • And, "Project -> Build automatically" is enabled (or if not, you are sure you built the project after your last change to the source)? – mihi Jul 05 '14 at 21:26
  • If all these is true, try File->Restart, then Project->Clean->Clean all projects (should never be needed, but easier than finding other reasons for messing up your project setup...) – mihi Jul 05 '14 at 21:27
  • @Arusher good point. To run Eclipse itself under JRE8 you need either Kepler version with Java8 update or Luna version. To target JRE8 (with the 1.7 compiler) it is not needed if you run Eclipse itself under JRE7 or older... – mihi Jul 05 '14 at 21:30
  • My Version: Indigo Service Release 2 I tried everything you said and it's still the same, How do I install the plugin or perhaps the version of Luna? –  Jul 05 '14 at 21:55
  • 1
    Eclipse Indigo is indeed a bit old. Either you install a Java7 JRE from java.oracle.com, or latest Eclipse Luna (4.4) from http://eclipse.org/downloads/. If download bandwidth is not a problem, I'd rather upgrade Eclipse than downgrade Java, but since Eclipse is >200MB and a JRE is about 30MB, your mileage may vary – mihi Jul 05 '14 at 22:00
  • Thank you it works! One more question, the Android SDK.. I have to download it again or it will be there from the Indigo version –  Jul 05 '14 at 22:20

2 Answers2

3

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:

  • Use a Java Version that is compatible with your current IDE.
  • Update your Eclipse IDE

Eclipse IDE with Java 8 support:

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.


Identifying the Eclipse Version and the JRE used by Eclipse

Go to Menu > Help > About Eclipse. In the Dialog, click on the Installation Details Button and switch to the Configuration Tab.

About Eclipse

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.


JREs available in your Workspace:

Using Windows > Preferences and Java > Installed JREs in the page tree, you will define all available JREs.

Installed 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.


Additional concept: Execution Environment

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.

Execution Environment - preference page

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.


Ensure the JRE System Library defined for your Project

In the Package Explorer, you should see the Java version:

Package Explorer

If this is not correct you should open the context menu the JRE System Library item and select the Properties menu entry.

JRE System Library - Properties

You can select:

  • One of the defined execution environments (mapped to a JRE in your preferences)
  • One of the available JRE defined in the preferences.
  • The workspace default JRE (also defined in the preferences)

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.


If you are using maven

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.


Rebuild 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...

Eclipse - Clean Projects

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:

Community
  • 1
  • 1
Jmini
  • 9,189
  • 2
  • 55
  • 77
0

Simple step is delete the project from Eclipse and import it again. Works for me!!