Downgrade java from version 8 to 7

2019-06-23 23:55发布

问题:

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.

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?

回答1:

Use the -target flag to generate bytecode for earlier version. E.g. javac -target 1.5 FooBar.java.

There's no need to downgrade.



回答2:

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.

You can get it from: http://ppa.launchpad.net/webupd8team/java/ubuntu (including the actual Oracle JDKs) See: https://launchpad.net/~webupd8team/+archive/java

Alternatively you could set the PATH and JAVA_HOME environment variables e.g. in /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.

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 steps to do it right).

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.



回答3:

  1. Set up java_home environment variable to older version and compile Java files using it. Make sure that java.exe in path variable is of earlier version.

  2. Use -target flag while compiling.