简体   繁体   中英

javac source and target options usage

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM