tags:

views:

40

answers:

2

Hello,

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

A: 

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

Erick Robertson
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
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?
Erick Robertson
Yeah, it came with the distribution (Mint linux), but when I try "echo $JAVA_HOME", it is empty.
Chrisc
+1  A: 

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.

glowcoder
The result will probably be `/etc/alternatives/java`, which is not very helpful
finnw
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
The result you get isn't a link - it's a string you can parse that and get the folder from it, no?
glowcoder
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`.
glowcoder
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