Search code examples
windowscdjava-home

When causing a makefile to change directory and run ant in that new directory error "JAVA_HOME is not defined correctly"


Even if you have Java and/or ant installed through the windows installers, you still need to set your environment variables. Make sure you are linking your java to a JDK rather than a JRE, my guess as a newbie is that when you are compiling your own code you should always use the JDK as it has all the tools you need.

Original post:

This is all happening on Windows. I call make. It checks out a "code" folder from svn into the directory that makefile is in. Inside "code", is a build.xml. I want my makefile to change to that directory and then run ant on it. I am getting the error in my command prompt:

C:\Users\Ryan\Desktop\maketest>make
svn co (address removed)
Checked out revision 107.
cd code; ant clean compile jar run
uname: not found
basename: not found
dirname: not found
which: not found
Error: JAVA_HOME is not defined correctly.
  We cannot execute java
make: *** [runant] Error 1

I checked here http://sunsite.ualberta.ca/Documentation/Gnu/make-3.79/html_chapter/make_5.html#SEC46 and it seems that this should be do-able in my makefile:

runant: 
    cd code; ant clean compile jar run

as this will open the command in a subshell but it will also run ant in that subshell.

This all works fine if I manually change into the code folder and run ant. I only get this JAVA_HOME not defined correctly error when I try to do this all from a single makefile.

makefile:

commands = checkoutcode checkoutdocs runant

svnCode = https://version-control.adelaide.edu.au/svn/SEPADL15S2UG7/code/
svnDocs = https://version-control.adelaide.edu.au/svn/SEPADL15S2UG7/updateDocs

ifdef version
REVISION = -r$(version)
endif

all: full

full: checkoutcode checkoutdocs runant

# ensure you use a single tab for the commands being run under that name
checkoutcode: 
    svn co $(svnCode) $(REVISION)

checkoutdocs: 
    svn co $(svnDocs) $(REVISION)

#change directory into the code folder and compile the code
runant: 
    cd code; ant clean compile jar run

build.xml:

<project>
    <!-- add some property names to be referenced later. first one is lib folder to hold all libraries -->
    <property name="lib.dir"     value="lib"/>
    <!-- images directory used to compile with the images and also add them to the jar -->
    <property name="src/images.dir"     value="images"/>

    <!-- ensure the classpath can see all classes in the lib folder by adding **/*.jar -->
    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
    </path>

    <!-- "Target" attribute is what we will write as a parameter to ant in the command prompt or terminal -->
    <!-- eg. "ant clean" will run the commands inside the target tags, which is just to delete the directory 
            "build" -->
    <target name="clean">
        <delete dir="build"/>
        <echo message="Cleaned "/>
    </target>

    <!-- compile command. creates a directory for the classes to be stored in, instead of the root folder
         and then compiles everything in the src folder, using the classpath properties we set earlier -->
    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac includeantruntime="false" srcdir="src" destdir="build/classes" classpathref="classpath"/>
        <echo message="Compiled "/>
    </target>

    <!-- jar command. this creates an executable jar file called Robot.jar which can run the program in full.
         it uses the images and it uses the lejos libraries and embeds them into the jar -->
    <target name="jar">
        <jar destfile="Robot.jar" basedir="build/classes">
            <fileset dir="src/images" />
            <manifest>
                <attribute name="Main-Class" value="Main"/>
            </manifest>
            <!-- this line adds everything in the lib folder to the classes being added to the jar,
            ie. the lejos classes ev3 and pc -->
            <zipgroupfileset dir="${lib.dir}" includes="**/*.jar" />
        </jar>
            <echo message="Created JAR "/>
    </target>

    <!-- run command. runs the program. Running the program in the command prompt or terminal is good 
         because it allows you to see any exceptions as they happen, rather than hiding them in eclipse -->
    <target name="run">
        <java jar="Robot.jar" fork="true"/>
            <echo message="Run "/>
    </target>

</project>

I am required to run this on different machines without any extra configuration performed, and will not be able to confirm the location of Java on those machines - the only guarantee I will get is that Java is installed and the environment variables work.


Solution

  • Just answering to allow you to mark as solved.

    Even if you have installed Java with the oracle installer, you still need to create environements variables.