How to set specific Java version to Maven?
Asked Answered
G

29

316

On my machine I have two Java versions installed: (1.6 and 1.7 installed manually by me). I need both of them for different projects. But for Maven I need 1.7, but my Maven uses the 1.6 Java version.

How can I set Maven to use 1.7?

Guadalupeguadeloupe answered 29/10, 2013 at 9:41 Comment(5)
I cannot export JAVA_HOME with java 1.7 because I cannot touch configuraton of my machine, the default version of Java must be 1.6.Guadalupeguadeloupe
Maven uses JAVA_HOME so dont think what you want to do is possible without changing JAVA_HOME value.Ingar
Are you using an IDE or the command line? Are you on a *nix or windows?Huntsman
Does this answer cover what you want? https://mcmap.net/q/101132/-configure-maven-to-use-different-jdk-for-different-j2se-versionsBelong
Yes is useful but I cannot make chnges to pom of project, nevertheless the stuff is the same and answer is correctGuadalupeguadeloupe
W
252

Maven uses the JAVA_HOME parameter to find which Java version it is supposed to run. I see from your comment that you can't change that in the configuration.

  • You can set the JAVA_HOME parameter just before you start maven (and change it back afterwards if need be).
  • You could also go into your mvn(non-windows)/mvn.bat/mvn.cmd(windows) and set your java version explicitly there.
Whiffletree answered 29/10, 2013 at 9:47 Comment(4)
@andPat: so? You can set JAVA_HOME in your local shell without changing the configuration of your machine at all (as soon as you close that shell, the change is gone). You can even change it for a single command: JAVA_HOME=/path/to/jdk17/ mvn buildBigot
@JoachimSauer: Yes, but I had also made a reference to mvn.bat, which is Windows specific.Whiffletree
On mac you can create a .mavenrc file with the JAVA_HOME insideKohinoor
Old, but throwing in my 2cents. I am working on a Java 8 service that is called by a Java 7 application. To avoid having to switch constantly when building, I (on windows) created a MVN_JAVA_HOME environment variable, and then replace every occurrence of JAVA_HOME with MVN_JAVA_HOME inside mvn and mvn.cmd.Cherub
B
133

Edit:

A better solution is presented by the answer from Ondrej, which obviates remembering aliases.


Original Answer:

Adding a solution for people with multiple Java versions installed

We have a large codebase, most of which is in Java. The majority of what I work on is written in either Java 1.7 or 1.8. Since JAVA_HOME is static, I created aliases in my .bashrc for running Maven with different values:

alias mvn5="JAVA_HOME=/usr/local/java5 && mvn"
alias mvn6="JAVA_HOME=/usr/local/java6 && mvn"
alias mvn7="JAVA_HOME=/usr/local/java7 && mvn"
alias mvn8="JAVA_HOME=/usr/local/java8 && mvn"

This lets me run Maven from the command line on my development machine regardless of the JDK version used on the project.

Bridie answered 20/4, 2017 at 16:9 Comment(7)
Unless you need maven 2 and 3 for various projects, in which case you already have mvn2 and mvn3 defined and the naming gets a bit funky when Java versions also come into play... :DSalvation
If you're using a Mac, your JDK comes with java_home command so you can avoid hardcoding paths, e.g. alias mvn8 = "JAVA_HOME=`/usr/libexec/java_home -v 1.8` && mvnAfterdeck
On (ubuntu + bash), you have to omit the && before mvn. mvn does not see JAVA_HOME's value with &&.Cupellation
@Cupellation good to know; I use ZSH and didn't know about the Bash differencesBridie
@acabra85 see this answer from Ondrej that is even better: https://mcmap.net/q/99007/-how-to-set-specific-java-version-to-mavenBridie
@Cupellation with && that just worked fine for me now on Ubuntu 20 and default bashCoarsen
I don't think toolchains answer is always better. It requires fiddling with project pom which you might not want to do if you have a lot of and/or complex maven projects. This achieves the same without the need to touch poms.Gabbey
W
100

In the POM, you can set the compiler properties, e.g. for 1.8:

<project>
...
 <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
...
</project>
Wanda answered 30/3, 2017 at 15:38 Comment(5)
As of compiler-plugin version 3.7.0, the default values for source and target if left unspecified is 1.5Storer
Can you use version 9, 10 11 instead of 1.X for those versions ?Woken
Somehow, with Java 15 and Maven 3.6.0, I get a package javax.xml.bind.annotation does not exist error when I try to compile a Java 8 project, even after adding the properties you mentioned. If I set JAVA_HOME to the java8 folder, mvn works fine again.Authenticity
Your code won't work id JAVA_HOME=<path to JDK 1.6>. See the answer from @ondrej-burkert below about Toolchains plugin which helps in such cases.Installment
It does not work: Could not find artifact com.sun:tools:jar:1.6.0Arvizu
A
92

I just recently, after seven long years with Maven, learned about toolchains.xml. Maven has it even documented and supports it from 2.0.9 - toolchains documentation

So I added a toolchains.xml file to my ~/.m2/ folder with following content:

<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
 <!-- JDK toolchains -->
 <toolchain>
   <type>jdk</type>
   <provides>
     <version>1.8</version>
     <vendor>sun</vendor>
   </provides>
   <configuration>
     <jdkHome>/opt/java8</jdkHome>
   </configuration>
 </toolchain>
 <toolchain>
   <type>jdk</type>
   <provides>
     <version>1.7</version>
     <vendor>sun</vendor>
   </provides>
   <configuration>
     <jdkHome>/opt/java7</jdkHome>
   </configuration>
 </toolchain>
</toolchains>

It allows you to define what different JDKs Maven can use to build the project irrespective of the JDK Maven runs with. Sort of like when you define JDK on project level in IDE.

Amatol answered 17/2, 2016 at 17:27 Comment(7)
The best answer, ! Thanks. This explains a clear situation where we want to run Maven and the project under compilation with two different java runtimes/framework.Winger
This is the best solution on the page, and should have been the accepted answer.Bridie
How this config works in pom.xml in each project? Is define '<maven.compiler.source>1.8</maven.compiler.source>' in pom.xml means using /opt/java8?Cyclohexane
This is the best answer and should have been the accepted answer. Toolchains are designed for this very scenario.Carlicarlick
Chiming in - best answer! And, thank you. I, too, have been using maven for over a decade now and never knew about toolchains! CheersJeff
Okay, it's good answer, but how we can make this plugin is unnecessary indepenth of machine on which u want build your program?Excretion
I followed the steps to activate the toolchain and I can see the effect showing in the output of mvn clean install, but when I unzip the resulting JAR and check the file MANIFEST.MF I always see the JDK defined by the system PATH and JAVA_HOME.Kristofor
K
74

On windows

If you do not want to change your JAVA_HOME variable inside the system variables.

Edit your mvn.bat file and add a line like this

set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45\jre

This can be done after @REM ==== START VALIDATION ==== like mentionned by @Jonathan

On Mac (& Linux ?)

If you do not want to change your JAVA_HOME variable inside your ~/.bashrc or ~/.bash_profile

you can create a ~/.mavenrc file and redefine your JAVA_HOME using the java_home tool

export JAVA_HOME=`/usr/libexec/java_home -v 1.7.0_45`

Sanity Check

You can verify that everything is working fine by executing the following commands. The jdk version should be different.

mvn -version

then

java -version

Kohinoor answered 30/7, 2015 at 8:11 Comment(4)
I set it to set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45 exluded jre subfolder.Hooded
Yes, @Hooded This works only if JAVA_HOME is set to JDK directory, not JRE with \jre.Carmon
If you can't find ^the mvn.bat^, this might help you: #32706500Silicle
This should be the accepted answer.. Just add the file ~/.mavenrc and export JAVA_HOME there pointing it to the directory where the version you want or need to use is located.. And voilá..Pretoria
P
37

Adding my two cents and explicitly providing the solution.

I have two JDKs installed on my Windows Machine - JDK 1.5 and JDK 1.6.

My default (and set to windows system environment variable) JAVA_HOME is set to JDK 1.5.

However, I have a maven project that I need to build (i.e., JBehave Tutorial's Etsy.com) using JDK 1.6.

My solution in this scenario (which worked!), is as suggested by @DanielBarbarian to set it in mvn.bat.

For some not familiar with window's batch file, I just basically added the set JAVA_HOME=<path_to_other_jdk> line after @REM ==== START VALIDATION ==== in mvn.bat (i.e., %MAVEN_HOME%\bin\mvn.bat):

@REM ==== START VALIDATION ====
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_45\jre
if not "%JAVA_HOME%" == "" goto OkJHome
Paraphrastic answered 8/5, 2014 at 2:49 Comment(1)
In maven 3.5, mvn.bat is renamed to mvn.cmd.Lablab
D
12

You can set Maven to use any java version following the instructions below.

Install jenv in your machine link

Check the available java versions installed in your machine by issuing the following command in command line.

jenv versions

You can specify global Java version using the following command.

jenv global oracle64-1.6.0.39

You can specify local Java version for any directory(Project) using the following command in the directory in command line.

jenv local oracle64-1.7.0.11

add the correct java version in your pom.xml

if you are running maven in command line install jenv maven plugin using below command

jenv enable-plugin maven

Now you can configure any java version in your machine to any project with out any trouble.

Donata answered 7/6, 2019 at 2:45 Comment(2)
like this option. jenv is like virtualenv for pythonGomuti
Only this option worked for me. My system had jenv installed and though JAVA_HOME had updated version but not jenv. Maven was taking java version from jenvQueston
A
11

One simple solution to the problem -

JAVA_HOME=/usr/lib/jvm/java-6-sun/ mvn clean install

On Mac, it would look something like -

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home/ mvn clean install

PS: One special case that i found is the above given command does not work on 'fish' shell. I also had bash shell available and it worked fine there. just use command 'bash' to switch to bash shell.

Acquire answered 3/3, 2017 at 12:23 Comment(0)
L
10

You could configure compiling sources using different JDK with maven-compiler-plugin.

Just specify path to javac in <executable> tag. E.g for java11 it looks like:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <source>11</source>
        <target>11</target>
        <fork>true</fork>
        <executable>C:\Program Files\Java\jdk-11.0.1\bin\javac</executable> <!--PATH TO JAVAC -->
    </configuration>
</plugin>
Lat answered 30/3, 2019 at 21:57 Comment(0)
P
10

On Macs, (assuming you have the right version installed)

JAVA_HOME=`/usr/libexec/java_home -v 1.8` mvn clean install -DskipTests

Plumbaginaceous answered 20/8, 2019 at 16:17 Comment(0)
I
10

I am using Mac and none of the answers above helped me. I found out that maven loads its own JAVA_HOME from the path specified in: ~/.mavenrc

I changed the content of the file to be:

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

For Linux it will look something like:

export JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre
Ionosphere answered 6/10, 2021 at 10:40 Comment(0)
O
6

On windows, I just add multiple batch files for different JDK versions to the Maven bin folder like this:

mvn11.cmd

@echo off
setlocal
set "JAVA_HOME=path\to\jdk11"
set "path=%JAVA_HOME%;%path%"
mvn %*

then you can use mvn11 to run Maven in the specified JDK.

Oilstone answered 4/7, 2019 at 6:56 Comment(2)
this is cleaner cz we know what version we run!Halvaard
For setting path, you should use bin subfolder: set "path=%JAVA_HOME%\bin;%path%"Ivelisseivens
R
5

Without changing Environment Variables, You can manage java version based on the project level by using Maven Compiler Plugin.

Method 1

<properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Method 2

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
Running answered 22/10, 2019 at 8:48 Comment(0)
S
4

To avoid any impact to your project and to your Environment Variables, you can configure the Maven Compiler Plugin just to the project's POM, specifying the Source and Target java version

  <plugins>
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
    </plugin>
    ...
  </plugins>
Superconductivity answered 2/2, 2017 at 9:25 Comment(0)
P
4

Recently installed Java10 to see whats new and fun about it. Once I did this and tried running existing projects which use java8. To my surprise, maven began using java10 as its default java version, even though my JAVA_HOME is set to use java8 — /usr/libexec/java_home -v 1.8

Given, my installation was done using brew — Brew is simply a package manager for Mac OS, my M2_HOME was automatically set up. This is usually in /usr/local/Cellar/maven/3.5.4

With this new found knowledge, I run

nano /usr/local/Cellar/maven/3.5.4/bin/mvn

The content was

#!/bin/bash
JAVA_HOME="${JAVA_HOME:-$(/usr/libexec/java_home)}" exec "/usr/local/Cellar/maven/3.5.4/libexec/bin/mvn" "$@"

Important bit being

"${JAVA_HOME:-$(/usr/libexec/java_home)}"

To resolve this, you will need to specify the java version you need maven to default to. So in my case, I needed java8.

Update /usr/local/Cellar/maven/3.5.4/bin/mvn file as follows

#!/bin/bash
JAVA_HOME="${JAVA_HOME:-$(/usr/libexec/java_home -v 1.8)}" exec "/usr/local/Cellar/maven/3.5.4/libexec/bin/mvn" "$@"
Pyrrhonism answered 9/3, 2021 at 3:25 Comment(1)
Good approach checking the executable logic, lucky that it's a bash script. In my case it checks JAVA_HOME, if absent will be satisfied with which java. which java points me to /usr/bin/java which is /etc/alternatives/java and will update when new version is installed.Beare
P
4

I am using MAC Mini - M1 chip, after installing maven through brew install maven I noticed it's using java 19 but I rather wanted to use java 17 version that I downloaded in to my machine. Was out of luck with specific instructions to reset the default mvn java version. Here are the steps you can follow.

  1. mvn --version #this will give where the maven is installed in my case Maven home: /opt/homebrew/Cellar/maven/3.8.7/libexec
  2. then go to /opt/homebrew/Cellar/maven/3.8.7/bin folder
  3. edit mvn file, I used vi mvn
  4. below I have edited the jdk location to point where I have java 17 jdk
#!/bin/bash
JAVA_HOME="${JAVA_HOME:-/Library/Java/JavaVirtualMachines/jdk-17.0.3.1.jdk/Contents/Home}" exec "/opt/homebrew/Cellar/maven/3.8.7/libexec/bin/mvn"  "$@"
  1. save the mvn file after edit
  2. mvn -v will now show it's default java version is 17
Apache Maven 3.8.7 (b89d5959fcde851dcb1c8946a785a163f14e1e29)
Maven home: /opt/homebrew/Cellar/maven/3.8.7/libexec
Java version: 17.0.3.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-17.0.3.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "13.1", arch: "aarch64", family: "mac"
Personally answered 28/1, 2023 at 23:26 Comment(0)
D
3

Ondrej's answer worked perfectly. It truly solves the problem. Few other things to do though as detailed in the toolchain documentation

  1. Add the maven-toolchains-plugin in your project POM
  2. Configure the maven-toolchains-plugin to use a specific JDK in the toolchain as configured in your toolchains.xml file.

See sample configurations below:

toolchains.xml

<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
  <!-- JDK toolchains -->
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>1.8</version>
      <vendor>sun</vendor>
    </provides>
    <configuration>
      <jdkHome>/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>17</version>
      <vendor>sun</vendor>
    </provides>
    <configuration>
      <jdkHome>/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home</jdkHome>
    </configuration>
  </toolchain>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>11</version>
      <vendor>sun</vendor>
    </provides>
    <configuration>
      <jdkHome>/Library/Java/JavaVirtualMachines/jdk-11.0.12.jdk/Contents/Home</jdkHome>
    </configuration>
  </toolchain>
</toolchains>

As seen above, this works for machines having multiple java versions installed. Then in the project's POM, the maven-toolchains-plugin is configured to use one of the JDK versions defined as a toolchain.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-toolchains-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>toolchain</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <toolchains>
      <jdk>
        <version>11</version>
        <vendor>sun</vendor>
      </jdk>
    </toolchains>
  </configuration>
</plugin>
Dumpcart answered 7/4, 2022 at 7:43 Comment(0)
T
2

I did not have success on mac with just setting JAVA_HOME in the console but I was successful with this approach

  • Create .mavenrc file at your at your home directory (so the file will path will be ~/.mavenrc
  • Into that file paste export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
Taal answered 31/1, 2019 at 14:20 Comment(0)
D
2
  1. on terminal vi ~/.bash_profile
  2. After that, paste the below path in the base profile file and save it by vim command wq!
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home
export M2_HOME=/Users/mj/Tools/apache-maven-3.8.1
  1. run source ~/.bash_profile to make it work forever

  2. after that, you can run mvn -v in your terminal to check

Maven home: /Users/mj/Tools/apache-maven-3.8.1
Java version: 1.8.0_211, vendor: Oracle Corporation, 
runtime: /Users/mj/Tools/jdk1.8.0_211.jdk/Contents/Home/jre
Default locale: en_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"

if you want to know why, read below

JAVA_HOME is used by many Java-based applications to define the place of Java Runtime Environment (JRE) installation. M2_HOME is used by Maven, and again it tells the program where to find Maven installation.

Disperse answered 27/5, 2021 at 3:21 Comment(2)
Well this is true and false. You can set it and it may work, but actually it can be null and you still can use Maven, it just picks up the latest version you installed, at least in my Ubuntu it's true. (echo $JAVA_HOME and it's empty), just as @new coder said.Beare
works flawlessly!Cottbus
T
2

You just need to add your JAVA version to mvn file before the call of JAVA_HOME for example my JAVA Version in .bashrc file is Java-17 and i want to run maven with Java-11 :

in file /usr/share/maven/bin/mvn : I add :

JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

before the call of JAVA_HOME in the script

and then it works fine with Java-11 :)

Template answered 8/1, 2022 at 20:13 Comment(0)
M
1

On Linux/Unix, the JAVA_HOME within 'mvn' shell script is overridden by the settings in

$HOME/.mavenrc

please Where to add JAVA_HOME and MAVEN path variables in linux

Mcallister answered 21/11, 2018 at 13:48 Comment(0)
M
1
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45\jre

Adding the above line of code as the first statement in $MAVEN_HOME\bin\mvn.cmd worked for me straight away in Windows 10.

Magnesia answered 17/1, 2022 at 17:13 Comment(0)
M
1

I couldn't solve this problem with the answers above and I tried many things to solve this and I got it. I hope someone like me could solve the problem.

I am using brew to install java and maven. For me, I wanted to use java 1.8. So I installed java 1.8 but still maven use version 17.0.2

Solution:
install java 1.8 I referred to this website to install it: https://devqa.io/brew-install-java/

mvn --version and remember the version \

cat /usr/local/Cellar/maven/VERSION(for me 3.8.5)/bin/mvn

Then you will see

#!/bin/bash
JAVA_HOME="${JAVA_HOME:-/usr/local/opt/openjdk/libexec/openjdk.jdk/Contents/Home}" exec "/usr/local/Cellar/maven/3.8.5/libexec/bin/mvn"  "$@"\

Check this directory

ls /usr/local/opt

You will see files like openjdk, openjdk@17, openjdk@8

change the directory openjdk to openjdk@8 in mvn file:

#!/bin/bash
JAVA_HOME="${JAVA_HOME:-/usr/local/opt/openjdk@8/libexec/openjdk.jdk/Contents/Home}" exec "/usr/local/Cellar/maven/3.8.5/libexec/bin/mvn"  "$@"\
Monochloride answered 30/3, 2022 at 3:4 Comment(0)
S
1

Mac Users, If you have installed maven from HomeBrew(Brew), Or even if you have installed it from elsewhere, If you want to change the default version of the Java used by the Maven, You can use following commands to change the version that the maven uses,

vim ~/.mavenrc

In the gui that pops up enter this and save-

JAVA_HOME="(put the Home path of your desired java version package)"

To verify wether the path is changed for maven you can check using -

mvn -v
Suzette answered 24/8, 2022 at 7:14 Comment(0)
J
0

Also you can have two versions of maven installed, and edit one of them, editing here:

mvn(non-windows)/mvn.bat/mvn.cmd(windows)

replacing your %java_home% appearances to your java desired path. Then just execute maven from that modified path

Jahnke answered 13/12, 2018 at 10:33 Comment(0)
P
0

I've used the base idea from @Jonathan. I've set the windows with: set JAVA_HOME=C:\Program Files\java\AdoptOpenJDK-11.0.8+10 call mvn clean package -DskipTests

Punk answered 9/10, 2020 at 0:1 Comment(0)
A
0

I am using jenv and was facing the compilation error for "javax.xml.bind.annotation" as the mvn in terminal was using openjdk 13

Maven home: /usr/local/Cellar/maven/3.6.3_1/libexec
Java version: 13.0.2, vendor: N/A, runtime: /usr/local/Cellar/openjdk/13.0.2+8_2/libexec/openjdk.jdk/Contents/Home

For me, it worked after running 'jenv enable-plugin maven' as suggested here https://mcmap.net/q/99007/-how-to-set-specific-java-version-to-maven

Accommodating answered 12/4, 2021 at 17:52 Comment(0)
M
0

Setting the Default Java Version You might have 2 different versions of Java on your system. To set one as the default , use the command:

sudo alternatives ––config java

The system displays a list of different Java versions. If you like the default, press Enter.

If you want to change it, type the number of the version you want, then press Enter.

Magnificent answered 29/9, 2022 at 11:11 Comment(0)
V
0

I used https://mcmap.net/q/99007/-how-to-set-specific-java-version-to-maven and created the below functions for use with Powershell:

function J08 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk1.8.0_161'}
function J09 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-9'}
function J11 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-11.0.10'}
function J15 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-15.0.2'}
function J16 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-16.0.1'}
function J17 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-17'}
function J18 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-18.0.2'}
function J19 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-19'}

These functions must be placed in file C:\Program Files\PowerShell\7\profile.ps1 (or any other PowerShell profile) and be called before use. for example, if you want to use maven with Java 8, you must first call J08, then call mvn ....

I found the toolchain answer so complicated and preferred this way.

Velate answered 9/4, 2023 at 18:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.