LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-25-2011, 02:10 AM   #1
kaloon88
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
Smile setting multiple java home variables in linux/.bashrc


Hi all

I have two applications I need to set my JAVA_HOME variable to 2 different java versions (jdk.1.4.2_19 and jdk1.5.0_06)on AWS Linux AMI

After some research I found some pointers but I need some support from Linux experts
  • Do I need to change /home/ec2-user/.bashrc by setting conditional export (if possible)?
  • Do I need to make such change in the .bash_profile?
  • or any other better way to do it?

I look forward to your thoughts

Thanks in advance
NK
 
Old 11-25-2011, 01:23 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
Make one of the two (1.5.0_06) your default and put in your .bash_profile. It will get used most of the time.

Add the other one to a script which is used to call that application:

Code:
#!/bin/bash
JAVA_HOME=/usr/java/jdk1.4.2_19/bin/java
PATH=/usr/java/jdk1.4.2_19/bin:$PATH
app2
 
1 members found this post helpful.
Old 11-29-2011, 05:54 PM   #3
kaloon88
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by smallpond View Post
Make one of the two (1.5.0_06) your default and put in your .bash_profile. It will get used most of the time.

Add the other one to a script which is used to call that application:

Code:
#!/bin/bash
JAVA_HOME=/usr/java/jdk1.4.2_19/bin/java
PATH=/usr/java/jdk1.4.2_19/bin:$PATH
app2

Thanks for your prompt help..
I made the default jdk1.5 using export command and .bashrc file. I configured the other one (jdk1.4) through run.con of the Jboss server

Both apps. work with corresponding version now

NK
 
Old 11-30-2011, 07:43 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You could also add a shell function to your environment for switching quickly between versions:

Code:
javaswitch(){
	local jpath1=/usr/java/jdk1.4.2_19/bin
	local jpath2=/usr/java/jdk1.5.0_06/bin

	#Clear any previous entries first
	export PATH=${PATH//$jpath1:}
	export PATH=${PATH//$jpath2:}
	unset JAVA_HOME

	case $1 in
		1) export JAVA_HOME=$jpath1/java
		   export PATH=$jpath1:$PATH
		;;
		2) export JAVA_HOME=$jpath2/java
		   export PATH=$jpath2:$PATH
		;;
	esac
}
Then running javaswitch 1 will give you the 1.4.2 environment, javaswitch 2 will give you the 1.5.0 environnment, and running it with anything else, or nothing, will clear all settings.


BTW, you can condense the two PATH removal commands above into one if you enable exended globbing first (which is useful to enable globally anyway):

Code:
shopt -s extglob

export PATH=${PATH//*($jpath1:|$jpath2:)}
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
setting shell variables- no .bashrc aquaboot Linux Mint 9 01-07-2011 10:33 AM
set up java_home path in /home/user/.bashrc but use path in /usr/lib/java vitalstrike82 Slackware 4 01-14-2009 12:25 AM
Setting up environment variables for j2se and j2ee on linux in .bashrc vitalstrike82 SUSE / openSUSE 1 03-30-2008 08:26 AM
Setting path: /etc/profile, /etc/bashrc or ~/.bashrc Swakoo Linux - General 1 08-07-2007 11:59 PM
Setting environment variables in Java linuxprogrammer Linux - General 2 03-03-2005 06:09 PM


All times are GMT -5. The time now is 08:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration