15

Currently my JAVA_HOME is set to a JDK that is 1.4.2.

To run Maven 3, I would need 1.5 and above.

Is there any way to instruct my Maven installation to use another JDK that is installed other than changing the JAVA_HOME environment variable?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
  • Can't you explicitly overwrite JAVA_HOME in the script you use to start maven? You need JAVA_HOME for anything else than maven? – home Sep 03 '11 at 13:45
  • Yes, JAVA_HOME is set to 1.4.2 for my development environment, namely for BEA 8.1 server and alot of things. – Oh Chin Boon Sep 03 '11 at 13:47

2 Answers2

27

That's pretty much the way to do it. I assume you mean you don't want to change your global JAVA_HOME. That's fine. You just need to set a different JAVA_HOME for Maven. You can easily do that by creating a file at /etc/mavenrc or ~/.mavenrc (Linux) or %HOME%\mavenrc_pre.bat (Windows) with the appropriate JAVA_HOME defined in it. Maven executes those on startup if they exist unless you've defined a variable named MAVEN_SKIP_RC. You could also modify the Maven startup scripts directly, but there's no good reason for that since it gives you these nice hooks.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
1

On Windows:

STEP 1:

Find apache-maven-3.2.1\bin\mvn.bat, then find and replace JAVA_HOME to JAVA_HOME_MVN. Note: Before this, make a backup of mvn.bat

STEP 2:

Create a new environment variable called JAVA_HOME_MVN and point it to the bin of whichever jdk you want to use.

YashxRaj
  • 11
  • 1
  • 2
  • This is a good attempt. Working fine for me. My JAVA_HOME_MVN environment variable points to Jdk 11 and my regular JAVA_HOME has Jdk 8 – Utpal Kumar Jul 22 '21 at 16:44