954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

setting classpath in bash_profile using scripting

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

manish250
Junior Poster in Training
88 posts since Aug 2008
Reputation Points: 10
Solved Threads: 1
 

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
masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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

manish250
Junior Poster in Training
88 posts since Aug 2008
Reputation Points: 10
Solved Threads: 1
 

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

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
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

manish250
Junior Poster in Training
88 posts since Aug 2008
Reputation Points: 10
Solved Threads: 1
 

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

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
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

manish250
Junior Poster in Training
88 posts since Aug 2008
Reputation Points: 10
Solved Threads: 1
 

I give up.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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, yousource 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.

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

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: <code>export CLASSPATH=&quot;$CLASSPATH:/usr/java&quot;</code>

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, yousource 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

manish250
Junior Poster in Training
88 posts since Aug 2008
Reputation Points: 10
Solved Threads: 1
 

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: <em>(empty)</em> 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

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: