LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Ubuntu (https://www.linuxquestions.org/questions/ubuntu-63/)
-   -   How to add CLASSPATH parameters permanently when using sudo? (https://www.linuxquestions.org/questions/ubuntu-63/how-to-add-classpath-parameters-permanently-when-using-sudo-4175455231/)

Fnux 03-23-2013 06:07 AM

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.

tronayne 03-23-2013 07:16 AM

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.

Fnux 03-23-2013 08:41 AM

I already tried with su
 
Hello tronayne,

Quote:

Originally Posted by tronayne (Post 4917054)
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. :banghead:

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.

tronayne 03-23-2013 09:38 AM

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.

Fnux 03-23-2013 11:12 AM

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.

Fnux 03-25-2013 10:38 AM

Solved
 
Hi tronayne,

Quote:

Originally Posted by tronayne (Post 4917132)
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! :D

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.

tronayne 03-25-2013 11:35 AM

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.


All times are GMT -5. The time now is 12:51 PM.