简体   繁体   中英

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"); is: "The type java.util.regex.Pattern cannot be resolved. It is indirectly referenced from required .class files"

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.

When I hit run I get " 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.

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.

Yes, I have cleaned my project.

Eclipse version: Indigo Service Release 2 Build 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

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

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.JTable with JDK 8, but not 7 or 6, so it must be 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.

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.

I do hope Oracle does fix this issue soon in future releases of JDK 8.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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