I created a script in /etc/profile.d
and named it jdk_home.sh
. The contents of jdk_home.sh
are as follows :
#!/bin/sh
export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
export PATH=$JAVA_HOME/bin:$PATH
(I followed an answer of this question on Stack Overflow to set up $JAVA_HOME).
I then typed source /etc/profile.d/jdk_home.sh
on the command line. After that, I typed echo $JAVA_HOME
and it gave me the following output:
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.131.x86_64
After I typed echo $PATH
I got this output:
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.131.x86_64/bin:/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.131.x86_64/bin:/bin:/bin:/bin:/bin:/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.121-0.b13.el6_8.x86_64/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.121/bin:/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin:/bin:/usr/apache/apache-ant-1.9.9/bin:/bin:/bin:/bin:/bin:/bin:/bin
I then opened another terminal in a project folder I named dal
. I put build.xml
in that folder. My build.xml
has the following contents:
<project name="Hello World Project" default="info">
<target name="info">
<echo>Hello World - Welcome to Apache Ant!</echo>
</target>
</project>
I typed ant there on the command line:
[root@gksrv dal]# ant
This produced the following output:
Unable to locate tools.jar. Expected to find it in /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.121-0.b13.el6_8.x86_64/lib/tools.jar
Buildfile: /root/Desktop/dal/build.xml
info:
[echo] Hello World - Welcome to Apache Ant!
BUILD SUCCESSFUL
Total time: 0 seconds
Why am I seeing the tools.jar
warning? Doesn't the path contain the required information?
#!/bin/sh
in scripts that are sourced from/etc/profile.d
. This has the side effect of starting another shell, doing your export commands and then exiting once done which won't source anything into your shell. You just want to put actual commands in/etc/profile.d
files. Look at the other files in that directory as references./etc/profile/*.sh
files are invoked by.
(i.e.,source
) — and, in that context, a shebang is just a comment. Unnecessary, yes; harmful, no.