classpath echo returns blank in linux - java

I just open file :.bash_profile
export JAVA_HOME=/usr/java/jdk1.8.0_91/bin
Now in command prompt when I fire command : echo $JAVA_HOME , It returns me blank.
Am I missing anything? I am trying to set permanent class path in linux.
I have read and follow many similar questions but still no success.

The contents of .bash_profile are only executed when you start a Login Shell. This is what happens if you, say, log in, or if you run bash --login.
The file you're probably looking for is ~/.bashrc, which is sourced by bash (i.e. executed) every time you start bash. You should put your export there.
Alternatively, on Linux you could put the line JAVA_HOME=/usr/java/jdk1.8.0_91/bin in the file /etc/environment, which should do exactly what you're trying to do (but you need to log out and log in first). This will be system-wide, though.
(for the record, the system-wide .bashrc file is /etc/bash.bashrc)
EDIT: Protip: You can check your environment variables in BASH by typing export with no arguments.

If your .bash_profile doesn't read also .bashrc, you need to add the above export also to .bashrc.
Or, you could add:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
And add everything in .bashrc from now on.

Related

$JAVA_HOME path not sourced correctly in ~/bash_profile at login

Upon logging into my CentOS 7 VM, my $JAVA_HOME is always set to :
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-11.b12.el7.x86_64/jre, which is incorrect.
My ~/.bash_profile reads:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.144-0.b01.el7_4.x86_64/jre
export JAVA_HOME
PYCHARM_HOME=/opt/pycharm-community-2017.2.3/
export PYCHARM_HOME
ECLIPSE_HOME=/opt/eclipse
export ECLIPSE_HOME
export SPARK_HOME=/opt/spark
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$JAVA_HOME/bin:$SPARK_HOME/bin:$PYCHARM_HOME/bin:$ECLIPSE_HOME
export PATH
Sourcing .bash_profile each time I open a new terminal appears to correct the issue, but why is my $JAVA_HOME path defaulting to an old version on startup and not being set to the directory specified in .bash_profile?
Sourcing .bash_profile each time I open a new terminal appears to
correct the issue, but why is my $JAVA_HOME path defaulting to an old
version on startup and not being set to the directory specified in
.bash_profile?
You need to logout from current user and login again so environment variables changes take place.
May be useful
In Bash:
Bash as login shell will load /etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile in the order.
Bash as non-login interactive shell will load ~/.bashrc
Environment :
Use /etc/environment to permanently system wide (all users, all processes) set environmental variables for all users.
/etc/environment is a system-wide configuration file, which means it is used by all users. It is owned by root though, so you need to be an admin user and use sudo to modify it.
Suppose if you set foo="bar" variable foo will be accessible from all the user sessions. To test the variable output first source it
source /etc/environment
~/.profile is one of your own user's personal shell initialization scripts. Every user has one and can edit their file without affecting others.
/etc/profile and /etc/profile.d/*.sh are the global initialization scripts that are equivalent to ~/.profile for each user. The global scripts get executed before the user-specific scripts though; and the main /etc/profile executes all the *.sh scripts in /etc/profile.d/ just before it exits.
Also note,
The /etc/environment file sets the variable system wide for every user on login.
Commands in the bash_profile are is executed if the bash shell is opened by any user. So the variables would not be set unless a bash shell is opened at least one time.
Run ps in the interactive shell to make sure you're in bash.
Try bash -l after login and see if it fixes your problem.
Add set -x before JAVA_HOME=... to check if there's something wrong.
It worked for me!
In my case, The follwing caused the error:
export JAVA_HOME = /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
The error was that there were spaces before and after the = . The error disappeared after those spaces were removed.
So the correct command is as follows:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home

setting JAVA_HOME to the JDK location mac osx 10.9.5

I followed this thread to solve my problem, I have the same problem with a different version of JDK and for some reason it didn't work. I still get -bash: JAVA_HOME: command not found when I type JAVA_HOME to my terminal.
I have JDK1.8.0.45 located at Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home which I can access with /usr/libexec/java_home
Using
echo "export JAVA_HOME=/usr/libexec/java_home" >> ~/.profile
~/.profile
I do get
"export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home"
saved to ~/.profile, and I can check this using /usr/bin/open ~/.profile
But I do not understand why JAVA_HOME still doesn't work and I need this to install maven.
Thanks!
My ~/.bash_profile has this line in it, and it's been working fine.
export JAVA_HOME=$(/usr/libexec/java_home)
This line basically says "run the program named /usr/libexec/java_home and export its output as a variable named JAVA_HOME."
I suggest opening your .profile or .bash_profile in a text editor rather than using echo statements to append new lines to it. That way you can see everything that's in the file and make sure other old lines in the file aren't causing you issues.
After you make a change to .bash_profile, make sure you open a new terminal window before testing it.
You can check the value of any environment variable (including JAVA_HOME) by simply echo'ing its value:
echo $JAVA_HOME
In my case the output of that echo command is:
/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home
Probably you misunderstood things. JAVA_HOME is not a command, it's an environment variable. You can't call JAVA_HOME in your terminal since - in fact - there is no such command, what your error message says.
You can see this variable's value by:
echo $JAVA_HOME
If it is set and points to a proper location then it's fine. Some tools that expect this variable to be set (e.g. Maven or Java IDEs) will work well.

set System.getenv in Linux

Im building some application, where i would like to get some variable directly from linux.
I know, that the command to get the variable is System.getenv(), but i don't exackly know where i should set variable in linux.
I'm using Centos, and my tomcat is set in /usr/share/tomcat6
I was thinking, that maybe i should write it in /etc/profile file, but it doesn't works, and additionally i think that i should past the variable in user folder. User for Tomcat is tomcat, and his folder is /usr/share/tomcat6.
Should i create there file profile and add something like this? :
MY_VARIABLE=value
export MY_VARIABLE
Please help
From Terminal:
gedit ~/.bashrc
~ here refers to /home/USER directory.
Edit the file:
MY_VARIABLE=value
export MY_VARIABLE
confirm changes:
echo $MY_VARIABLE

default value of JAVA_HOME seems to be overwriting the value set by: /etc/profile.d/java.sh

I've created a file /etc/profile.d/java.sh that includes (among other things) the line of code: export JAVA_HOME=/usr/java/jdk1.6.0_30. However, when I log in via SSH or reboot the linux Centos 6.2 server, I always see:
[root#host3 /etc/profile.d]# echo $JAVA_HOME
/usr/local/jdk
This is a new installation and server, so there's not much else done. It seems that the java.sh file is executing upon reboot or SSH login, however, the value of JAVA_HOME is being overwritten by the default value of /usr/local/jdk somehow. There isn't even a directory jdk under /usr/local.
Any idea how to correct this? The value of JAVA_HOME needs to be set (as done in java.sh) for all users.
UPDATE 1:
I checked the /etc/profile file and searched for the text jdk as well as JAVA and neither were found. I also checked the /root/.bash_profile and it shows as follows:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
Also, if I source java.sh, everything gets set correctly. But if I reboot server, JAVA_HOME goes back to /usr/local/jdk.
Check the /etc/profile file or the .bash_profile/.profile in your home directory.
It appears that cPanel adds the following lines to the /etc/bashrc file to set the JAVA_HOME to it's default.
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/classes.zip
So, if you have cPanel installed, make sure you check this file as well.

Do commands run from current directory in a shell script?

In a bash shell script I tried these two versions:
java -jar abc.jar&
and
CMD="java -jar abc.jar&"
$CMD
The first verison works, and the second version complains that abc.jar cannot be found. Why?
Commands do run from current directory in a shell script.
This is why the first command in your test script worked.
The second command may not work because either java isn't in your ${PATH} or abc.jar isn't in your ${CLASSPATH}. You could echo these environment variables or set +x to debug your bash script.
Bash (and others) won't let you do backgrounding (&) within the value of a variable (nor will they let you do redirection that way or pipelines). You should avoid putting commands into variables. See BashFAQ/050 for some additional information.
What is the actual error message you're getting? I bet it's something like "abc.jar& not found" (note the ampersand) because the ampersand is seen as a character in the filename.
Also, the current directory for the script is the directory that it is run from - not the directory in which it resides. You should be explicit about the directory that you want to have your file in.
java -jar /path/to/abc.jar&

Categories