The "import java.util.function cannot be resolved" error - java

I'm trying to run this practice script from the standard Oracle Java tutorials.
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.
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
}
}

Per the java.util.function Javadoc,
Since:
1.8
So upgrade to Java 8, or try to find an older version of the tutorial.
I'm new at this. How can you tell what you are running? I'm using Eclipse
To determine your current Java version in eclipse, go to
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=1.8
Or the line java.runtime.version - on my machine that is
java.runtime.version=1.8.0_11-b12

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

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).

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.
-yash

I had something similar with Apache Camel:
the following function was not working anymore
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.

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.

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.
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>

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.
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.

Related

eclipse error new to code please

so I coded a little thing to try and I don't know why eclipse isn't running my code. Please help I'm new to coding. Error occurred during
import java.util.GregorianCalendar;
import java.util.Scanner;
public class Calendar {
public static void main(String []args) {
GregorianCalendar cal1 = new GregorianCalendar();
Scanner input = new Scanner(System.in);
System.out.println("Please enter your hire year (yyyy): ");
int hireYear = input.nextInt();
int currYear = cal1.get(GregorianCalendar.YEAR);
System.out.println("He's worked here for " + (currYear - hireYear) + "years!");
}
}
initialization of boot layer
java.lang.LayerInstantiationException: Package jdk.internal.jimage.decompressor in both module java.base and module jrt.fs
this seems to happen when there's multiple versions of java (previous to 9 and 9+)
see this bug report for eclipse
maybe try using another IDE or use command line for compiling and running
Are you using Eclipse Photon? This isn't officially released yet (but I've found it quite stable). It does look like Bug 532490, which is a very recent regression and it will be fixed in the next ~ 6-weekly milestone update.
In the meantime, right-click your project > Build Path > Configure Build Path > 'Libraries' tab. Double-click "JRE System Library" and change it from 8 to 10. Build & run and the problem should be gone.
As a rule it is good to compile and run using the same JRE/JDK. It is possible to mix and match these, but I would say it should only be done in certain "advanced usage" scenarios.
The only thing that worked for me when I had this error was uninstalling then reinstalling Eclipse. Then I made sure to make all future java projects with java version 1.8 (when creating a java project, under JRE, select "use an executing environment JRE:", then select "JavaSE-1.8"). Hope that helps!

println cannot be resolved as a variable

I am studying object oriented programming in my university and my professors is making an example of streams on java. Unfortunately when I try to run this code it says "println cannot be resolved as a variable" on System.out::println.
It also says to delete the two colons,
while on my professor's eclipse it does work.
This is the code. I am running it on eclipse for ubuntu and my version of java is 1.8 update 91
package stream;
import java.util.Arrays;
public class StreamExamples {
public static void main(String[] args){
String text = "I have no idea what is the problem";
String[] words = text.split(" ");
Arrays.stream(words)
.sorted()
.limit(3)
.forEach(System.out::println);
}
}
Most likely the version of Eclipse you are running does not understand Java 8 syntax.
You need to be running the current Eclipse 4.6 (Neon) or the previous release 4.5 (Mars) for full Java 8 support. You code works fine on Eclipse Neon.
1) Check your build path by right clicking on your project, Build Path > Configure Build Path. The libraries tab should show a JRE with version 1.8, if that is not the case, click Edit and select a JRE that is at least version 8.
2) Check the compiler settings by right clicking on your project, Properties > Java Compiler. Change the Compiler compliance level to 1.8.

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!

Eclipse returns error on run: "java selection does not contain a main type"

I am unable to run the following code in Eclipse (Eclipse IDE for Java Developers, Version: Indigo Service Release 1, Build id: 20110916-0149), and I think that I may have a configuration problem in Eclipse (but I do not know what or where):
class Saluton {
public static void main(String args[])
{
String greeting = "Saluton mondo!";
System.out.println(greeting);
}
}
I am also running against Java 6 (1.6.0_29-b11-402) on Macintosh 10.7.2.
When I run this, I get the error:
"java selection does not contain a main type"
but I am pretty sure that my class is written correctly...? I have looked this error up, and cannot explain this problem so far.
TIA for any thoughts or opinions!
I think you have to make your class public, otherwise it won't work?
EDIT: My previous answer was incorrect. The JLS says you can declare arrays with the brackets at either end. See here for some examples. My mistake.
Have you verified your configuration in Eclipse such that it knows where to find the Java compiler, and a JVM? You can check the project-specific libraries by right clicking the project and going to Properties, Java Build Path
You can also check the JRE's installed by clicking Window, Preferences, Java, Installed JREs. Make sure the JRE you wish to use is listed here and that the path is correct.
Another solution (simple and direct):
In Eclipse: File -> Restart
Right click your class Saluton and choose Run as --> Java Application.
It should work.
Ok, so I'm a fellow noob, and I came here because I had this problem. I checked all my class paths and everything, and they were correct. I had actually been putting all of my files in the JRE System Library Folder, instead of the source folder. I'm not sure if this was you'r problem, but it seemed to work for me when I moved the code files into the src folder.
I took the long way around to solve this; eventually I created a package within my project and added my source code to that package and it compiled and ran! I am learning that Wizards rule!
It will work if you make your class as public !

Fatal error by Java runtime environment

I am executing a junit test case
I got the following error,
A fatal error has been detected by the Java Runtime Environment:
Internal Error (classFileParser.cpp:3174), pid=2680, tid=2688
Error: ShouldNotReachHere()
JRE version: 6.0_18-b07
Java VM: Java HotSpot(TM) Client VM (16.0-b13 mixed mode windows-x86 )
Can any body please suggest the solution to resolve
I got the same problem, but with alot of googling I found the answer! See this page
Quote from the link:
# An unexpected error has been detected by Java Runtime Environment:
#
# Internal Error (classFileParser.cpp:2924), pid=5364, tid=6644
# Error: ShouldNotReachHere
That's because we are using Android's JUnit stub implementation. Go to Run -> Run As -> Run configurations again and in the recently created JUnit configuration Classpath's Bootstrap Entries remove Android Library
Then Add Library, using Advanced... button, and add JRE System Library and JUnit 3
Apply and Run
Try this, it worked for me.
You'll need to take this up with Sun -- looks like a JVM bug to me. If it's reproducible, you should be able to run java in such a way as to generate more details (e.g. -verbose, etc). If you can reduce it to a minimal case that triggers the bug (source code always helps!), that also goes a very long way.
http://java.sun.com/developer/technicalArticles/bugreport_howto/index.html
http://bugreport.sun.com/bugreport/crash.jsp
In the meantime, you might want to try it with a different JVM implementation (maybe even an older patch level of the Sun JRE).
Go to Run As -> Run Configurations... and select the configuration you are using.
Select the Class Path tab and select BootStrap Entries.
Click on Advance, then Add Library and select JRE System Library.
Bring it up and make it the first entry in the BootstrapEntries List.
Apply and Run...
Another possible explanation: hardware failure. Ruled out if you can reproduce the error on different machines.
I resolved this by
Quit eclipse
Delete the bin and gen directories in your project
Start eclipse
Rebuild your project
I just recently found solution for this issue that was posted by devdanke:
"As of 11-July-2010 and Android 2.1, the work around I use is to segregate tests into different classes. Any test(s) that don't call any Android APIs go into their own classes. For each of these classes, I remove the reference to Android in their Run Configurations, Classpath tab."
The problem with having it configured class by class is then is not possible to run all tests in project. Better approach is creating 2 test projects with different sets of libraries.
Standard Android JUnit Test project can be created following link, and sample test class looks like:
import android.test.AndroidTestCase;
public class ConverterTest extends AndroidTestCase {
public void testConvert() {
assertEquals("one", "one");
}
}
Then JUnit Test project can be converted from Android JUnit Test project by removing Android Library from project build path, and adding JRE System Library, and JUnit 3 library, and sample test class looks like:
import junit.framework.TestCase;
public class ConverterTest extends TestCase{
public void testConvert() {
assertEquals("one", "one");
}
}
I have had a similar problem, I found it was because I had generated a new activity with a main[] stub entry. Once I deleted the main[] code from the new activity templatye the error went away.
YMMV
This could be a JVM bug; see #Zac's answer. But it could also be that your junit test case is causing a corrupted bytecode file to be loaded. Try rebuilding all your .class files, and if that does not fix the problem try refetching any external libraries that your code depends on.
Do you run on a supported platform (Windows, one of a few Linux versions?) If not, that is the first to try.
If you ARE on a supported platform, then downgrade to _17 and see if THAT helps.
Then make a bug report to Sun and hope they will fix it someday (unless you want to give them money for fixing it faster).
Go to Run As -> Run Configurations->classpath->BootStrap Entries
Click on Advance, then Add Library and select JRE System Library as a first entry.
Apply and Run...
I am not sure whether you were able to reach the solution for your problem or not but your question just popped up while I was searching for the solution for the same problem I am facing. And I got one solution from the stack itself, so just thought to share a link with you if that aids you by any means. The link is as below:
Can't run JUnit 4 test case in Eclipse Android project
Another possible reason (for future references):
I had accidentally copied in a main method in my code, causing Eclipse to recognize the project as a java application, thus launching it with that configuration.
To solve it I went into Run > Run Configurations... and then changed from my presumed main in java application to the main activity of my android application simply by choosing it in the left column.

Categories

Resources