println cannot be resolved as a variable - java

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.

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!

Uncompilable source code - Erroneous sym type:

There was error occurred in Line 14.
Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - Erroneous sym type:
Hard to find solution.
package example;
public class Num
{
public static void main(String [] args)
{
String s = "42";
try
{
s = s.concat(".5");
double d = Double.parseDouble(s);
s = Double.toString(d);
int x = (int) Math.ceil(Double.valueOf(s).doubleValue()); //Line 14
System.out.println(x);
}
catch (NumberFormatException e)
{
System.out.println("Wrong Number");
}
}
}
The code you provided runs fine on my computer.
I'm guessing you are using Netbeans and may be affected by a bug.
Try this:
Open the project properties, select the Build-Compiling, uncheck "Compile on save" and rerun the application. This will make sure all your source code becomes recompiled before running it.
Link: https://forums.netbeans.org/topic43241.html
This is a common issue which I get while working. What I do is clean and build the project. It solves the problem.
Right Click on the project name >> Clean and build
Download/install JDK major version number the same as NetBeans.
i.e. If You have NetBeans 9 version, but your JDK is version 10,then try use NetBeans 10 version... or download/install JDK 9 (the same as NetBeans).
Create new java platform for new jdk installation.
Open netbeans menubar->tools->Java Platforms->Add Platform
Specify the folder that contains the Java platform as well as the sources and Javadoc needed for debugging.
Set new java platform to your project
Right click on project ->properties->Libraries->Java Platform->select the jdk version that same as NetBeans
Select source format of your project
Right click on project ->properties->Sources->Sources/binary format->select the jdk version that same as NetBeans.
If it still gives errors like,
Exception in thread "main" java.lang.RuntimeException:
at javaapplication2.JavaApplication2.main(JavaApplication2.java:1)
C:\Users\James\AppData\Local\NetBeans\Cache\dev\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\James\AppData\Local\NetBeans\Cache\dev\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 1 second)
do as "sinclair" has mentioned above.
Open the project properties, select the Build-Compiling, uncheck "Compile on save" and rerun the application. This will make sure all your source code becomes recompiled before running it.
I received this error after I'd changed the type of a function parameter and it was the calling page that was in error - but I didn't see this until a magical Clean and Build pointed me to the actual issue!
I got this error message because I copied and pasted code without including the package name. I added the package name and it was fixed.
in my case
i have solved using import with package name and class name
like this import package.class1;
Please uncheck "Compile on save"

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

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.

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!

Syntax error, 'for each' statements are only available if source level is 1.5 or greater

Receiving the below error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error, 'for each' statements are only available if source level is 1.5 or greater at Tuto.OneDimArrays.main(OneDimArrays.java:14)
CODE:
public class OneDimArrays {
public static void main(String[] args) {
int[] xabc = new int[5];
xabc[2]=20;
xabc[0]=50;
for(int temp:xabc){
System.out.println(temp);
}
}
}
Please help..
If you are using Eclipse IDE then right click on the project, goto properties, Java Compiler, Check Enable project specific settings and set the Compiler compliance level to greater than 1.5. I solved this issue by setting it to 1.6.
Hope it helps
The error tells you exactly what the problem is. You are using a for-each loop:
for(int temp : xabc)
{
System.out.println(temp);
}
This syntax was only added in Java 1.5, so you appear to be compiling using an earlier version. Without knowing what IDE/environment you're using, I can't tell you how to fix this, but see if you can find a way to compile your code using a more up-to-date version of java.
Note that there is a difference between the JRE (Java Runtime Environment) and the JDK (Java Development Kit). The JRE is used to run java programs on your computer, while the JDK is used to write java programs on your computer. Normally your system only warns you when the JRE is outdated (as most computer users don't write code and probably don't even have the JDK installed). Therefore, even if your JRE is up to date, you won't be able to compile the latest features unless you have the right JDK.
If you're using an old JDK, you can download the latest version here.
If you're using an up-to-date JDK, you will have to change some settings in your project in order to use it.
If for whatever reason neither of these are an option, you can emulate the for-each loop using pre-1.5 language constructs. The most basic approach is described in Pat's answer.
Compile this in a later version of java if you can.
If you are unable to use a later version of java for some reason just use a regular for loop:
for(int i = 0; i < xabc.length; i++) {
int temp = xabc[i];
System.out.println(temp);
}
Try changing compiler compilance level in eclipse:-
click Windows , go to preferences.
select java from the options ,select compiler in drop down menu.
change compiler compilance level to a value 1.5 or greater.
click on the link for reference: https://i.stack.imgur.com/79tvV.png
This is for eclipse.
window -> preferences -> java -> compiler -> configure project specific settings -> double click on created 'project name' -> uncheck 'use compliance from execution environment' -> then select 'compiler compliance level' to 1.5 or higher(best to choose the last level which is maximum and latest)
Done. now apply and close all. re run the program.

Categories

Resources