即使我在 eclipse 中使用 jre8 也不能使用 java.util.regex.Pattern
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24230469/
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
Can't use java.util.regex.Pattern even though I'm using jre8 in eclipse
提问by GWigWam
The error I get when I hover over scan.useDelimiter("\n");
is: "The type java.util.regex.Pattern cannot be resolved. It is indirectly referenced from required .class files"
悬停时出现的错误scan.useDelimiter("\n");
是:“无法解析 java.util.regex.Pattern 类型。它是从所需的 .class 文件间接引用的”
I have tried re-installing my Java and JDK. JRE system library jre8 is referenced in the Java build path of my project. It's the workspace' default jre. It has rt.jar in it, which I am told, should contain all I need.
我试过重新安装我的 Java 和 JDK。我项目的Java构建路径中引用了JRE系统库jre8。这是工作区的默认 jre。它有 rt.jar,我被告知,它应该包含我需要的所有内容。
When I hit run I get "Exception in thread "main" java.lang.Error: Unresolved compilation problem: at pack.Main.main(Main.java:15)
"
当我点击跑步时,我得到“ Exception in thread "main" java.lang.Error: Unresolved compilation problem: at pack.Main.main(Main.java:15)
”
Line 15 only says public static void main(String[] args) {
The code with the error is in the main class, not in the main method though.
第 15 行只说public static void main(String[] args) {
有错误的代码在主类中,但不在主方法中。
My goal with this piece of code is to read user input, all of it until user hits enter. The delimiter part is there because on default scan.next() stops at spaces, I want the entire line.
我这段代码的目标是读取用户输入,直到用户点击输入。分隔符部分在那里,因为默认 scan.next() 在空格处停止,我想要整行。
Yes, I have cleaned my project.
是的,我已经清理了我的项目。
Eclipse version: Indigo Service Release 2 Build id: 20120216-1857
Eclipse 版本:Indigo Service Release 2 构建 ID:20120216-1857
Some code:
一些代码:
import java.util.Scanner;
import java.util.regex.*;
private static void someMethod(){
Scanner scan = new Scanner(System.in);
scan.useDelimiter("\n");
String pass = scan.next();
}
What is my next step here?
我的下一步是什么?
EDIT: I'm getting a comparible error when using .contrains(String) on a String: The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
编辑:在字符串上使用 .contrains(String) 时出现类似错误:无法解析类型 java.lang.CharSequence。它是从所需的 .class 文件间接引用的
String test = "bla_bla";
if(test.contains("a_"))
回答by Sorin Markov
I had the same issue using JDK 1.8.0_20 and Eclipse 4.4.0. Eclipse kept saying "The import java.util.regex.Pattern cannot be resolved". This also wasn't happening in JDK 7 and 6. From my estimation, there must be something wrong with the package in JDK 8. I've also had the same issue with javax.swing.JTablewith JDK 8, but not 7 or 6, so it must be JDK 8.
我在使用 JDK 1.8.0_20 和 Eclipse 4.4.0 时遇到了同样的问题。Eclipse 一直说“无法解析导入 java.util.regex.Pattern”。这在 JDK 7 和 6 中也没有发生。据我估计,JDK 8 中的包肯定有问题。我也有同样的问题,javax.swing.JTable和 JDK 8,但不是 7 或6,所以必须是JDK 8。
To solve the issue, I went to find a .jar file of the Util package that would contain a working Pattern class. After downloading one from http://www.java2s.com/Code/Jar/r/Downloadregexjar.htm, and incorporating it into my build path, Pattern once again worked. The same solution also solved my issue with the Swing package.
为了解决这个问题,我找到了一个 Util 包的 .jar 文件,其中包含一个工作模式类。从http://www.java2s.com/Code/Jar/r/Downloadregexjar.htm下载一个并将其合并到我的构建路径后,Pattern 再次起作用了。同样的解决方案也解决了我的 Swing 包问题。
This is not the ideal solution, as the .jar file contains a lot of redundant packages already included in JDK 1.8.0_20, but it was the only solution I could come up, besides downgrading to JDK 7 or 6.
这不是理想的解决方案,因为 .jar 文件包含许多已包含在 JDK 1.8.0_20 中的冗余包,但这是我能想到的唯一解决方案,除了降级到 JDK 7 或 6。
I do hope Oracle does fix this issue soon in future releases of JDK 8.
我确实希望 Oracle 在 JDK 8 的未来版本中尽快解决这个问题。
Hope this helps!
希望这可以帮助!