Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

java - javac source and target options

I have seen the compile options like discussed in Which JDK's distributions can run `javac -source 1.6 -target 1.5`?. I understand the individual options for source and target. I don't understand why source version is higher that the target version. Compiling the code for older targets makes sense. But in that case, why dont we just use -source of the oldest target we want to be able to run on

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • source: The version that your source code requires to compile.
  • target: The oldest JRE version you want to support.

Be sure to also set bootclasspath to ensure your program will work on older VMs.


From the javac documentation:

Cross-Compilation Example

The following example uses javac to compile code that will run on a 1.6 VM.

C:>javac -source 1.6 -target 1.6 -bootclasspath C:jdk1.6.0lib
t.jar -extdirs "" OldCode.java

The -source 1.6 option specifies that version 1.6 (or 6) of the Java programming language be used to compile OldCode.java. The option -target 1.6 option ensures that the generated class files will be compatible with 1.6 VMs. Note that in most cases, the value of the -target option is the value of the -source option; in this example, you can omit the -target option.

You must specify the -bootclasspath option to specify the correct version of the bootstrap classes (the rt.jar library). If not, the compiler generates the following warning:

C:>javac -source 1.6 OldCode.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6

If you do not specify the correct version of bootstrap classes, the compiler will use the old language rules (in this example, it will use version 1.6 of the Java programming language) combined with the new bootstrap classes, which can result in class files that do not work on the older platform (in this case, Java SE 6) because reference to non-existent methods can get included.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...