Use a Different Java Version When Executing. Sh Script in Linux

Use a Different Java Version When Executing. Sh Script in Linux

We have Java 7 installed in our linux server but for a specific shell script, want to use a different version and execute. Below is the code I have in shell script, when I execute the shell script, it is not using the new java path set in the file. how can I set the java home for running this script?

#!/bin/bash            
export JAVA_HOME=/data/....          
export PATH=$JAVA_HOME/bin:$PATH       
JAVA=`which java`          
$JAVA -cp DataLoader.jar com.salesforce.dataloader.security.EncryptionUtil

1 Answer

Find where the Java (version) is installed first. They are commonly located at /usr/lib/jvm.

For example I've Java 8 and want to specifically execute with it.

#!/usr/bin/env bash

/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -cp DataLoader.jar com.salesforce.dataloader.security.EncryptionUtil

If you have some bash scripting experience you can save this path in a variable and use it with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.