hello all
i m writing a script in which i am trying to set classpath using this


export CLASSPATH=$CLASSPATH:/usr/java


and then i run the script But nothing is added in the bash_profile

Recommended Answers

All 10 Replies

You don't want to use CLASSPATH anyway. If you insist, however (and they always do), simply add that line directly to the .bash_profile (if that's even used otherwise .bashrc) environment file.

Edit: If, however, you insist (and they always do) on keeping it in a separate script, then you need to source that script from the proper environment file, not just execute it i.e.

. script.sh

not just

script.sh

You don't want to use CLASSPATH anyway. If you insist, however (and they always do), simply add that line directly to the .bash_profile (if that's even used otherwise .bashrc) environment file.

Edit: If, however, you insist (and they always do) on keeping it in a separate script, then you need to source that script from the proper environment file, not just execute it i.e.

. script.sh

not just

script.sh

but how i add line in .bash_profile.how i know whether anyother classpath is set or i hav to create a new classpath variable

That line is creating the classpath variable. It doesn't matter if one already exists or not.

That line is creating the classpath variable. It doesn't matter if one already exists or not.

but when i use
. script.sh

no line added to my classpath

Where did you do that? If in .bash_profile, is .bash_profile even being used? Or is it using .bashrc?

Where did you do that? If in .bash_profile, is .bash_profile even being used? Or is it using .bashrc?

in script i m using

export CLASSPATH=$CLASSPATH:/usr/java

but it is not showing in .bash_profile

I give up.

OK. masijade gives up, but I will take a swing at it:

When you need something to be in your working environment "forever", then you do it by editing one of these two files: $HOME/.bash_profile or $HOME/.bashrc Use the first file if it exists, else the second. Use a text editor NOT a WYSIWYG editor. For instance vi or whatever programmers editor you prefer. You add one line to your .bash_profile and the line looks like this: export CLASSPATH="$CLASSPATH:/usr/java" The double quotes are important. You cannot have any spaces around the equal sign, but you can have spaces before the line, for indenting, if you want.

On the other hand, if you do NOT want your environment changed "forever" then you do NOT want to edit your .bash_profile. Instead you create a small script, say 'addjava.sourceme'. It contains the same CLASSPATH line mentioned above, but the script is not executable, so it should fail if you type addjava.sourceme on the command line. Instead, you source that file by typing this on the command line: . addjava.sourceme (Note the leading dot then a space).

Either way, you can check your environment to be sure it is correct. If you have edited .bash_profile then you must first log in again. Now, from the command line, chant echo $CLASSPATH . You will see that the last part of your CLASSPATH now is :/usr/java You possibly do not, in fact, want to alter your CLASSPATH environment variable this way, but you asked how to do it, so we have answered that question for you.

export CLASSPATH="$CLASSPATH:/usr/java"

but what if there is no CLASSPATH variable in bash profile before.

hi griswolf
i want to clear one thing that as you told first option

If you start with no CLASSPATH environment variable, then after the line, you have CLASSPATH=":/usr/java" because the value of an unset variable is the empty string. Try this from the command line, to understand what happens.

unset NO_SUCH
echo $NO_SUCH
export NO_SUCH="$NO_SUCH:/another"
echo $NO_SUCH

Because java first breaks the CLASSPATH by splitting at the colons, java treats :/usr/java as two individual places to look: [I](empty)[/I] and /usr/java Since the empty path is neither a directory nor an archive, it is ignored: http://download.oracle.com/javase/6/docs/technotes/tools/solaris/classpath.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.