将 java 从版本 8 降级到 7
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23322024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Downgrade java from version 8 to 7
提问by Vaidotas
I have installed Java 1.8 from Oracle on Ubuntu because I though it would be best, newest version compatible with previous ones. But it is not.
javac
1.8 produces bytecode runnable only on the java-8-oracle, scala does not run.
我已经在 Ubuntu 上从 Oracle 安装了 Java 1.8,因为我认为它是最好的,与以前的版本兼容的最新版本。但事实并非如此。
javac
1.8 产生的字节码只能在 java-8-oracle 上运行,scala 不能运行。
Before upgrade I was using java-7-openjdk, everything was fine. While
I can choose my older virtual machine using sudo update-alternatives --config java
, but I also need to be able to choose older compiler.
How can I do this?
在升级之前我使用的是 java-7-openjdk,一切都很好。虽然我可以使用 选择较旧的虚拟机sudo update-alternatives --config java
,但我还需要能够选择较旧的编译器。我怎样才能做到这一点?
回答by Kayaman
Use the -target
flag to generate bytecode for earlier version. E.g. javac -target 1.5 FooBar.java
.
使用该-target
标志为早期版本生成字节码。例如javac -target 1.5 FooBar.java
。
There's no need to downgrade.
没必要降级。
回答by Puce
At least for Oracle's JDK (not sure about OpenJDK): install either the oracle-java7-set-default
or the oracle-java8-set-default
package, depending on which java version you want to be the default on your system.
至少对于 Oracle 的 JDK(不确定 OpenJDK):安装oracle-java7-set-default
或oracle-java8-set-default
包,具体取决于您希望将哪个 Java 版本作为系统的默认版本。
You can get it from: http://ppa.launchpad.net/webupd8team/java/ubuntu(including the actual Oracle JDKs) See: https://launchpad.net/~webupd8team/+archive/java
您可以从:http: //ppa.launchpad.net/webupd8team/java/ubuntu(包括实际的 Oracle JDK)获取:https: //launchpad.net/~webupd8team/+archive/java
Alternatively you could set the PATH and JAVA_HOME environment variables e.g. in /etc/environment
或者,您可以设置 PATH 和 JAVA_HOME 环境变量,例如在 /etc/environment
That said, when you compile you could specify the source and target level to 1.7, which would generate Java SE 7 compatible bytecode also when using JDK 8. But note it won't check if you're using some API not available in Java SE 7.
也就是说,当您编译时,您可以将源和目标级别指定为 1.7,这也会在使用 JDK 8 时生成与 Java SE 7 兼容的字节码。但请注意,它不会检查您是否使用了 Java SE 中不可用的某些 API 7.
For this reason I recommend to use always the JDK version you target rather than doing some cross-compiling (which would need some additional extra stepsto do it right).
出于这个原因,我建议始终使用您的目标 JDK 版本,而不是进行一些交叉编译(这需要一些额外的额外步骤才能正确执行)。
Note however that you can install several JDK versions on your systems. IDEs usually let you choose which one you want to use during development.
但是请注意,您可以在系统上安装多个 JDK 版本。IDE 通常允许您在开发过程中选择要使用的 IDE。
回答by Anand Kulkarni
Set up
java_home
environment variable to older version and compile Java files using it. Make sure thatjava.exe
in path variable is of earlier version.Use
-target
flag while compiling.
将
java_home
环境变量设置为旧版本并使用它编译 Java 文件。确保java.exe
路径变量是早期版本。-target
编译时使用标志。