Simple tutorial example (lambda expression) doesn't run

25,152

Solution 1

Your code example lacks the printMembers() method, however this seems not to be the real problem. You were running into a Netbeans bug I ran into today as well. It seems that the Netbeans developer marked this over four years old bug as “resolved” but the solution is still the same: you have to turn off the “Compile on Save” options in the settings (Project Properties/Build/Compiling) and then compile manually (e.g. pressing F9). Then it works.

With the option not disabled you will see the class files being created when saying “Clean and Build” but suddenly disappear when telling Netbeans to run the code.

As a next step I suggest the code improvement by Josh M which is how you really ought to use lambdas.

Solution 2

That is not necessary, you should try doing something like this:

members.stream().filter(m -> m.getAge() >= 18).forEach(Member::printMember);

Share:
25,152
kleopatra
Author by

kleopatra

Java desktop enthusiast and consultant

Updated on August 29, 2020

Comments

  • kleopatra
    kleopatra over 3 years

    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();
            // 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;
        }
    
    }
    
    • Rohit Jain
      Rohit Jain over 10 years
      Did you try running it from Command line?
    • kleopatra
      kleopatra over 10 years
      @RohitJain no, I hate the command line even more than Netbeans ;-) Curious: does the example (using the lambda) run in your context, either IDE or commandline?
    • Rohit Jain
      Rohit Jain over 10 years
      It runs fine on IntelliJIdea. Well, the problem is somewhere else. You are probably trying to run a different file. The error you got suggests this only. Make sure you are running the same file.
    • kleopatra
      kleopatra over 10 years
      @RohitJain tried that (kind of): it's the only one with that name - strange (for me) that just commenting the lambda and uncommenting the typed-out predicate makes it run. Suspect that is is indeed something similarly stupid (as another file), though
  • kleopatra
    kleopatra over 10 years
    that's later down in the original example :-) Anyway, thanks - will certainly try once I get the most simple part running. Update: tried it, same result as with lambda as predicate (that is, not running with same error message)
  • kleopatra
    kleopatra over 10 years
    that's it - thanks a bunch! Yeah, certainly will, now that I can actually run it :-) BTW, do you have a bug # for the Netbeans issue?
  • Holger
    Holger over 10 years
    I found the solution here stackoverflow.com/questions/3851592/… but ofcourse detecting that disappearing of classes was the tricky part. A bug mentioning disappearing classes together with that option is under netbeans.org/bugzilla/show_bug.cgi?id=163169
  • kleopatra
    kleopatra over 10 years
    thanks for the details! Commented on the duplicate, that's reported to be fixed: netbeans.org/bugzilla/show_bug.cgi?id=151845