LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 03-23-2013, 06:07 AM   #1
Fnux
LQ Newbie
 
Registered: Mar 2013
Posts: 18

Rep: Reputation: Disabled
Question How to add CLASSPATH parameters permanently when using sudo?


Hello all,

On Ubuntu 12.04.2 LTS 64bits, when using sudo or sudo -i or sudo su or even su in a session, in order to use both Java AND Scala I need to enter in each terminal the following commands:

sudo -i
CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar
export CLASSPATH

Is it possible to make these changes permanent and effective when I boot?

I've tried to include CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar into .bashrc of both my user account and the root account but this change isn't taken when using sudo, or sudo -i or sudo su or even su!

TIA for any help on this problem.

Last edited by Fnux; 03-25-2013 at 10:24 AM.
 
Old 03-23-2013, 07:16 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Try using su - which gives you a "login" session (as if you logged in as root) so you get all the environment variables set.

I'm not familiar enough with sudo but I would assume that you can start it the same way (as a login session).

Hope this helps some.
 
Old 03-23-2013, 08:41 AM   #3
Fnux
LQ Newbie
 
Registered: Mar 2013
Posts: 18

Original Poster
Rep: Reputation: Disabled
Unhappy I already tried with su

Hello tronayne,

Quote:
Originally Posted by tronayne View Post
Try using su - which gives you a "login" session (as if you logged in as root) so you get all the environment variables set.
Thank you for your post.

However this doesn't solve the problem since I said I also tried with su

My problem is then in what file should I add the classpath needed for scala, since adding the line CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar into:

- .profile
- or .bashrc
- or /etc/bash.bashrc
- or even into sudoers

doesn't work, whenever I use:

- sudo
- or sudo -i
- or sudo su
- or even su

Someone told me on another forum to read the man 5 sudoers in the section environment but there is absolutely nothing about CLASSPASS to add it into the sudoers file (using visudo).

I maybe have a syntax problem but I've also tried to put only CLASSPATH=/usr/share/java/scala-library.jar into the 4 files defined above, but that doesn't work either.

Every time I start echo $CLASSPATH, I get an ampty answer when using any combination of "root" session.

So, I'm stock with my three command lines (that I've to type each time I open a terminal session):

sudo -i
CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar
export CLASSPATH

And this is not practical at all.

If you've another idea.

Maybe it's somewhere into a Java configuration file, but I don't know which one (and trust me, I've googeled a lot these last 4 or 5 days to find an answer that works).

Anyhow, thanx again.
Fnux.
 
Old 03-23-2013, 09:38 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Just in case... when you use su - (with the dash) you get a login shell; i.e., the content of /etc/profile, entries in /etc/profile.d and root's .profile are read (su without the dash does not do that, you get no environment settings other than a bare minimum). You can tell if you open a terminal window and type "su -" and you should see root's profile settings.

Another idea -- is there a directory in /etc with scala configuration? Or is there an etc/profile.d directory (with something like "scala.sh" in it? Or a "conf" file for scala? Any of those should be read at boot, login or application start up.

As a last resort, try adding your CLASSPATH at the end of /etc/profile or, if you have /etc/profile.d with a file jdk.sh in it (which looks like)
Code:
#!/bin/sh
export JAVA_HOME=/usr/lib64/java
export MANPATH="${MANPATH}:${JAVA_HOME}/man"
export PATH="${PATH}:${JAVA_HOME}/bin:${JAVA_HOME}/jre/bin"
you might try adding your CLASSPATH there -- in any case, these may not work if you don't invoke a login shell. You can always take a look at the settings for terminal emulator and click the box for a login shell (depending on your window manger it may be in system settings). In Xfce4 you click the preferences in the terminal window and click the box there).

Last resort: put a little executable in /root:
Code:
#!/bin/sh
export CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar
Save that as whatever and
Code:
chmod 755 whatever
then simply execute it before you start the application.

Best I can do.

Hope this helps some.
 
Old 03-23-2013, 11:12 AM   #5
Fnux
LQ Newbie
 
Registered: Mar 2013
Posts: 18

Original Poster
Rep: Reputation: Disabled
Hi Tronayne,

Thanx again a lot for all these explanations.

I'll try all of those tomorrow and I'll let you know.

Thanx again and have a nice week-end.

PS: I'm using Ubuntu 12.04.2 LTS 64 with Unity, but I'm also using the good old 10.04.4 LTS 64 and gnome and have the same problem with it.
 
Old 03-25-2013, 10:38 AM   #6
Fnux
LQ Newbie
 
Registered: Mar 2013
Posts: 18

Original Poster
Rep: Reputation: Disabled
Lightbulb Solved

Hi tronayne,

Quote:
Originally Posted by tronayne View Post
Just in case... when you use su - (with the dash) you get a login shell; i.e., the content of /etc/profile, entries in /etc/profile.d and root's .profile are read (su without the dash does not do that, you get no environment settings other than a bare minimum). You can tell if you open a terminal window and type "su -" and you should see root's profile settings.

Another idea -- is there a directory in /etc with scala configuration? Or is there an etc/profile.d directory (with something like "scala.sh" in it? Or a "conf" file for scala? Any of those should be read at boot, login or application start up.

As a last resort, try adding your CLASSPATH at the end of /etc/profile or, if you have /etc/profile.d with a file jdk.sh in it (which looks like)
Code:
#!/bin/sh
export JAVA_HOME=/usr/lib64/java
export MANPATH="${MANPATH}:${JAVA_HOME}/man"
export PATH="${PATH}:${JAVA_HOME}/bin:${JAVA_HOME}/jre/bin"
you might try adding your CLASSPATH there -- in any case, these may not work if you don't invoke a login shell. You can always take a look at the settings for terminal emulator and click the box for a login shell (depending on your window manger it may be in system settings). In Xfce4 you click the preferences in the terminal window and click the box there).

Last resort: put a little executable in /root:
Code:
#!/bin/sh
export CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar
Save that as whatever and
Code:
chmod 755 whatever
then simply execute it before you start the application.

Best I can do.

Hope this helps some.
Thanx again for all your suggestions.

However, this is how I solved my problem (may be it's not the best idea, but it works for me):

a) I put a sign # in front of the line Defaults env_reset of the /etc/sudoers file (using visudo of course).

b) Then I put the line CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar just below the lines ENV_SUPATH and ENV_PATH of the file /etc/environment

c) Then after a reboot: Now that's magical!

Now, I still have my CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar environment whenever I use sudo, or sudo -i or sudo su or su oe su -

Hope this may help others wanting tu use both Java and Scala symultaneously.

Thanx again for all your ideas that forced me to experience a lot more.

Kind regards.
Fnux.
 
Old 03-25-2013, 11:35 AM   #7
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Glad you got it.

Something you may want to forget about is "sudo su" -- the redundancy is... worthless?

The sudo utility (from the manual page)
Quote:
sudo allows a permitted user to execute a command as the superuser or another user,
as specified by the security policy.
The su utility, on the other hand (also from the manual page)
Quote:
The su command is used to become another user during a login session. Invoked
without a username, su defaults to becoming the superuser. The optional argument -
may be used to provide an environment similar to what the user would expect had the
user logged in directly.
The difference is not subtle, rather it is profound (and, nope, you don't want to mix 'em).

What you needed to do was place your CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar environment setting in a place where it would be executed (thus, set) at log in. Your addition of the CLASPATH variable to /etc/environmnet apparently accomplished that.

I do not use sudo (in favor of su or su -, personal preference). Digging further into the sudo and sudoers manual pages there are ways to set environment variables, ranging from passing the argument(s) on the command line to command line options -- I'll leave it to you to read about those in the manual pages.

Anyway, glad to know you've got it done.
 
1 members found this post helpful.
  


Reply



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
permanently add static route glock19 Linux - Networking 27 07-27-2015 04:59 PM
add to root's path permanently linuxfia Ubuntu 3 04-14-2008 11:42 PM
add route permanently rithik_ghoshal Solaris / OpenSolaris 8 04-08-2008 03:10 PM
How to permanently add localhost via xhost? h00chman Linux - Newbie 1 04-23-2004 11:57 PM
how to permanently add route? rob99 Linux - Networking 5 07-19-2003 06:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 12:24 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
Open Source Consulting | Domain Registration