[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 707
  • Last Modified:

how could I change JAVA_HOME in all these files

I am upgrading the JAVA_HOME parameter in a oracle application server.

I need change the string JAVA_HOME="/usr/java/jdk1.6.0_18" to JAVA_HOME="/usr/java/jdk1.7.0_45" in all of the following files.

Is there a way I can make a script to backup each file and update each file.


[forms@solaris middleware]$ find . -type f -name "*.sh" -exec grep -il JAVA_HOME {} \;
[forms@solaris middleware]$
Jason Yu
Jason Yu
  • 6
  • 4
1 Solution
Jason YuAuthor Commented:
all files are under $MW_HOME which is /u2/oracle/middleware
for file in $(find $MW_HOME -type f -name "*.sh" -exec grep -il JAVA_HOME {} \;)
     sed -i.bak 's@JAVA_HOME="/usr/java/jdk1.6.0_18"@JAVA_HOME="/usr/java/jdk1.7.0_45"@' $file

The above will ceate a backup of each file (by appending ".bak") andthen change each file in place.
Jason YuAuthor Commented:
Could you please explain a little bit of the "sed" line?

Also, do I need put these three four lines in a script and execute it?
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Jason YuAuthor Commented:
Can I also add a log file to this script so that I can check the result?


The "-i" flag tells "sed" to change the file "in place". If "-i" is immediately followed by some string then "sed" will create backup files using that string as a suffix.

The stuff between the quotes is the script to be interpreted by "sed". I used single quotes because there are double quotes in your patterns.

"s" is the subcommand ("subsitute") for replacing strings by other ones.

"@" is the field separator. I used it instead of the usual "/" because there are slashes in your patterns.

The string between the first and second separators is the one to be searched and replaced, and the string between the second and third separators is the one to replace the first string.

$file is the variable filled by the "for" loop with the results of the "find" command, one by one.

You should indeed create a file, maybe in your HOME directory or somewhere else, but not inside the directory structure you're working on (It might be that you choose the script's name as "something.sh", and the script obviously contains the search strings of "grep" and "sed"!) This file should contain the code I posted.
Execute the script either with

sh /path/to/script.sh

or with

chmod +x /path/to/script.sh

"sed -i" normally does not write any output to the terminal or to a log. We could write our own log, perhaps like this:

echo "------- $(date) -------" > $LOG
for file in $(find $MW_HOME -type f -name "*.sh" -exec grep -il JAVA_HOME {} \;)
     echo "Now working on --- $file ---"
     sed -i.bak 's@JAVA_HOME="/usr/java/jdk1.6.0_18"@JAVA_HOME="/usr/java/jdk1.7.0_45"@' $file
     echo "Processed      --- $file --- with returncode $?"
   done >>$LOG 2>&1

I added "2>&1" to also redirect possible error messages to the new log, not just our own "echos".
So you don't  have to go through this again when you update Java, I suggest you change it to:

/usr/java/latest is a link to the latest Java package installed, and gets relinked whenever you update Java
Good point, given that the "latest" version is indeed desired!
Jason YuAuthor Commented:
Dear experts, thank you two for quick responds, I really appreciate your help.

I will run the script as you suggested.


For this case, I just can't use the symbolic link. As it was explained in oracle article (Doc ID 1309855.1):



There are two different approaches that can be used to achieve the required goal. Both will be described briefly:

A) Installing a new JDK home, and then renaming the existing JDK directory

Please avoid using symlinks as previously suggested in this note, as tools such as T2P (test to production) do require a FULL JDK directory structure, and might fail if a symbolic link is used.

Following this approach, the JDK directory location originally used during the installation will not change , and hence it has the minimal drawback that it might keep a versioned name as for example "$INSTALL_HOME/jdk_<version>" while in truth it contains a new version. You may need to follow the next approach once to edit direcories to a generic name, but then able to update regularly by swapping out the contents of the jdk directory. (See the "Tips" in Note 1492980.1)

B) Installing a new JDK home directory and replacing the JAVA_HOME environmental variable in *** all *** the scripts referring to it.

This is the preferred approach if you want to do a partial upgrade (affecting to specific domains only, or not affecting to nodemanager), but its difficulty is around on how to know which files do refer to a JDK location.

The initial list of files to change will be :

* $INSTALL_HOME/wlserver_10.3/common/bin/commEnv.sh
* $INSTALL_HOME/user_projects/domains/<mydomain>/bin/setDomainEnv.sh
* $INSTALL_HOME/wlserver_10.3/common/nodemanager/nodemanager.properties
* $INSTALL_HOME/utils/bsu/bsu.sh
* $INSTALL_HOME/utils/quickstart/quickstart.sh
* $INSTALL_HOME/utils/uninstall/uninstall.sh
even that a more exhaustive way to determine them, will be to run the following command:

find . -type f -name "*.sh" -exec grep -il JAVA_HOME {} \;
Step by Step Actions
Jason YuAuthor Commented:
Wonderful, I got the script running successfully.  does the return code "0" mean it's successful?

------- Mon Sep 22 13:52:31 PDT 2014 -------
Now working on --- /u2/oracle/middleware/utils/clone/clone.sh ---
Processed --- /u2/oracle/middleware/utils/clone/clone.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/utils/clone/restore.sh ---
Processed --- /u2/oracle/middleware/utils/clone/restore.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/utils/uninstall/uninstall.sh ---
Processed --- /u2/oracle/middleware/utils/uninstall/uninstall.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/utils/bsu/bsu.sh ---
Processed --- /u2/oracle/middleware/utils/bsu/bsu.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/utils/quickstart/quickstart.sh ---
Processed --- /u2/oracle/middleware/utils/quickstart/quickstart.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/FormsHome/bin/runProvisioningAntTasks.sh ---
Processed --- /u2/oracle/middleware/FormsHome/bin/runProvisioningAntTasks.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/FormsHome/sqldeveloper/ide/bin/launcher.sh ---
Processed --- /u2/oracle/middleware/FormsHome/sqldeveloper/ide/bin/launcher.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/FormsHome/oui/bin/runSSHSetup.sh ---
Processed --- /u2/oracle/middleware/FormsHome/oui/bin/runSSHSetup.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/oracle_common/sysman/deploy/agentdeploy/remoteif/resources/scripts/sshConnectivity.sh ---
Processed --- /u2/oracle/middleware/oracle_common/sysman/deploy/agentdeploy/remoteif/resources/scripts/sshConnectivity.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/oracle_common/modules/oracle.ovd_11.1.1/oimtemplates/extendadschema.sh ---
Processed --- /u2/oracle/middleware/oracle_common/modules/oracle.ovd_11.1.1/oimtemplates/extendadschema.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/oracle_common/bin/SSLGenCA.sh ---
Processed --- /u2/oracle/middleware/oracle_common/bin/SSLGenCA.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/oracle_common/bin/WLS_SSL_Client_Config.sh ---
Processed --- /u2/oracle/middleware/oracle_common/bin/WLS_SSL_Client_Config.sh --- with returncode 0
Now working on --- /u2/oracle/middleware/oracle_common/bin/pasteConfig.sh ---
Processed --- /u2/oracle/middleware/oracle_common/bin/pasteConfig.sh --- with re
Yes, returncode 0 means "OK". Congrats!
Jason YuAuthor Commented:
woolmilkporc is best, his quick solution helped me achieve an important milestone.

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now