Can't get lambda's to compile in Netbeans 7.1.1+JDK8 (jdk-8-ea-bin-b35) [duplicate] - netbeans-7

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Lambda expressions don't work in Java 8?
Simple, pointless app with a lambda:
public static void main(String[] args) {
FileFilter java = (File f) -> f.getName().endsWith(".java");
}
Set it to use JDK 8. Compile/Runs fine if I don' try to use a lambda.
However if I do I get:
Compiling 1 source file to ...\JavaApplication1\build\classes
...JavaApplication1\src\javaapplication1\JavaApplication1.java:20:
error: lambda expressions are not supported in -source 1.8
FileFilter java = (File f) -> f.getName().endsWith(".java"); (use -source 8 or higher to enable lambda expressions) 1 error
Seems to be confused over whether it should be source 8 or source 1.8
Works now when using the lambda specific version, e.g. below runs fine:
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
public class JavaApplication1 {
public static void main(String[] args) {
Arrays.asList(new File("c:/").listFiles((File f) -> f.getName().endsWith(".txt")))
.forEach( file-> {System.out.println(file);});
}
}

In order to get lambda expressions to compile, you will need to download the version of jdk from http://jdk8.java.net/lambda/. You will also get red squiggles on lines that contain lambda expression, but it will compile and run just fine, so just ignore them :)

Download the lambda binary and then replace your old jdk8 with this new one, which has support for binary.
Then it should work.

Related

How to get rid of the compile time error (despite using the import) for assertThrows? (Java, Eclipse Oxygen)

I am testing out the following code snippet that I found here.
Eclipse Oxygen Version: Oxygen.2 Release (4.7.2) - if that matters
import org.junit.jupiter.api.Assertions;
....
#Test
void exceptionTesting() {
Executable closureContainingCodeToTest = () -> {throw new IllegalArgumentException("a message");};
Assertions.assertThrows(IllegalArgumentException.class, closureContainingCodeToTest, "a message");
}
However, the code doesn't compile.
I am getting the error below:
The method assertThrows(Class, Executable, String) in the type Assertions is not applicable for the arguments (Class, Executable, String) DbHandlerTest.java line 96 Java Problem
Of course my goal is not just to test the above snippet but to write a test for my code. Please help.
I figured out the problem ...
Thanks somuras for the right question.
Following import was wrong
import org.junit.jupiter.api.Executable;
It should have been this:
import org.junit.jupiter.api.function.Executable;

How to include code excerpts using tags in asciidoc?

I can include the full Greet.java file
public class Greet {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
from within the asciidoc file
== Hello Java
This is how one greets in Java:
[source,java]
.Greet.java
----
include::Greet.java
----
producing the documentation
But suppose that I only want to include an excerpt from the code delimited by tags. In the code above, suppose I only want to include the main function.
I don't see symbolic tags in the documentation, but this page suggests that it's sufficient to write
public class Greet {
// tag::helloMethod[]
public static void main(String[] args) {
System.out.println("Hello World!");
}
// end::helloMethod[]
}
and
== Hello Java
This is how one greets in Java:
[source,java]
.Greet.java
----
include::Greet.java[tags=helloMethod]
----
That just produces:
Can you suggest a method that would include just the excerpt? I'm using asciidoc 8.6.9.
What you're doing should work fine in Asciidoctor (the Ruby implementation of AsciiDoc), but not AsciiDoc (the Python implementation).
Notice though the different mechanism for obtaining syntax highlighting.
To get syntax highlighting with asciidoc one uses a command-line switch asciidoc -a source-highlighter=pygments file.adoc.
No command-line switch is needed (or possible) with Asciidoctor. With AsciiDoctor syntax highlighting is obtained by:
Inserting :source-highlighter: pygments at the top of each source file, and
Running sudo gem install pygments.rb to install pygments.
The Asciidoctor tags option can include multiple tags as well;
[tags="tag 1, tag 2, …"]
Bringing in more code excerpts in one go…

Executing an unused lambda expression in debug session throws ClassNotFoundException

This is a bit nitpicky- I wonder if it's a bug or a feature:
I have this main in Intellij:
public static void main(String[] args) throws InterruptedException {
Comparator<String> comp = (s1,s2) -> 1;
System.out.println("Break here");
}
When I debug and break at the "System.out.." I see that comp is initialized. However, when I try to execute it from "Expression Evaluation" window I get a ClassNotFoundException!
Of course evaluating the same thing in code works perfectly. Is it somehow related to the way lambdas are implemented under the hood or just a bug in the IDE?
I am using Intellij 13.1.4.
Evaluation of Lambda expressions is supported only starting from version 14.
Taken from What's New in IntelliJ IDEA 14 page:

toolprovider.getsystemjavacompiler() returns null

First, I am seeing a lot of questions about the use of the JavaCompilerAPI, I want to clarify that I am creating an on-line simulation builder that takes too many inputs from the user to precreate classes. That is why I am using a java compiler in order to write the classes using the user's inputs.
As for my problem, I have tested with some basic compiler programs, and am presently working of code found here: Dynamic Compiling Without Create Physical File
The compilation of the code is successful, however when I run the code,
ToolProvider.getSystemJavaCompiler();
returns null.
From other entries I understand one cause might be that the default java.home is JRE, so I added the line where I set java home to my JDK version:
System.setProperty("java.home", "C:\\Program Files (x86)\\Java\\jdk1.7.0_51;");
I have also added tools.jar to the folder with my program, and called the program specifying tools.jar in the classpath like so:
java -cp ".;tools.jar" Compiler
These approaches have not changed anything. Any ideas about what might be the problem?
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.*;
public class Compiler {
static final Logger logger = Logger.getLogger(Compiler.class.getName());
static String sourceCode = "class HelloWorld{"
+ "public static void main (String args[]){"
+ "System.out.println (\"Hello, dynamic compilation world!\");"
+ "}"
+ "}";
public void doCompilation() {
System.out.println(System.getProperty("java.home"));
System.setProperty("java.home", "C:\\Program Files (x86)\\Java\\jdk1.7.0_51;");
System.out.println(System.getProperty("java.home"));
SimpleJavaFileObject fileObject = new DynamicJavaSourceCodeObject("HelloWorld",sourceCode);
JavaFileObject javaFileObjects[] = new JavaFileObject[]{fileObject};
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
System.out.println(compiler);
StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);
...

Debugger cannot see local variable in a Lambda

I noticed that when I hover my mouse over a local variable when my debugger is stopped inside a lambda it will report Cannot find local variable 'variable_name' even if it's visible inside the lambda and it's used.
Example code
public class Main {
public static void main(String[] args) {
String a = "hello_world";
m1(a);
}
private static void m1(String a) {
AccessController.doPrivileged((PrivilegedAction<String>) () -> {
System.out.println("blala " + a);
return "abc";
});
}
}
Try with a breakpoint in System.out.println("blala " + a); and after return "abc" and it always report the same error.
I used AccessController.doPrivileged because it's what I used in my original code and of course i'm using Java 8.
It says the same thing in Watchers and Evaluate Expression.
I tried using the "anonymous class" version and the debugger sees the value of a correctly
private static void m1(String a) {
AccessController.doPrivileged(new PrivilegedAction<String>() {
#Override
public String run() {
System.out.println("blala " + a);
return "abc";
}
});
}
I'm missing something about lambda expressions or it's an IntellIJ IDEA 14 bug?
I don't want to report the bug right now because I already reported a bug that was caused by my code instead of IntellIJ IDEA, so I want to be sure before do something (and because I don't use Java 8 so often, so I could be wrong).
This appears to be a know issue. According to JetBrains the root causes of this behavior is with the JDK. For more info see: IDEA-126257
I can confirm what is written in IDEA bug report linked by Mike Rylander: this is a JDK bug and update to version 8u60_25 of the JDK solves it.