3

Using openSUSE, I downloaded the Oracle rpms for jdk1.6.0_24 and I want to set the java home environment variable to /usr/java/jdk1.6.0_24 but the /etc/alternatives system is unable to automatically detect this installed JDK. Update-alternatives , or whatever just doesn't find the jdk.

So, I want to detect the JAVA home manually in a BASH script.

If I run this command: sudo find /usr -name 'jdk1.6*' , I get this result:

/usr/java/jdk1.6.0_24

How do pipe that result into a environment variable? I want to do something like

#!/bin/bash
read in JAVA_HOME var from a file
if file doesnt exist
  sudo find /usr -name 'jdk1.6*' 
  prompt user for which jdk is correct
  set that choice to a variable 
  add the JDK to alternatives if it is missing
  save variable to a file and dont prompt next time
  set the alternatives java choice
fi
echo $JAVA_HOME
2
  • why do you need sudo to use find? if your userid can't read it, you shouldn't use that value for JAVA_HOME. Commented Mar 25, 2011 at 19:59
  • your right. took sudo out of all my scripts. thanks. Commented Mar 25, 2011 at 20:29

4 Answers 4

4

something like

#!/bin/bash
function validate_java_home {
    if [ -z ${JAVA_HOME} ]
    then
        # do something if the file doesn't provide ${JAVA_HOME}
    else 
        if [ ! -e ${JAVA_HOME} ]
        then
            # do something if the file provides a non existent ${JAVA_HOME}
        fi
    fi
}

if [ ! -e ${YOUR_FILE_NAME_CONTAINING_JAVA_HOME} ]
then
     JAVA_HOME_CANDIDATES=$(find /usr -name 'jdk1.6*')
     echo "Found the following candidates for JAVA_HOME, reply with the one you want then press return"
     echo ""
     echo $JAVA_HOME_CANDIDATES
     read USER_SUBMITTED_JAVA_HOME
     echo "You chose $USER_SUBMITTED_JAVA_HOME"
     JAVA_HOME=${USER_SUBMITTED_JAVA_HOME}
else
    . ${YOUR_FILE_NAME_CONTAINING_JAVA_HOME}
fi 
validate_java_home
export ${JAVA_HOME}

I haven't tested that but hopefully you get the gist (and I'd say using select as per glenn jackman's answer is more concise/friendly, didn't know that existed so I'm glad I read this Q!)

Sign up to request clarification or add additional context in comments.

2 Comments

Thats more than enough for me to figure it out. Thanks!
Thanks Matt. I posted your edited script below, in the form of my own answer.
4
oldIFS="$IFS"
IFS=$'\n'
choices=( $(find /usr/java -type d -maxdepth 1 -print) )
select choice in "${choices[@]}"; do
  [[ "$choice" ]] && break
done
IFS="$oldIFS"
export JAVA_HOME="$choice"

2 Comments

the "choices" method you used above is really interesting. didn't think of that.
In hindsight, I'd write choices=( /usr/java/*/ ) assuming bash.
2

Not sitting at a linux terminal, but this should get you going:

...
jdkpath=`sudo find /usr -name 'jdk1.6*'`
export JAVA_HOME=$jdkpath
...

Adjust as needed.

3 Comments

Don't get in the habit of tacking sudo on the front of things for no good reason? What good does having binaries readable by root, but not readable by the current user do for you? And don't you want -type d | head -1 at least on there to prevent multiple matches, and matches of zip files and the like?
@Mike I was following the OPs script on the sudo, and I completely agree on the -type d and would change the head -1 to a -maxdepth 1. However, it wasn't in the OPs script and I wasn't 100% sure of why it wasn't there, so I kept it.
sorry. I didn't see the sudo in the OP's script.
0

Based on Matt's answer , here is the script I am using:

#!/bin/bash
# JAVA_HOME script for RPM based java installations
# http://www.oracle.com/technetwork/java/javase/install-linux-64-rpm-138254.html
# examine and understand /etc/alternatives before you run this

cd $SITE_HOME

function set_java_home {
  if [ -z $JAVA_HOME ]; then
      echo "Using default value for JAVA_HOME: /usr/java/default"
      JAVA_HOME=/usr/java/default
  fi
  export -p JAVA_HOME
  echo $JAVA_HOME > java.home.config
  echo "JAVA_HOME variable set to $JAVA_HOME ."
}

if [ -f java.home.config ]; then
  JAVA_HOME=$(<java.home.config)
else
  JAVA_HOME_CANDIDATES=$(find /usr -type d -name 'jdk1.6*')
  echo "Found the following candidates for JAVA_HOME. Pick one: "
  echo "---"
  echo $JAVA_HOME_CANDIDATES
  echo "---"
  read USER_SUBMITTED_JAVA_HOME
  echo "You chose $USER_SUBMITTED_JAVA_HOME ."
  JAVA_HOME=${USER_SUBMITTED_JAVA_HOME}
fi
# Set the variable
set_java_home

1 Comment

It's been 2 years since I posted this answer. Now I am inclided to use a method like this: gist.github.com/djangofan/5526565/raw/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.