[SOLVED] How to add CLASSPATH parameters permanently when using sudo?
UbuntuThis forum is for the discussion of Ubuntu Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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!
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):
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).
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
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)
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).
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)
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).
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.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.