Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA_HOME not found as Sudo

I have a bash script on a Linux box that runs a Jar file. When logged in as a regular user I don't have permission to run the script, but it prints the following log:

*INFO * Using JVM found at /opt/jdk6/bin/java

When I try to use the script with Sudo though, it gives:

*ERROR* Unable to locate java, please make sure java is installed and JAVA_HOME set

I've set JAVA_HOME to the same path above — can see it with echo $JAVA_HOME & it's also set as an option within the script. I'm happy that the script isn't the issue — it's a default CQ5 control script & I'm using it on dozens of other boxes without issue. Just unsure what I'm doing wrong above & presume it's something I'm missing re Linux set-up?

When I run the sudo command, does it have access to the JAVA_HOME that I set up as myself?

like image 782
anotherdave Avatar asked Jul 13 '12 11:07

anotherdave


People also ask

Why JAVA_HOME is not working?

All you need to do to fix this error is edit the JAVA_HOME variable and point it to the correct directory. The JAVA_HOME environment variable must point to the root of the installation folder of a JDK. It cannot point to a sub-directory of the JDK, and it cannot point to a parent directory that contains the JDK.

Where is JAVA_HOME in Ubuntu?

From the java installation that you use select the path up to jre . e.g. /usr/lib/jvm/java-8-oracle/ . This will become your JAVA_HOME path.

Do I need to set JAVA_HOME on Linux?

If you are running Java programs on Ubuntu using Eclipse, Maven or Netbeans etc, you'll need to set JAVA_HOME to your path. Otherwise, your system will complain that “java_home environment variable is not set”.


2 Answers

By default, sudo will cleanup the environment of the spawned commands. Pass -E to keep it:

sudo -E env

Compare to:

sudo env
like image 89
fork0 Avatar answered Sep 28 '22 23:09

fork0


"sudo -E " didn't solve the problem when JAVA_HOME was not exported. And when it was exported, "sudo " without -E works the same.

So you can add export JAVA_HOME=.../jdk<version> in your .bash_profile and .bashrc file.

In case you wondered what's the difference of .bash_profile and .bashrc, .bash_profile is executed upon login (e.g., show some diagnostic/welcome information). .bash_rc is executed when you open a new terminal (e.g., shift-ctrl-T).

In order to run some commands for both cases, you can put it in .bashrc file, and let .bash_profile source .bashrc:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi
like image 41
Danke Xie Avatar answered Sep 29 '22 00:09

Danke Xie