简体   繁体   English

即使我在Eclipse中使用jre8,也无法使用java.util.regex.Pattern

[英]Can't use java.util.regex.Pattern even though I'm using jre8 in eclipse

The error I get when I hover over scan.useDelimiter("\\n"); 当我将鼠标悬停在scan.useDelimiter("\\n");时,我得到的错误scan.useDelimiter("\\n"); is: "The type java.util.regex.Pattern cannot be resolved. It is indirectly referenced from required .class files" 是:“无法解析类型java.util.regex.Pattern。它是从所需的.class文件中间接引用的”

I have tried re-installing my Java and JDK. 我尝试重新安装Java和JDK。 JRE system library jre8 is referenced in the Java build path of my project. 我的项目的Java构建路径中引用了JRE系统库jre8。 It's the workspace' default jre. 这是工作区的默认jre。 It has rt.jar in it, which I am told, should contain all I need. 它被告知里面有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) ”中的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) {带有错误的代码在主类中,而不是在main方法中。

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内部版本: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. 编辑:在字符串上使用.contrains(String)时出现可比错误:无法解析类型java.lang.CharSequence。 It is indirectly referenced from required .class files 从所需的.class文件间接引用它

String test = "bla_bla";
if(test.contains("a_"))

I had the same issue using JDK 1.8.0_20 and Eclipse 4.4.0. 使用JDK 1.8.0_20和Eclipse 4.4.0时遇到相同的问题。 Eclipse kept saying " The import java.util.regex.Pattern cannot be resolved ". Eclipse一直说“ 导入java.util.regex.Pattern无法解析 ”。 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.JTable with JDK 8, but not 7 or 6, so it must be JDK 8. 在JDK 7和6中也没有发生这种情况。据我估计,JDK 8中的程序包一定有问题。我在JDK 8中使用javax.swing.JTable也遇到了相同的问题,但在7或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. 为了解决这个问题,我去找了一个Util包的.jar文件,其中包含一个有效的Pattern类。 After downloading one from http://www.java2s.com/Code/Jar/r/Downloadregexjar.htm , and incorporating it into my build path, Pattern once again worked. http://www.java2s.com/Code/Jar/r/Downloadregexjar.htm下载一个并将其合并到我的构建路径中之后,Pattern再次起作用。 The same solution also solved my issue with the Swing package. 相同的解决方案也解决了我的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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM