11

I am trying to understand how classpath really works. After searching around the web this is where I have reached so far:

I have added

export CLASSPATH="/home/foo:/home/foo/Java_code/my_code"

at /etc/environment. I am running Ubuntu by the way.

Java finds the path and compiles without problem.

The problem is that if I change the CLASSPATH and then I do: source /etc/environment, the new CLASSPATH is not applied. It is applied if and only if I restart the system. For example if I delete the

export CLASSPATH="/home/foo:/home/foo/Java_code/my_code"

line, then I do source /etc/environment and I finally do echo $CLASSPATH, what I get is /home/foo:/home/foo/Java_code/my_code. I think I should get an empty line, shouldn't I?

Is there a way to apply the changes in PATH or CLASSPATH variables immediately without having to restart the system?

It might help you know that the /etc/environment file originally contained only the following line:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

Thank you for your time.

3
  • How are you deleting the classpath? Commented Feb 17, 2012 at 14:08
  • why do you do all that stuff, what is your environment? /etc/environment is used to define environment variables for graphical applications. If you have graphics, you can use IDE. Why go to all this trouble with including your home folder+projects in classpath? Commented Feb 17, 2012 at 14:09
  • I am just trying to set CLASSPATH permanently in order to import packages from specific folders without having to define it each time I run javac. Commented Feb 17, 2012 at 14:13

4 Answers 4

7

I think you should not put any paths that a local to your home directory in a system wide file. I would leave /etc/environment well alone, unless you provide some changes, that are necessary or beneficial to all users. Put any changes to the CLASSPATH in your .bashrc in your home directory.

  CLASSPATH=$CLASSPATH:/home/foo:/home/foo/Java_code/my_code
  export CLASSPATH

This way you can source it and any newly started bash will have the settings right at once.

Sign up to request clarification or add additional context in comments.

Comments

5
 export CLASSPATH=""

or better

 unset CLASSPATH

will delete an existing Classpath. There are multiple locations where you can set or unset the classpath - a missing entry will not unset it.

Comments

3

When you remove

export CLASSPATH="/home/foo:/home/foo/Java_code/my_code"

line and then source ... it wouldn't remove the CLASSPATH because it has already been set. It doesn't clear the variables which have already been defined.

2 Comments

So the only way is to restart my system in order the new CLASSPATH to be applied?
You can put the export CLASSPATH="/home/foo:/home/foo/Java_code/my_code" in your .bashrc file in your ~ directoy to set the variables each time you login. To unset the CLASSPATH variable, do unset CLASSPATH
2

The /etc/environment file is not a normal shell script; it is not executed as a shell script when you boot or login into your system. So running it with source /etc/environment does not the same as when you normally boot your system.

See: Ubuntu environment variables

/etc/environment - This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.