“无法解析导入 java.util.function”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25129781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The "import java.util.function cannot be resolved" error
提问by StacyM
I'm trying to run this practice scriptfrom the standard Oracle Java tutorials.
我正在尝试从标准 Oracle Java 教程运行此练习脚本。
This seems to be a common error and I've used SO resources to make attempts to fix this. I've tried Cleaning the project, refreshing the project, switch workplace and switch back, removed and re-added the JRE7.
这似乎是一个常见错误,我已使用 SO 资源尝试解决此问题。我试过清理项目,刷新项目,切换工作场所并切换回来,删除并重新添加 JRE7。
I don't know what else to do.
我不知道还能做什么。
import java.util.List;
import java.util.function.Consumer; -----> cannot be resolved ERROR
import java.util.function.Function; -----> cannot be resolved ERROR
import java.util.Comparator;
import java.util.function.Predicate; -----> cannot be resolved ERROR
import java.lang.Iterable;
import java.time.chrono.IsoChronology; -----> cannot be resolved ERROR
public class LambdaExpressions_RosterTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
采纳答案by Elliott Frisch
Per the java.util.function
Javadoc,
根据java.util.function
Javadoc,
Since: 1.8
Since: 1.8
So upgrade to Java 8, or try to find an older version of the tutorial.
所以升级到Java 8,或者尝试找一个旧版本的教程。
I'm new at this. How can you tell what you are running? I'm using Eclipse
我是新手。你怎么知道你在跑什么?我正在使用 Eclipse
To determine your current Java version in eclipse, go to
要在 Eclipse 中确定您当前的 Java 版本,请转到
Help -> About Eclipse -> Installation Details (Button in
lower Left) -> Configuration pane
Look for the line java.specification.version
- on my machine that is
寻找线路java.specification.version
- 在我的机器上
java.specification.version=1.8
Or the line java.runtime.version
- on my machine that is
或者线路java.runtime.version
- 在我的机器上
java.runtime.version=1.8.0_11-b12
回答by Tapan Pandya
Lambda Expressions are newly added into Java 8. They are not available for JRE7. Try to upgrade your eclipse project's JRE to 8 (window -> preferences->java->compiler).
Lambda 表达式是 Java 8 中新添加的。它们不适用于 JRE7。尝试将您的 eclipse 项目的 JRE 升级到 8(窗口 -> 首选项->java->编译器)。
回答by SoftwareDev
i solved this problem by trying following solution
我通过尝试以下解决方案解决了这个问题
Project > Properties > Java Build Path
Select Libraries tab
Select JRE System Library
Click Edit button
Choose an alternate JRE (jre1.8.0_20)
Click Finish button
项目 > 属性 > Java 构建路径
选择库选项卡
选择 JRE 系统库
单击编辑按钮
选择备用 JRE (jre1.8.0_20)
单击完成按钮
回答by Salahuddin Quazi
same problem is coming to me,I make a changes in eclipse.ini file and did -Dosgi.requiredJavaVersion=1.8 instead of -Dosgi.requiredJavaVersion=1.6 and my problem has been resolved.
同样的问题出现在我身上,我在 eclipse.ini 文件中进行了更改,并使用 -Dosgi.requiredJavaVersion=1.8 而不是 -Dosgi.requiredJavaVersion=1.6 并且我的问题已解决。
回答by shivakumar
Ran into this problem when the version I was using in the below properties was <1.8. Updating my POM to a version >1.8 resolved my issue.
当我在以下属性中使用的版本 <1.8 时遇到了这个问题。将我的 POM 更新到版本 > 1.8 解决了我的问题。
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
回答by cowang
I ran into this while trying to use Java 9 with Eclipse Oxygen. Eclipse claimed that I was using less than Java 1.8 and asked if I wanted to use 1.8. Saying yes solved the problem, but led to this error message later on.
我在尝试将 Java 9 与 Eclipse Oxygen 一起使用时遇到了这个问题。Eclipse 声称我使用的版本低于 Java 1.8,并询问我是否要使用 1.8。说是解决了问题,但后来导致此错误消息。
I went in to Window => Preferences => Java => Compiler and saw that this had been changed to 1.8, so I changed it back to 9 and everything seems to be working now.
我进入 Window => Preferences => Java => Compiler 并看到它已更改为 1.8,因此我将其更改回 9,现在一切似乎都在工作。
回答by Yash Jagdale
For idea IntelliJ you need to go to Menu-> Project Structure -> Module here you will see Source tab, above this tab you can see language level. change it to more than 8.
对于idea IntelliJ,您需要转到菜单-> 项目结构-> 模块,在这里您将看到“源”选项卡,在此选项卡上方您可以看到语言级别。将其更改为 8 个以上。
-yash
-yash
回答by IVIike
I had something similar with Apache Camel: the following function was not working anymore
我有一些与 Apache Camel 类似的东西:以下功能不再起作用
String headerKey = exchange.getIn().getHeader("headerKey",String.class);
because of the error-message:
由于错误消息:
The type java.util.function.Supplier cannot be resolved. It is indirectly referenced from required .class files
To solve the issue I change the casting and it worked afterwards:
为了解决这个问题,我改变了铸造,然后它起作用了:
String headerKey = (String) exchange.getIn().getHeader("headerKey");
maybe it helps you or somebody else.
也许它可以帮助您或其他人。
One other point to mention here would be the Apache Camel version itself as stated on the official page linked here.
这里要提到的另一点是 Apache Camel 版本本身,如此处链接的官方页面上所述。