3

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.shon 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?

5
  • You don't want to put a #!/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.
    – slm
    Mar 21, 2017 at 1:00
  • @slm:  Are you sure?  I believe that all /etc/profile/*.sh files are invoked by . (i.e., source) — and, in that context, a shebang is just a comment.  Unnecessary, yes;  harmful, no. Mar 23, 2017 at 23:10
  • @a_sid: You should probably not do software development as root unless you really really need to. In general, you shouldn’t do anything as root except what you absolutely have to do as root. Mar 23, 2017 at 23:10
  • @G-Man Thank you for your suggestion. Why should we not do anything as root?
    – a_sid
    Apr 1, 2017 at 13:44
  • 1
    Because it’s like using a chainsaw to make your sandwich and a flamethrower to make your tea — it makes it easy for the slightest error to cause massive damage. Apr 6, 2017 at 6:52

1 Answer 1

8

I later discovered that tools.jar wasn't in the jdk directory. I ran the command yum install java-1.8.0-openjdk-develand lib/tools.jarbecame available in the java folder. When I ran the ant command after this, I did not get the same warning.

4
  • 3
    For future reference on yum-based systems whenever you run into a "I need to know what to install to get this file" problem you can use yum whatprovides <path-to-file> for instance, in this case: yum whatprovides */tools.jar Which lists all the packages that provide that file. That's usually a good one to start with.
    – Bratchley
    Mar 20, 2017 at 19:16
  • @Bratchley: I believe the OP’s problem was that he thought that he had the file, but the system just wasn’t looking in the right place.  However, a more pertinent comment might be, “For future reference, whenever you get an Unable to locate «file» message, you should verify that you have the file, and determine where it is.” Mar 23, 2017 at 23:12
  • @G-Man I don't think the OP implies they thought anything other than it should be in $PATH somewhere. In that case just trying to install a JRE-appropriate package that contains the file and retrying the operation is a valid troubleshooting MO.
    – Bratchley
    Mar 25, 2017 at 18:48
  • For ubuntu users check if you install openjdk or openjdk-headless , if you install headless tools.jar is not installed
    – fatcook
    May 6, 2022 at 12:46

Not the answer you're looking for? Browse other questions tagged .