1

I have a CentOs machine where Java 8 and Java 11 is installed. Some (command line) programs need Java 8 because they did not moved to Java 11 yet, others need Java 11.

Is there a way to switch Java version before starting a command line program?

devopsfun
  • 1,407
  • 1
    You could set your PATH variable appropriately before starting the program either manually or in a start script. – mnille Dec 18 '19 at 12:01
  • @mnille could be simpler to just the specify /full/path/to/java in a script. – xenoid Dec 18 '19 at 12:27

1 Answers1

2

If you can specify the java command to use, in many cases that will be sufficient; for example, using the OpenJDK packages,

/usr/lib/jvm/java-1.8.0/bin/java

will start the Java 8 VM,

/usr/lib/jvm/java-11/bin/java

will start the Java 11 VM.

In some cases, or if you can’t specify the java command to use, you’ll also need to set JAVA_HOME, to the directory containing bin (/usr/lib/jvm/java-1.8.0 or /usr/lib/jvm/java-11 following the examples above).

Stephen Kitt
  • 434,908
  • If one is stuck using /usr/bin/java note that the alternatives system, if employed, does not respect JAVA_HOME. See https://unix.stackexchange.com/a/419023/5132 for a lot more on this. – JdeBP Dec 18 '19 at 14:14