views:

74

answers:

4

I'm writing a make file that compiles and runs a Java program, but it'll be run on different platforms so I want the make file to first determine if Java IS installed, and only then will it compile and run the .java file.

How would I go about doing this?

A: 

Consider bundling a jar file with the java code.

A makefile is just an executable script so you could check if java is in the $PATH of the executing environment.

You could also do something like:

#!/bin/bash
java>/dev/null
if [ $? -eq 0 ]
then 
   echo "java is installed"
else
   echo "java is not installed"
fi

But this, again, checks if java exist in the $PATH of the script.

Jes
A: 

Check if the "java" executable can be run (e.g. with -version), and fail if not.

Also note that the java compiler (javac) is not placed in the PATH by default by the JDK installer. If you need a compiler, your simplest approach is to include and use the Eclipse compiler ecj which can be downloaded separately from eclipse.org.

Thorbjørn Ravn Andersen
+3  A: 

I am Trying to understand what you are trying to do, and I get the hunch that you are not going with accepted practices, which is going to make your process delicate and brittle.

The "accepted" way of compiling and running Java is to use Ant or Maven and then build those on a build machine. You do not need eclipse or any other tools to build just a Java JDK or SDK (The names are interchangeable) and either ant or maven on your computer.

You would put the Java code directory structure on the machine and run it through ant or maven and then would end up with class files or JAR,EAR,WAR or whatever you are developing to.

The proper way to deploy is to usually deploy the class files to the machine you are going to run on, which in turn would require a JRE (Java Runtime Environment). The reason for JAR files is to take the directory structure of class files and put it in one file. So it is a good thing that is easy to deploy.

Since you have control of the "build machine" you can make sure that you have both the JDK and ant on it to be able to compile. The challenge is making sure that the machines you are deploying to have a JRE.

To make sure that the machines have a JRE. You have several options.

  1. Use JNLP and have the installer prompt the user to download the proper JRE if he does not have it.
  2. Compile your code to Native Code using GCJ (Has some issues - Does not work with all code)
  3. Prompt your users to install Java some other way.
Romain Hippeau
A: 

If you don't mind restricting yourself to GNU Make (I don't think this is portable), you could do something using the $(shell) function and the which command:

JAVAC := $(shell which javac)

ifeq ($(JAVAC),)
$(warning No javac found!)
endif
Jack Kelly