1 2 Previous Next 22 Replies Latest reply: Jun 10, 2014 10:42 PM by Nhuy Van RSS

    Eclipse does not recognize "Default Method keyword" and "Lamda expression"

    Nhuy Van

      Hi guys, I just started learning Java, so I don't know much about it. I have this very annoying issue with Eclipse (I have the latest version installed). For some reason, every time I use the "default" keyword in an interface, it gives me an error similar to "Syntax error on token default",  I deleted the "default" keyword, the error is gone. The same thing happens with "Lamda expression as well", say I have this object like this Actions myActions = () -> {System.out.print("Blah blah blah");};   , Eclipse also displays the error message similar to "Method body expected after (), delete '->' ". I checked the Java version I have, it is the latest one also, I don't know what it's wrong here, any suggestions are greatly appreciated. Thank you very much for your time and help.

        • 1. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
          gimbal2

          Don't just post a description and singular snippets of the code - post the actual code. And don't post a description of the error, post the error.

          • 2. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
            rp0428

            Java 1.5 does NOT support the DEFAULT keyword or lamda expressions.

             

            I'm just guessing what Java version you are using since you didn't post:

            1. The Java version your code is using

            2. The Eclipse version

            3. The Java version Eclipse is using

            4. Your code

            5. The actual error and stacktrace you are getting

             

            Without that all anyone can do is 'guess'.


            • 3. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
              Nhuy Van

              SORRY, FOR SOME REASON, I AM NOT ALLOWED TO ATTACHE IMAGES, SO I HAD TO COPY AND PASTE EVERYTHING TO HERE

               

              ECLIPSE VERSION NUMBER I HAVE

              Version: Kepler Service Release 2

              Build id: 20140224-0627

              (c) Copyright Eclipse contributors and others 2000, 2014.  All rights reserved.

              Visit http://eclipse.org/

               

              ----------------------------------------------

               

              JAVA VERSION NUMBER I HAVE:

              Version 8 Updates 5  (build 1.8.0_05-b13)

               

              ------------------------------------------------------------------------------------------------------------------------------------

              THIS IS THE INTERFACE I AM MENTIONING

               

              public interface Actions{

                   void eat();

                   default void run() {

                   System.out.println(AnimalName + " can run 10 miles without stopping!!!");

                  }

              }

                /*

                 --- BELOW IS THE ERROR DETAIL I GET WHEN I USE THE DEFAULT METHOD ---

                        -Syntax error on token "default", delete this token

                */

               

                /*

                 --- WHEN I CHANGE "DEFAULT" TO "STATIC", HERE IS WHAT I GET ---

                          Multiple markers at this line

                     - Abstract methods do not specify a body

                     - Illegal modifier for the interface method Run; only public & abstract are permitted

                */

               

              -------------------------------------------------------------------------------------------------------------------------------------

               

              THIS IS THE SOURCE FILE WITH THE LAMDA EXPRESSION

               

              import java.util.Scanner;

               

               

              public class Animal {

                    static Scanner myScanner = new Scanner(System.in);

                    public String AnimalName;

               

                   {

                       System.out.print("What kind of Animal?...");

                      AnimalName = myScanner.next();

                   }

               

                   public Animal() {

                         System.out.println("It is " + AnimalName);

                    }

              }

               

               

               

              class myAnimal  {

                      public static void main(String[] args) {

                      Animal Human =  new Animal();

               

                    Actions myHuman = () -> {System.out.println(Human.AnimalName + " loves ice cream!") ; } ;

                    myHuman.eat();

                    myHuman.run();

                /*

                    --- THIS IS THE ERROR DETAIL ---

                 Multiple markers at this line

                     - Syntax error on tokens, delete these tokens

                     - Syntax error on token(s), misplaced construct(s)

                     - println cannot be resolved or is not a field

                     - Syntax error on token "}", delete this token

                */

                }

              }

              • 4. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                rp0428

                Those errors are telling you that you are NOT using Java 8 for the compilation.

                 

                1. Open a command window
                2. type 'javac -version' and post the results

                3. if that shows version 8 compile your simple interface and post the results of the compilation.

                • 5. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                  gimbal2

                  Good, text is way better than stupid screenshots from which you cannot copy/paste. Do realize that writing in all uppercase is considered shouting or taking an aggressive tone, there is no need to do that.

                  • 6. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                    Nhuy Van

                    , you are way sensitive buddy, I used uppercase so that you can identify each section I mention easier, no yelling or shouting intended.

                    • 7. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                      Nhuy Van

                      JAVA VERSION NUMBER I HAVE:

                      Version 8 Updates 5  (build 1.8.0_05-b13)

                       

                      I have java 8, maybe because Eclipse just doesn't recognize lamda or default method?

                      • 8. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                        rp0428

                        I fail to see how ANY of what you posted is related to what I ask you to do.


                        • 9. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                          gimbal2

                          You make too many assumptions, I was only passing you a hint so you may prevent passing off the wrong idea to other people in the future. Because yes, some people are sensitive to such things, so don't do things that are outside of common forum etiquette.

                           

                          If you want to identify sections, the site has rich editing features that you can utilize. But I'd rather just use proper spacing and keep your posts to the point so you don't need to make the effort.

                          • 10. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                            theRealPedron

                            So, just an FYI for using eclipse, the version you are using, and that comes through on your command-line "java -version" may not be the version you're using in Eclipse.

                             

                            First, you need to check what version of Java you're using in Eclipse:

                            1. Open Eclipse
                            2. Browse to the "Preferences" window
                            3. Click on "Java"
                            4. Click on "Installed JREs"
                            5. In this window you will see a list of directories. Each should point to the JAVE_HOME directory of a separate installation. Do you see the checked one pointing to a Java 8 installation? If NO, then do the following steps:
                              1. Click 'Add'
                              2. Click on 'Standard VM' and 'Next'
                              3. Under the 'JRE_HOME' field, click on 'Directory'
                              4. Browse to the JAVA_HOME directory (this is the base directory, where your 'bin', 'include', and 'lib' directories reside) for your Java 8 installation
                              5. Once you click it, if it's a valid JAVA_HOME directory, all the info should fill in automatically.
                              6. Click Finish
                              7. Once you finish this, make sure this entry is checked. Click OK.
                              8. Go back and try again. GOOD LUCK!
                            • 11. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                              Nhuy Van

                              C:\Users\NhuY>javac -version

                              javac 1.8.0_05

                               

                               

                               

                              C:\Users\NhuY>java -version

                              java version "1.8.0_05"

                              Java(TM) SE Runtime Environment (build 1.8.0_05-b13)

                              Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

                               

                              here you go man, it is the latest version like I told you.

                              • 12. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                                Nhuy Van

                                Yes buddy, I had checked, and it is the latest JRE, jdk1.8.0_05, but I don't know why it does not recognize the "default" keyword at all.

                                • 13. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                                  rp0428

                                  here you go man, it is the latest version like I told you.

                                  Except that you missed one

                                  3. if that shows version 8 compile your simple interface and post the results of the compilation.

                                  You are the one dragging things out. We are trying to help you but you don't seem very interested in the process required to do that.

                                   

                                  All GUIs (Netbeans, JDeveloper, Eclipse) that are Java based use 'SOME' version of Java when they launch. When YOU write code in that GUI that code can use a totally different version of Java.

                                   

                                  You reported a syntax error on a construct that Java 8 supports. So the first order order of business is to determine EXACTLY what version of Java your code is actually using.

                                   

                                  That means you need to eliminate the GUI and perform command-line tests with a KNOWN version of Java

                                  C:\Users\NhuY>javac -version

                                  javac 1.8.0_05

                                   

                                  C:\Users\NhuY>java -version

                                  java version "1.8.0_05"

                                  That shows that from THAT particular command-line you have version 1.8.0_05 configured. So the next step is what I told you to do before.

                                  3. if that shows version 8 compile your simple interface and post the results of the compilation.

                                  That means compile your simple code:

                                  1. from the command line

                                  2. from that SAME folder you just tested 'java' and 'javac' from

                                  3. post the results

                                   

                                  If you have to drop you java classes into that same folder you show above. If that compiles correctly you then know that Eclipse is using a DIFFERENT version of Java or your project in Eclipse is using a different version of Java.

                                   

                                  There are NO SHORTCUTS when troubleshooting. You can SAY what version you are using all you want. We want to SEE what Java version you are using.

                                  • 14. Re: Eclipse does not recognize "Default Method keyword" and "Lamda expression"
                                    Nhuy Van

                                    Sorry about that buddy, like I said, I am new to this, so you guys have to guide me through this. Ok, I have compiled the source file and interface requested, here is what I got:

                                     

                                    This is my simple interface:                                                                           

                                    public interface Actions{

                                         void eat();

                                         default void run() {

                                         System.out.println(AnimalName + " can run 10 miles without stopping!!!");

                                        }

                                    }

                                     

                                    This is the source code for the class "Animal":

                                    import java.util.Scanner;

                                     

                                     

                                    public class Animal {

                                          static Scanner myScanner = new Scanner(System.in);

                                          public String AnimalName;

                                     

                                         {

                                            System.out.print("What kind of Animal?...");

                                            AnimalName = myScanner.next();

                                         }

                                     

                                         public Animal() {

                                               System.out.println("It is " + AnimalName);

                                          }

                                    }

                                    class myAnimal {

                                            public static void main(String[] args) {

                                            new Animal();

                                     

                                            Actions Human = () -> {System.out.println(new Animals() + " loves ice cream!") ; } ;

                                            Human.run();

                                            }

                                    }

                                    The compilation:

                                    C:\Users\NhuY>javac Animal.java

                                    Animal.java:23: error: cannot find symbol

                                            Actions Human = () -> {System.out.println(new Animals() + " loves ice cream!") ; } ;

                                                                                          ^

                                           symbol:   class Animals

                                           location: class myAnimal

                                          .\Actions.java:4: error: cannot find symbol

                                              System.out.println(AnimalName + " can run 10 miles without stopping!!!");

                                                            ^

                                          symbol:   variable AnimalName

                                          location: interface Actions

                                    2 errors

                                     

                                     

                                    C:\Users\NhuY>javac Actions.java

                                    Actions.java:4: error: cannot find symbol

                                         System.out.println(AnimalName + " can run 10 miles without stopping!!!");

                                                            ^

                                        symbol:   variable AnimalName

                                        location: interface Actions

                                    1 error

                                    They are in the same folder which is my home folder(C:\Users\NhuY)

                                    Thank you!!!

                                    1 2 Previous Next