0

When we use javac option:
-source and -target
how does the compiler "knows" the code to produce ?

I mean how does any compiler knows the specification of any previous java release or I have to do some setting in order to provide it with some information about them ?

GionJh
  • 2,742
  • 2
  • 29
  • 68
  • See related question http://stackoverflow.com/questions/21204299/compile-using-source-and-target-javac-options and http://www.codejava.net/java-core/tools/using-javac-command. – Mihai8 Sep 29 '15 at 18:56

1 Answers1

0

The javac program knows how to handle these arguments without you providing any extra information. Do note that there is a bit of a trap here, however: these options relate to the source syntax and the class file form and content, but they do not take into account the Java standard library. As a result, they will be perfectly content to compile your code that relies on standard library classes or methods that are new in (say) Java 7, despite the source or target Java version being set to something earlier. In principle the result could run on that earlier Java version, but in practice that would require the later standard library to be backported, at least in part.

If you are developing for a particular minimum version of Java, you should develop using that version. The -source and -target options options come into play if you or someone else later wants to rebuild the source using a later javac, without dropping runtime compatibility with the earlier target Java.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • sorry my ignorance but could you explain what you intended withe the following statament: In principle the result could run on that earlier Java version, but in practice that would require the later standard library to be backported, at least in part. thanks ;) – GionJh Sep 29 '15 at 19:11
  • @GionJh, basically, I meant "in practice, if your code relies on standard library features newly introduced in a given version of Java, then it will not run on any earlier version of Java, no matter how you compile it." – John Bollinger Sep 29 '15 at 19:12