Today i experienced a strange error. The Strange was if i add the below code block in eclipse IDE it shows no error , but the same piece of code i compile it from cmd its showing error as
inference variable R has incompatible bounds
The Code Piece as follows:
import java.util.*;
import java.util.stream.*;
import java.util.function.*;
public class Test{
public static void main(String[] args){
int[] x = {1,2,3,4,5,6,1,2,3,1,4,65,3,56,24};
System.out.println(Arrays.stream(x).boxed().collect(Collectors.groupingBy(Function.identity(),LinkedHashMap::new,Collectors.counting())));
}
}
I Used same JDK versions(11.0.15 2022-04-19 LTS) both in eclipse as well as cmd prompt to compile it. Is Eclipse is doing some magic here or it is a bug at eclipse IDE.
Related
No matter what I do Lightrun will generate the following error message:
General agent error at jvm_internals.cc:186.
This is my first time using Lightrun.
This is my code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println(new Scanner(System.in).nextInt() + new Scanner(System.in).nextInt());
}
}
The code itself runs perfectly.
I am using IntelliJ IDEA 2022.3.1.
LR doesn’t work on this version. A bit buggy on the ij versions for the past 2 months…
The following code works fine in all online java compilers but eclipse throws compiler error. Is it a bug in eclipse or am I missing some setting somewhere? A simple fix to silence eclipse? online: https://ideone.com/l0bbhz. Note: This is a simplified cooked-up example to just point to the problem. I understand flatMap is not necessary in this case. In the actual case, I really need flatMap
package dummy;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.util.stream.Collectors.toList;
public class LearnJava {
public static void main(String[] args) {
String[] sa = {"ne", "two", "three"};
List<String> l = Arrays.stream(sa)
.flatMap(s -> Collections.singleton(s).stream().map(c -> c.toUpperCase()))
.collect(toList());
System.out.println(l.get(0));
}
}
Error in eclipse console.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from List<Object> to List<String>
at dummy.LearnJava.main(LearnJava.java:13)
My eclipse version:
Eclipse Java EE IDE for Web Developers.
Version: Luna Service Release 2 (4.4.2)
Build id: 20150219-0600
Update: I went with this minor workaround. It works without major refactoring!
.flatMap(s -> Collections.singleton(s).stream().map(c -> c.toUpperCase()))
To
.<String>flatMap(s -> Collections.singleton(s).stream().map(c -> c.toUpperCase()))
The Eclipse compiler is not perfect. Sometimes you'll hit issues such as this. For example, there are currently two bugs open related to flatMap and type interference - 482664 and 502158.
If you believe the code is legit, which is strongly the case when javac compiles it without issues, then you should open a bug and post a snippet there in order to let them know about it. This helps improving the compiler.
When using this code in Eclipse:
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Test {
public static void main(String[] args) {
List<Object> objs = Arrays.asList(new Object(), new Object());
Set<String> s = objs.stream().collect(HashSet::new, HashSet::add, Object::toString);
System.out.println(s);
}
}
I get:
Internal compiler error: java.lang.ArrayIndexOutOfBoundsException: 0 at
org.eclipse.jdt.internal.compiler.lookup.ConstraintExpressionFormula.reduceReferenceExpressionCompatibility(ConstraintExpressionFormula
.java:273)
I know that this is this line which is producing the error:
Set<String> s = objs.stream().collect(HashSet::new, HashSet::add, Object::toString);
Not sure if it's relevant but I'm using:
Eclipse Kepler 4.3.2
Plugins: Eclipse Java Development Tools Patch with Java 8 support (for Kepler SR2) and Eclipse Plug-in Development Environment Patch with Java 8 support (for Kepler SR2)
java.runtime.version=1.8.0-b132
Here's the screenshot:
I know that the collect method is not correct but why I don't have a compiler error telling something like:
- The method collect(Supplier<R>, BiConsumer<R,? super Object>, BiConsumer<R,R>) in the type Stream<Object> is not applicable for the arguments etc.
This looks like Eclipse bug 433085 a duplicate of bug 430766. This is targeted to be fixed in Eclipse 4.4 Luna M7.
I wrote a very simple program on java stack operation. I am using Java(TM) SE Runtime Environment with OS X 10.9.1. I have tried JDK build 1.7.0_13-b20 and build 1.7.0_51-b13, both do not work for me. I just wanted to test the java Stack class. I do not have a private stack implementation. And the $CLASSPATH environment variable is set to empty.
I tested the same program on Windows (Win 8.1) with JDK 1.7.0_25. It worked fine.
import java.util.*;
public class MyStackTest
{
public static void main(String[] args){
Stack<Integer> mys = new Stack<Integer>();
mys.push(5);
while ( ! mys.empty() ) {
System.out.println(mys.peek());
mys.pop();
}
}
}
However, while compiling using javac 1.7.0_13, I got "cannot find symbol" error:
$ javac MyStackTest.java -Xlint
MyStackTest.java:9: error: cannot find symbol
while ( ! mys.empty() ) {
^
symbol: method empty()
location: variable mys of type Stack<Integer>
1 error
I found that if I change the import statement to
import java.util.Stack
the program compiles fine. Why did "import java.util." cause the problem? How can I tell which class in java.util. cause the problem?
Thanks!
You may have a Stack implementation in the same package as your MyStackTest class (in this case the default package name). Comment out everything after new Stack and print out the class type. If it's not java.util.Stack then you've found your answer.
import java.util.*;
public class MyStackTest
{
public static void main(String[] args){
Stack<Integer> mys = new Stack<Integer>();
System.out.println(mys.getClass());
}
}
Another possibility is that you have non-UTF8 characters in mys.empty(). Try deleting that line and hand type it again.
when you run the compiler for execution from the command line (DOS I guess...) it has to be specified the complete route to every class involved in the java file, or every class needs to be in the same package. That's why when you call javac it can't find the Stack class.
I'm using this code:
for (final String code : Locale.getISOCountries())
{
//stuff here
}
But on compile I get this error:
[ERROR] Line 21: No source code is available for type java.util.Locale; did you forget to inherit a required module?
And then a stack trace of compiler errors.
I'm doing both of these imports at the beginning of the class:
package com.me.example;
import java.util.Locale;
import java.util.*;
What can be wrong?
In Netbeans i see the autocomplete options and no syntax error for the Locale object...
Something screwy with your setup, the folllowing program works fine for me.
import java.util.*;
import java.util.Locale;
public class Donors {
public static void main (String [] args) {
for (final String code : Locale.getISOCountries()) {
System.out.println (code);
}
}
}
The fact that it's asking for source code leads me to believe that it's trying to compile or run it in some sort of debugging mode. You shouldn't need the source code for java.util.* to compile, that's just bizarre.
See if my simple test program works in your environment, then try looking for something along those lines (debugging options). Final step: compile your code with the baseline javac (not NetBeans).
UPDATE:
Actually, I have found something. If you are creating GWT applications, I don't think java.util.Locale is available on the client side (only the server side). All of the references on the web to this error message point to GWT and its limitations on the client side which are, after all, converted to Javascript goodies, so cannot be expected to support the entire set of Java libraries.
This page here shows how to do i18n on GWT apps and there's no mention of java.util.Locale except on the server side.
Looks like there might be something fishy in your build environment, as Locale.getISOCountries() should work just fine. Try compiling a small test program manually and see if you get the same error.
Definitely try to boil this down to a minimum, three-line program (or so), compile from the command-line, then put that class into your IDE and see if you still get the error, and if not, then change/add one line at a time until you have the original failing program, looking for what causes the problem. I'm thinking maybe some other import in your code is importing a Locale class? Why in the world would it be looking for source code?
See what happens when you compile this from the command-line:
import java.util.*;
public class LocaleTest {
public static void main(String[] args) {
Locale.getISOCountries();
}
}