This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Setting Java Path in Linux Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Setting Java Path in Linux" Watch "Setting Java Path in Linux" New topic
Author

Setting Java Path in Linux

Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Good day to everyone,

Since I have not found a satisfying thread about this (or otherwise specific), I would like to know how to set the Path for java.
I have the following path for java:

/home/jose/java/jdk1.6.0_16/bin

How can I configure Path for Java, so that it's directed to the directory above?

Now, There are some people out there that do it by editing some configuration file around, But I know there's a way of doing it by using commands in terminal. I prefer the latter because editing config. files is error-prone for linux newbies.
So, does anybody know how to ?

I know there's a command along the lines of:

$ sudo update-alternatives --set java /usr/lib/jvm/jre1.6.0/bin/java

But somehow, it didn't work for me this time (can't remember how I did it last time)

Please help me out.

Best Regards,

Jose

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38393
    
  23
Which terminal are you using?
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Hi,

I really wouldn't know how to distinguish between one terminal and another...

I just go to Applications -> Accesories -> Terminal

in Linux Ubuntu 9.04.

I wasn't aware there were many versions for Linux terminals.

Why?


And, thanks again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38393
    
  23
Open a terminal and enter a nonsense command. What is the response?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38393
    
  23
Try entering the following at a terminal
export PATH=/home/jose/java/jdk1.6.0_16/bin:$PATH
That should set the PATH for the lifetime of that terminal. You need to know the type of terminal before setting it permanently. I only know how to do it for bash, I am afraid.
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
The terminal that comes with Ubuntu by default is Gnome Terminal; and it's the one I use of course.

I will have to keep looking about this subject because I really need to have it permanently. any suggestions on where to look for would be nice.

Thank you Campbell.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38393
    
  23
It's almost certainly bash, if it is the Ubuntu default. As I said, enter a nonsense command. Like this
campbell@queeg:~$ rhtogdsncbkjsdhfo
bash: rhtogdsncbkjsdhfo: command not found
See, it says "bash".

Go your home folder and open the file called .bashrc with a text editor. Note it is a hidden file.
cd ~
jose@josescomputer:~$ gedit .bashrc &
Add the line I told you about earlier:
export PATH=/home/jose/java/jdk1.6.0_16/bin:$PATH
to your .bashrc file. Save it, but don't close it. Close your terminal and reopen it. If your new PATH works, Success. If not, try undo/ctrl-Z and try again.
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
You're the MAN Campbell !

That worked very well. The best part is, that If I have to go through this again, I can look it up here on the Topics started by me.

Always a pleasure to interact with someone as knowledgeable as yourself.

Have the best day.

Sincerely,

Jose.

PD. One more thing, will NetBeans detect this PATH, and use it as the one to run and/or compile programs ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38393
    
  23
Jose Campana wrote:You're the MAN Campbell !
You've embarrassed me now . . . but "You're welcome "
PD. One more thing, will NetBeans detect this PATH, and use as the one to run and/or compile programs ?
Don't know, I am afraid. I usually use Eclipse.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

For what it's worth, a "terminal program" is not the same as the shell. You can run any number of shells (bash, csh, ksh, tcsh, scsh, sh, etc.) on Unix-like systems; terminal programs feed the shell input and display its output.

I'd be skeptical that NetBeans will use your PATH--like Eclipse you can set up installed JDKs. This is because you may want to compile/etc. with a different JDK than you're running NetBeans with... but I honestly don't recall at the moment.
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Update:

NetBeans detects that PATH and uses it for program compiling and running.

Hve a great day Campbell !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38393
    
  23
Success
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

How did you install the JDK on your Ubuntu system; I guess you downloaded it yourself from Sun's website and installed it manually in /home/jose/java/jdk1.6.0_16?

Note that Ubuntu has a very good software package management system which can automatically download, install and manage software for you, and which will automatically track updates for you. You should prefer using Ubuntu's package management system above manually downloading and installing software.

The "right" way to install the JDK for Sun Java 6 on Ubuntu is the following command:

sudo apt-get install sun-java6-jdk

That will also automatically put Java in the PATH for you and make it available for NetBeans and other programs. See Ubuntu Documentation > Community Documentation > Java for more details.

Note: If you want to be able to also run Java applets in Firefox, then also install the package sun-java6-plugin:

sudo apt-get install sun-java6-plugin

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38393
    
  23
I keep forgetting about apt-get. Thank you for reminding us, Jesper. And remind us how to use apt to update Java when new version becomes available?
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Good day Jesper, and Everybody !

What I did was to download the jdk from Sun, and unpacked it to the directory I detailed in this thread. I did it in this way for two reasons:

The first one is ignorance about Linux Scripting commands, like the one you just posted. Had I known there was a command to do it in one single step, I would have done it in that way of course.
The second reason is because, I prefer to be able to "see" the process of installing java. I guess one can learn much more about the overall process if one does it this way.

However, it comes to my attention that this is not the right way to do it. now that I know the right way, I only wonder why I did Not find the Right way while doing my research. hmmmmm, anyways, I must say I'm really grateful for Jesper's contribution; I really needed that little amount of guidance to expand my knowledge about how to use Java under Linux.
I've been trying for quite a long time to get better at Linux scripting, and now it's the time.

and hey, you guessed it correctly, and as I detailed it above, I did install Java manually, but it's redundance at this point I guess..... he he

Thank you very much guys,

Read you next time,

Best regards,

Jose


PD. Oh yes ! I do want to know how to update Java too, please tell us !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38393
    
  23
Most people go to the Sun site and follow the instructions there. It doesn't say that many Linux operating systems have an easier way to do it.
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Exactly!

Campbell, you've made a very good point there. I'd dare to say it's sun's sin to have omitted trascendental info. like that, But I haven't really looked into the readmes, and all the text files that come with a jdk.

But still, very good point. The right Procedure, it's certainly kind of 'concealed' from the public eye, he he he

Greetings,

Jose
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Things like apt-get are distro-specific. RH-like systems, for example, used to use RPM (and still might), while Debian-esque distros use apt-get.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38393
    
  23
Yes, I think Fedora still uses rpm. OpenSUSE which I use too, certainly still has rpm.
 
Consider Paul's rocket mass heater.
 
subject: Setting Java Path in Linux