0

I used it before on JDK 1.7. It was OK. After move it to new laptop with JDK 1.8. I got Syntax error from "ArrayList array = new ArrayList()" line. Someone know what happen it in here.

Syntax error, annotations are only available if source level is 1.5 or greater FileWriter.java

    public static String[] fileToArray(String filename) throws IOException
        {
            FileReader file = new FileReader();
  error >>  ArrayList<String> array = new ArrayList<String>();
            String line;

            file.open(filename);

            while((line = file.readLine()) != null)
            {
                if (line.length() > 0)
                    array.add(line);
            }

            return (String[])array.toArray(new String[array.size()]);
        }
    }
4
  • How are you compiling you code? Is it in Eclipse? Commented Aug 15, 2014 at 15:10
  • ArrayList<String> array = new ArrayList<>(); Commented Aug 15, 2014 at 15:13
  • @user3076246 No, that's not the problem, the code is fine. Commented Aug 15, 2014 at 15:14
  • 1
    Somehow the IDE assumes the source code is below 1.5. My guess: you are using eclipse. Commented Aug 15, 2014 at 15:17

2 Answers 2

4

Looks like you work with Eclipse IDE and the version you use doesn't support Java 8. If so, the problem is not in the code itself, but in the IDE. When you use JDK 8 as default, Eclipse won't recognize it and it will set the source code level to 1.4 by default, despite if your code compiles for another version of Java. It is a nasty problem when dealing with Eclipse and JDK 8.

If you want to use Java 8, then move to Eclipse Luna or use Eclipse Kepler + Java 8 support for Eclipse Kepler SR2. If you don't then just install JDK 7 and configure it for your Eclipse IDE.

More info:

Sign up to request clarification or add additional context in comments.

4 Comments

I don't see any syntax here that's specific to Java 8...or 7, for that matter.
@Makoto answer edited to show what's the exact problem.
I think previous Kepler version was OK. I updated newer version as Luna and I setup jdk 8. do you know where I can change the source code level?
@duly right click in your project, Properties, Java Compiler, JDK Compliance.
0

Probably you are using Eclipse. Set the Source Code Level to 1.7.

Check what is under Preferences > Java > Compiler > JDK Compliance. You should be able to set to 1.7.

7 Comments

Java 8 isn't supported by default in Eclipse Kepler or prior.
Thank you for your answer. Could kindly let me know where I can update the setting?
If you go to that option, you may see that there's no Java 8 support unless you're working with Eclipse Luna. And if you post the source code level to Java 6 or 7, Eclipse will complain that you don't have that version of Java installed/configured.
@LuiggiMendoza OP's code contains no JDK8 specific syntax, so any JDK5+ compiler should be able to compile his code. Of course, he cannot use lambda, etc. that is JDK8 specific.
Thank you! The default value was 1.6. So, I changed 1.7 then the error is gone. The eclipse may not support 1.8 yet.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.