不能用Eclipse做StringBuilder [英] Can't do StringBuilder with Eclipse
问题描述
public class Application {
public static void main String [] args){
StringBuilder sb = new StringBuilder();
sb.append(a);
}
}
我收到以下错误:
类型java.lang.CharSequence无法解析。它是从所需的.class文件间接引用的。
和
线程main中的异常java.lang.Error:未解决的编译问题:
无法解析类型java.lang.CharSequence。它是从Application.main(Application.java:8)
间接引用的.class文件。 b
解决方案我的猜测是,您尝试使用不支持它的旧版本的Java 8 JVM。当Java版本的JVM无法识别时,我认为Eclipse行为是不可预测的。
java.lang.CharSequence
被引入与Java 1.4,但我的猜测是,旧版本的Eclipse不能识别Java 8 JVM中包含的CharSequence
。
Java 8支持
@ mkrakhin in类型java.lang.CharSequence不能在包声明中解析
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)
解决方案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 theCharSequence
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 theInstallation Details
Button and switch to theConfiguration
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.
JREs available in your Workspace:
Using
Windows
>Preferences
andJava
>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.
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.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:
If this is not correct you should open the context menu the
JRE System Library
item and select theProperties
menu entry.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 thePackage Explorer View
, change the filter configuration or use theNavigator 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
...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:
- @Holger in Error when using LogManager (l4j2) with Java 8 (java.lang.reflect.AnnotatedElement cannot be resolved)
- @mkrakhin in The type java.lang.CharSequence cannot be resolved in package declaration
这篇关于不能用Eclipse做StringBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!