Git Pre-Commit Hook doesn't know my JAVA_HOME
I recently added a Bash script as pre-commit hook that ultimately runs a Maven command. Hence, it required JAVA_HOME to be set.
When I commit in a terminal (even the Idea terminal), this works just fine. JAVA_HOME is set.
However, when I try to commit via Idea's commit dialog, I get the error message
The JAVA_HOME environment variable is not defined correctly,
this environment variable is needed to run this program.
So I added a printenv to the script and indeed, the JAVA_HOME variable is not listed.
#!/bin/bash
set -e
printenv
"/home/mkutz/.m2/wrapper/dists/apache-maven-3.8.3-bin/5a6n1u8or3307vo2u2jgmkhm0t/apache-maven-3.8.3/bin/mvn" -f "/home/mkutz/Projekte/ffp-spring-boot-starter/pom.xml" com.cosium.code:git-code-format-maven-plugin:on-pre-commit
The variable is usually set via SDKMan!, but even adding it directly to my ~/.bashrc did not change anything.
The script is overwritten by Maven, so I cannot just add setting JAVA_HOME in it.
How can I make sure that the pre-commit hooks have the same env as my usual shell?
Please sign in to leave a comment.
Hello mkutz
You may have such an error message since an IDE had no access to this environment variable at the moment of its start. IDE inherits environment variables from the parent process that starts it, and if there was no JAVA_HOME set, then IDE won't be able to use while running.
>but even adding it directly to my ~/.bashrc did not change anything.
It won't take effect until you restart an IDE so it could inherit all environment from a parent process. So if you make any changes to environment you need to restart the IDE (if IDE has been started from Toolbox, then first you restart toolbox and then open IDE)
Opened an issue for this: https://youtrack.jetbrains.com/issue/IDEA-287495
Hey Ruslan Kuleshov,
thanks for the reply. Seems you are right. I usually start Idea via the system launcher (Ubuntu/Gnome in my case). When I start it from the command line, the pre-commit-hook works just fine and also the mechanisms of SDKMan! work there.
Hello,
This page is always comes whenever i google this issue on a fresh linux installs. Here's my working solution.
I'm having this problem too, after a fresh Linux mint 21.3 install using sdkman to setup jdk and maven and installing idea from toolbox app. on my OS terminal and idea's terminal window everything works fine, but using git tool window to commit returns problems(log says about not finding JAVA_HOME) on my projects. After finding this page https://ntsim.uk/posts/running-git-hooks-with-environment-variables-in-intellij and testing it, my issues are resolved. In summary i did
add this in the ~/.profile file:
after that i rebooted and checked if it worked. And it did!
Have a nice day!