Friday, 23 August 2013

Simple tutorial example (lambda expression) doesn't run

Simple tutorial example (lambda expression) doesn't run

Finally decided to start a bit of experimentation on the new features of
jdk8, namely the lambda expressions following the tutorial. For
convenience, I stripped-down the example, see SSCCE below.
Typing out the predicate just runs fine, refactoring it to a lambda
expression as suggested (and actually done) by Netbeans compiles (?)
without errors, but doesn't run. The laconic console printout is
Fehler: Hauptklasse simple.Simple konnte nicht gefunden oder geladen werden
("Error: main class couldn't be found or loaded")
Environment:
jdk: jdk-8-ea-bin-b102-windows-i586-08_aug_2013.exe
Netbeans 7.4 beta, bundle from 14.7.2013. Not sure if that's the latest,
couldn't download from the Netbeans site (got a "content encoding error"
when clicking on its download link)
BTW, thought of using Netbeans only because it already has support for
jdk8 (if not netbeans, who else ;-) - the eclipse beta preview from
efxclipse has a similar issue (compiling but not running the example). So
being definitely out off my comfort zone, possibly some very stupid
mistake on my part... ?
package simple;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class Simple {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
List<Member> members = createMembers();
printMembers(members);
// implement predicate directly, runs fine
// Predicate<Member> predicate = new Predicate<Member>() {
// public boolean test(Member member) {
// return member.getAge() >= 18;
// }
// };
// predicate converted to lambda, fails to run
// "class couldn't be found"
Predicate<Member> predicate = (Member member) -> member.getAge()
>= 18;
for (Member member : members) {
if (predicate.test(member)) {
member.printMember();;
}
}
}
public static class Member {
private String name;
private int age;
public Member(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void printMember() {
System.out.println(name + ", " + getAge());
}
}
private static List<Member> createMembers() {
List<Member> members = new ArrayList<>();
members.add(new Member("Mathilda", 45));
members.add(new Member("Clara", 15));
members.add(new Member("Gloria", 18));
return members;
}
}

No comments:

Post a Comment