-1

For some reasons I need to use JDK8 and JDK9. Is a good idea to put both paths (to JDK8 and JDK9) into the same JAVA_HOME system environment's variable?

Details: I need to run both systems at the same time, one with ant (which uses jdk8) and second with maven (which uses jdk9).

Czarcik
  • 17
  • 1
  • 3
  • 4
    explain what you are trying to do, why do you think you need both? – Darren Forsythe Mar 15 '18 at 12:54
  • obviously, you can't use both at the same time, are you asking about switching between them? – Andrew Tobilko Mar 15 '18 at 12:57
  • Yes - about switching but without big effort. I need to run both systems at the same time, one with ant (which uses jdk8) and second with maven (which uses jdk9) – Czarcik Mar 15 '18 at 13:01
  • 4
    In that case, I'd recommend to set `JAVA_HOME` to Java 8 and [use the `.mavenrc` file to launch Maven with Java 9](https://blog.codefx.org/tools/maven-on-java-9/#The-mavenrc-File). – Nicolai Parlog Mar 15 '18 at 13:06
  • Just set the `JAVA_HOME` before you run the `ANT` Task then set it again before running the `MAVEN` Task – Kenneth Clark Mar 15 '18 at 13:06
  • @Kenneth Clark - just what I thought, but then realised that it is annoying to switch paths every time I need to rebuild both projects. – Czarcik Mar 15 '18 at 13:10
  • 3
    Potential third option: use only JDK 9 and have one build produce Java 8 compatible classes. You can say "for some reasons" but if you don't explain what those reasons are, its hard to really know what the true limitations are. – Gimby Mar 15 '18 at 13:10
  • My limitation at the moment is ant. I tried to run it with jdk9 with no success. – Czarcik Mar 15 '18 at 13:20
  • I think @Nicolai's answer is what you want. Your ant build shouldn't fail *because of* jdk9, it seems more likely that there are issues with your code or build configuration which is causing the build to fail. – wesrobin Mar 15 '18 at 13:40
  • @Czarcik seems like valuable information to add to the question. – Gimby Mar 15 '18 at 14:20
  • Maybe you should ask a question about your trouble running Ant. I have done a full Ant build with Java 9 literally hundreds of times. – VGR Mar 15 '18 at 14:20

2 Answers2

1

Usually that path is reserved for the current active java command keyword in the command line interface. You can't have multiple JDK active at the same time at any moment when using the terminal. So it is not a good idea.

You can however point the JAVA_HOME to the folder where you have multiple JDK installations and then set the PATH variable to a certain JDK. So when you want to change the JDK you change only the PATH variable and leave JAVA_HOME as it is.

If you intend to use different JDK across multiple projects in an IDE, then yes you can have multiple JDK and you can chose the JDK you want to use in the Project Settings.

Alin Gabriel Arhip
  • 2,568
  • 1
  • 14
  • 24
0

If I were you then I would create function() in .profile or .bashrc for command prompt or terminal which will export JAVA_HOME variable to Java8 or Java9 depending on whether I am running ant or mvn respectively.

Lets say your Java8 and Java9 are installed at below locations ...

  • C:\Program Files\Java\jdk1.8.0_151\bin
  • C:\Program Files\Java\jdk1.9.0_4\bin

Then your functions in .profile or .bashrc should be like this ...

For ant and Java8 (here i am passing command line argument $1 to ant command)...

runant() {
  export JAVA_HOME="C:\Program Files\Java\jdk1.8.0_151\bin";
  ant $1;
}

For mvn and Java9 ...

runmvn() {
  export JAVA_HOME="C:\Program Files\Java\jdk1.9.0_4\bin";
  mvn clean install;
}

With the above functions, you can run ant and mvn from command prompt and JAVA_HOME will be set appropriately ONLY for that specific run.

JRG
  • 4,037
  • 3
  • 23
  • 34