4

I have a makefile, which needs to know the location of the Java include directory because it makes use of the jni.h file. What is the best way of allowing the Makefile to auto-detect where Java is installed on Linux?

Thanks,
Chris

1
  • I think I've decided to just have the user set the JAVA_HOME variable before they run the makefile.
    – Chrisc
    Aug 5, 2010 at 17:46

4 Answers 4

8

How about using

JAVA_HOME=$(shell readlink -f /usr/bin/javac | sed "s:bin/javac::")

Adapted from: https://serverfault.com/questions/143786/how-to-determine-java-home-on-debian-ubuntu

4

You can try this too:

JAVA_HOME:=$(jrunscript -e 'java.lang.System.out.println(new java.io.File(java.lang.System.getProperty("java.home")).getParent());')
1

The JAVA_HOME environment variable contains the path to the Java installation.

3
  • I did think of that, but on my test box, the JAVA_HOME variable is not set (which was how it was by default after a clean install).
    – Chrisc
    Jul 23, 2010 at 16:09
  • 1
    I've actually never seen a java installation without the JAVA_HOME variable set. Did Java come with your distribution, yet they unset the JAVA_HOME variable? Jul 23, 2010 at 16:14
  • 2
    Yeah, it came with the distribution (Mint linux), but when I try "echo $JAVA_HOME", it is empty.
    – Chrisc
    Jul 23, 2010 at 17:28
1

On Linux you can use the which command, such as which java. It should tell you where the location of the java exec it would use is.

However, I'd recommend setting up JAVA_HOME as Erick pointed out.

5
  • The result will probably be /etc/alternatives/java, which is not very helpful
    – finnw
    Jul 23, 2010 at 16:14
  • Cool, I didn't know about that command. This tells me where the "java" executable is, but this is just a link to the real location, which should be something like (/usr/lib/jvm/java-version-here/bin). It's this folder that I need to find the location of.
    – Chrisc
    Jul 23, 2010 at 16:15
  • The result you get isn't a link - it's a string you can parse that and get the folder from it, no?
    – corsiKa
    Jul 23, 2010 at 16:26
  • Also, I still recommend setting the JAVA_HOME, as this is the more common convention amongst Java-based programs and scripts. Although, it's worth noting that more Java is built with ant than with make.
    – corsiKa
    Jul 23, 2010 at 16:28
  • Oh, ok I see. Yeah, when I do an "ls -la" on the "/usr/bin" directory I see the line "javac -> /usr/lib/jvm/jdk1.5.0_22/bin/javac". I think this would work, but how would I get that string inside of a Makefile, and then parse it so that I only had the string "/usr/lib/jvm/jdk1.5.0_22"?
    – Chrisc
    Jul 23, 2010 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.