I have written a script to change the java environment variables of the shell:
#!/bin/bash
#env variables can be changed only if we call the script with source setJavaVersion.sh
case $1 in
6)
export JAVA_HOME=/atgl/product/java/jdk-1.6.0_43/linux-redhat_x86_64/jdk1.6.0_43/
export PATH=$JAVA_HOME:$PATH ;
;;
7)
export JAVA_HOME=/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51
export PATH=$JAVA_HOME:$PATH ;
;;
8)
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64/jre/
export PATH=$JAVA_HOME:$PATH ;
;;
*)
error java version can only be 1.6, 1.7 or 1.8
;;
esac
To execute it, I enter:
source setJavaVersion.sh 6
to set the environment with jdk6, source setJavaVersion.sh 7 for jdk7 and so.
when I look at the environment variables with:
$ echo $JAVA_HOME
and
$ echo $PATH
I see that the variables are well updated.
However, when I execute the command
java -version
it is not updated.
If I enter the same export commands directly in the shell, java -version returns the updated result.
Why ?
Edit:
I have updated my script with the deathangel908 answer.
Here is the output of which java and PATH before and after the script execution:
$ which java
/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin/java
$ echo $PATH
/CPD/SDNT/tools/bin:/CPD/SDNT/tools/x86_64-pc-unix11.0/bin:/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin:/CPD/SDNT/tools/bin:/CPD/SDNT/tools/x86_64-pc-unix11.0/bin:/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/users/t0059888/bin:/users/t0059888/bin
$ source setJavaVersion 6
$ which java
/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin/java
$ echo $PATH
/CPD/SDNT/tools/bin:/CPD/SDNT/tools/x86_64-pc-unix11.0/bin:/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin:/CPD/SDNT/tools/bin:/CPD/SDNT/tools/x86_64-pc-unix11.0/bin:/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/users/t0059888/bin:/users/t0059888/bin:/atgl/product/java/jdk-1.6.0_43/linux-redhat_x86_64/jdk1.6.0_43/
You're appending path every time, you need to remove it and add again
export
: export a="$a:3"
$ echo $a # :3
: export a="$a:3"
: echo $a # :3:3
When you executing java, bash starts lookup in PATH variable, finds the first occurrence and executes it.
You can use which java to check the real path of java command that's executed.
So to solve your issue just remember the path w/o java:
if [ -z ${PATH_SAVE+x} ]; then
export PATH_SAVE="$PATH"
fi
export PATH="$PATH_SAVE:$JAVA_HOME"
Remember to quote variables, in case they contain special symbols or spaces.
Also you can debug your script by running echo $PATH
Based on the output you added in your Edit, the new PATH was added at the end. Since Java 7 is at the beginning of your PATH that one is used when you run which java.
When you execute a command, the first occurrence found on on the PATH will be used, so, try adding it at the beginning of the variable (as you did in your original script, without the changes proposed in the other answer. I mean, it is a good idea what the other answer suggested, that you should not be appending the same paths over and over again, but if you add the Java path at the end of your PATH variable, make sure no other java is found on a previous path).
For what I can see in your original script, it should be working fine.
Try adding set -x at the beginning of your original script, and look at the output. It would be helpful if you could share that output as well.
Finally, make sure the binaries in Java 6 have the right file permissions (make sure java is executable).
My error was that the java executable was not accessible in the path. It is located in the bin folder. What I did before was wrong:
export PATH="$JAVA_HOME:$PATH"
This is the solution:
export PATH="$JAVA_HOME/bin:$PATH"
Related
I am trying to port a simple shell script starting a Java program to a CMD batch script for Windows:
#echo on
set REPO="C:\Users\user1\.m2\repository"
set VERSION=9.3.9.v20160517
"C:\Program Files\Java\jdk1.8.0_66\bin\java.exe" -classpath C:\Users\user1\slova\WebSockets\target\classes;%REPO%\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;%REPO%\org\eclipse\jetty\websocket\websocket-server\%VERSION%\websocket-server-%VERSION%.jar;%REPO%\org\eclipse\jetty\websocket\websocket-common\%VERSION%\websocket-common-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-io\%VERSION%\jetty-io-%VERSION%.jar;%REPO%\org\eclipse\jetty\websocket\websocket-client\%VERSION%\websocket-client-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-servlet\%VERSION%\jetty-servlet-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-security\%VERSION%\jetty-security-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-server\%VERSION%\jetty-server-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-http\%VERSION%\jetty-http-%VERSION%.jar;%REPO%\org\eclipse\jetty\websocket\websocket-servlet\%VERSION%\websocket-servlet-%VERSION%.jar;%REPO%\org\eclipse\jetty\websocket\websocket-api\%VERSION%\websocket-api-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-util-ajax\%VERSION%\jetty-util-ajax-%VERSION%.jar;%REPO%\org\eclipse\jetty\jetty-util\%VERSION%\jetty-util-%VERSION%.jar;%REPO%\org\postgresql\postgresql\9.4.1208.jre7\postgresql-9.4.1208.jre7.jar de.afarber.websockets.MyHandler
As you can see (if you scroll the above code to the right) there is a longer list of file paths following the java -classpath string.
Is it please possible to list the paths each on a separate line - and then concatenate that list by the means of CMD shell to a variable (adding semicolon ; inbetween)?
That way I could better maintain my batch file (easier to edit in editor) and would finally just call java -classpath %CPATHS% de.afarber.websockets.MyHandler
UPDATE:
If all JAR-files would be located in the same dir, I could have used the new Java 8 wildcard syntax java -classpath "\that\dir\*" de.afarber.websockets.MyHandler - but that wasn't the case here.
You can use a variable in this form:
set MYCLASSPATH=C:\Users\user1\slova\WebSockets\target\classes
set MYCLASSPATH=%MYCLASSPATH%;%REPO%\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar
...
"C:\Program Files\Java\jdk1.8.0_66\bin\java.exe" -classpath %MYCLASSPATH% ...
I've tried to make a python script to switch between 32bit and 64 bit java but for some reason
os.system('export JAVA_HOME=/usr/java/path')
os.system('export PATH=$JAVA_HOME/bin:$PATH')
does nothing, but manually it works. How can I fix this? (BTW, this is running on a Linux system.)
The export line will set an environment variable for the shell in which it's executed and all its sub-shells. But what's happening here is that Python creates a new shell, executes the line to set the environment variable, and then the shell terminates. That means the environment variable is no longer in force. In fact, the JAVA_HOME environment variable you set in the first line isn't even in force for the second line when that gets executed, because that's in its own shell that also terminates immediately!
The way round it is to run a whole shell script that sets the environment variable and then launches Java:
#!/bin/bash
JAVA_HOME=/usr/java/path
PATH=$JAVA_HOME/bin:$PATH
java ...
Environment variables are local to each process. If you want to make a permanent change then you can follow the official java PATH instructions. They recommend adding the export variable command to your .bashrc file.
In ~/.bashrc:
export JAVA_HOME=/usr/java/path
export PATH=$JAVA_HOME/bin:$PATH
You can chain the commands together - jdk set + script execution like this:
setJdk4Gradle = 'export JAVA_HOME=/home/jdkPath && export PATH=$JAVA_HOME/bin:$PATH'
os.system(setJdk4Gradle + ' && executeSomething')
I have installed oracle jdk in /usr/lib/jvm/ and i have setted up path in etc/environment as
JAVA_HOME=/usr/lib/jvm/jdk1.7.0_51
PATH=$PATH:$JAVA_HOME/bin
But still when i am running javac, I am getting following error. The program 'javac' can be found in the following packages:
* default-jdk
* ecj
* gcj-4.6-jdk
* gcj-4.7-jdk
* openjdk-7-jdk
* openjdk-6-jdk
It means javac is not installed or java path has not setted properly, however i am able to see javac,java,jps and other programs in my /usr/lib/jvm/jdk1.7.0_51. I have searched enough about it but still not able to get solution of this problem.
The file /etc/environment is not a file executed by the shell (like a shell script); you cannot use $SOMETHING references in this file. Variables are not substituted in this file. So,
JAVA_HOME=/usr/lib/jvm/jdk1.7.0_51
PATH=$PATH:$JAVA_HOME/bin
the second line will not work like this. You have to put the exact path in.
JAVA_HOME=/usr/lib/jvm/jdk1.7.0_51
PATH=...:/usr/lib/jvm/jdk1.7.0_51/bin
The javac binary (and probably other java binaries) is/are not in your user's $PATH environment variable. There are several ways you can address this:
Add /usr/lib/jvm/jdk1.7.0_51/bin to your user's $PATH
environment variable. You can do this by adding a line similar to
the following in your user's .bash_profile:
export PATH=${PATH}:/usr/lib/jvm/jdk1.7.0_51/bin
You'll have to restart your terminal session for it to take effect.
Create symbolic links to the java binaries from some directory
that's already part of your path (such as /usr/bin)
sudo ln -s /usr/lib/jvm/jdk1.7.0_51/bin/java /usr/bin/
sudo ln-s /usr/lib/jvm/jdk1.7.0_51/bin/javac /usr/bin/
BTW: There are several other java executables in /usr/lib/jvm/jdk1.7.0_51/bin. see the symlink commands for java and javac above. You should run similar command for any other executables you may want to use.
Use the fully qualified path directly on the command line:
$ /usr/lib/jvm/jdk1.7.0_51/bin/javac
https://help.ubuntu.com/community/Java
have you tried this page? Its where I go when I need Java info. You may not have the one you installed set as default.
Could it be that you did not refresh the shell after change in path variable?
if you echo $PATH are the changes present?
I am unable to run a crontab job , under a different user.(For e.g sudo -u someuser crontab -e)
It runs absolutely fine under my user profile.
I know what is the issue, but cannot find the resolution for it.
The issue is , when i configure this crontab job for other user, crontab is not able to find java ,as a result even simple java -version is not working.
Below is my script.
#!/bin/bash
export JAVA_HOME=/usr/jdk/jdk1.6.0_31
export PATH=/usr/local/bin:/bin:/usr/bin:/opt/dell/srvadmin/bin:/home/VishalS/bin
echo "JAVA_HOME is: " $JAVA_HOME >> log.out
echo "PATH is: " $PATH >> log.out
which java >> log.out
/usr/bin/java -version >> log.out
/usr/jdk/jdk1.6.0_31/bin/java -version >> log.out
output of above script :
JAVA_HOME is: /usr/jdk/jdk1.6.0_31
PATH is: /usr/local/bin:/bin:/usr/bin:/opt/dell/srvadmin/bin:/home/VishalS/bin
/usr/bin/java
so obviously, the below lines did not work.
/usr/bin/java -version >> log.out
/usr/jdk/jdk1.6.0_31/bin/java -version >> log.out
Could somebody please help me here? I do not understand why even after setting jdk path crontab does not executes java -version ?
Try setting the paths in the other users crontab directly. See 'man 5 crontab'.
The only that comes to my mind is that, the java command might not have the Executable permission to the user you are trying to execute it from.
So use chmod to give necessary permissions to execute.
Crontab(5) runs without ENV, so you need to source an environment (you are building the JAVA_HOME and PATH, but crontab gives you (almost) nothing. notice that the output of "which java" did not appear in your log file.
build an environment script you can source for your crontab script(s), ". path/to/env.sh"
Put full paths on all programs executed in shell scripts etc. You cannot rely upon environment in cron
run "which which", did you get /usr/bin/which? then put that in your script.
we often omit paths from scripts for convenience, but give paths in scripts run from crontab
does the java run when logged in as the other user?
Thanks everyone for your helpful comments.
However the actual fix which worked in my-case , was mix of steps as mentioned below :-
1. Setup Sun JDK path under root's user profile.(earlier open jdk was setup)
2. Gave permission to logs folder where logs were being written.(earlier the permission were not correctly set)
3. Tweaked my cronjob(i think there was an extra space there)
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&