1

I have jre7 installed on my pc. In Eclipse Kepler I created a project with compiler compliance level 1.5. The source compatibility setting is 1.5 too. But this setting not works.

I can wirte code which using methods that were declared in 1.6 or later. For example:

String s = "aaa";
boolean b = s.isEmpty();

The method isEmpty() in java.lang.String is since 1.6.

Of course, when running the generated class file with a real jre 1.5 enviroment, i got a NoSuchMethodError exception.

Here is my source and settings: source and settings

How can I fix this? Or is it an Eclipse bug?

zsom
  • 479
  • 1
  • 5
  • 19
  • Did you tried to clean and build your project ? *Project* -> *Clean*, *Project* -> *Build* – ortis Sep 12 '14 at 07:47
  • Of course, but nothing changed – zsom Sep 12 '14 at 07:50
  • 1
    The compliance level only sets the language feature that are accepted. It does not attempt to check for new methods in libraries. If you want you do that you have to use the correct level of JDK - you can tell Eclipse about several JDKs for different projects. – greg-449 Sep 12 '14 at 07:52
  • But what about the source compatibility setting? If i choose 1.4, than i can use variables called enum. But s.isEmpty() is still not a compile error. – zsom Sep 12 '14 at 08:00

2 Answers2

1

The settings are releated to the -source and -target flags for the javac compiler which enables backwards compatibility (for an explanation, see for example this SO question and answer).

This is mostly a technical option and not a feature, i.e. these settings in Eclipse were never intended to free you of the burden of knowing what is and is not available in an old JRE. But Eclipse does provide you the option to make it so and even shows you a warning message at the bottom of the Java Compiler options screen on how to make it so (and yes, that does involve downloading and installing the old (target) version of the JRE - there is no way around that).

Community
  • 1
  • 1
vanOekel
  • 6,358
  • 1
  • 21
  • 56
0

Whether or not a method from Java 6 is available depends on the JRE library that you are using.

Go to "Java Build Path", into the "Libraries" tab, remove the "JRE System Library" of Java 7. Then, click "Add library..." and go though the dialogs for adding a Java 5 JRE Library. (Of course, a Java 5 JRE has to be installed for that...).

(Note: There is also a message about this in the right screenshot, at the bottom...)

Marco13
  • 53,703
  • 9
  • 80
  • 159
  • I know, that using the appropriate jre is the correct solution. But 1 thought that the "source compatibility" setting will solve this issue – zsom Sep 12 '14 at 08:19
  • @zsom One could probably imagine the JRE library as "some arbitrary library": If a method is there, it can be called. Regardless of whether there is an older (unknown) version of the library where this method was *not* present. – Marco13 Sep 12 '14 at 08:22
  • I accept this. But what does "source compatibility" means? – zsom Sep 12 '14 at 08:30
  • This mainly refers to the *language features*. With compatibility 1.4, you will not be able to use generics. With 1.5 or 1.6, you will not be able to use binary literals. With 1.7, you will not be able to use lambda expressions. See http://docs.oracle.com/javase/8/docs/technotes/guides/language/enhancements.html for a list of features for each version. – Marco13 Sep 12 '14 at 08:49