为什么 javac “-source"标志不起作用? [英] Why javac "-source" flag doesn't work?

查看:36
本文介绍了为什么 javac “-source"标志不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试 javac -source 标志,但我对它应该如何工作感到有些困惑.

I am testing the javac -source flag and I am a bit confuse about how it is supposed to work.

请参阅此代码作为示例.这是一个不兼容的 Java5 代码,因为该版本的 JDK 中没有为 String 定义方法 isEmpty().

See this code as an example. It is a non-compatible Java5 code as the method isEmpty() is not defined for String in that version of the JDK.

public class TestJavac {
   public static void Main(String args[]) {
   String pippo = "pippo";   
   pippo.isEmpty();
   }
}

尝试编译使用:

javac -source 5 TestJavac.java

然后就可以了!这对我来说听起来很奇怪,但也许我忽略了一些东西.我的 JAVA_HOME 指向一个 1.6 JDK.

Then it works! This sounds weird to me, but maybe there is something that I am ignoring. My JAVA_HOME point to a 1.6 JDK.

推荐答案

source 选项仅针对语言级别,不针对 API(由 JDK 运行时库提供).

The source option is only for the language level, not for the API (supplied by the JDK runtime libraries).

有一个单独的选项 (-bootclasspath -extdirs) 来指定要使用的 JDK(并且您需要获取适当的 jar 文件).

There is a separate option (-bootclasspath -extdirs) to specify which JDK to use (and you need to get the appropriate jar files).

请注意,还有 -target(控制输出字节码版本).

Note that there is also -target (which controls the output bytecode version).

您应该始终指定所有(甚至使用旧版 JDK 进行编译,无论如何您或多或少都需要它来使第二个开关工作)

You should always specify all (or even compile using the older JDK, which you more or less need anyway for that second switch to work)

这篇关于为什么 javac “-source"标志不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆