当导致 makefile 更改目录并且 运行 ant 在那个新目录错误 "JAVA_HOME is not defined correctly"

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

即使您通过 windows 安装程序安装了 Java and/or ant,您仍然需要 set your environment variables。确保将 java 链接到 JDK 而不是 JRE,我作为新手的猜测是,当您编译自己的代码时,您应该始终使用 JDK您需要的所有工具。

原版post:

这一切都发生在 Windows。我叫作。它从 svn 检出一个 "code" 文件夹到 makefile 所在的目录中。在 "code" 里面是一个 build.xml。我希望我的 makefile 更改为该目录,然后 运行 ant 就可以了。我在命令提示符中收到错误消息:

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

我在这里查看 http://sunsite.ualberta.ca/Documentation/Gnu/make-3.79/html_chapter/make_5.html#SEC46 似乎这应该可以在我的 makefile 中实现:

runant: 
    cd code; ant clean compile jar run

因为这将在子 shell 中打开命令,但它也会 运行 在该子 shell 中运行。

如果我手动切换到代码文件夹和 运行 ant,这一切都可以正常工作。当我尝试从单个 makefile 执行所有操作时,我只收到此 JAVA_HOME not defined correctly 错误。

生成文件:

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>

我需要在不同的机器上 运行 执行此操作而无需执行任何额外配置,并且无法确认 Java 在这些机器上的位置 - 我能得到的唯一保证是Java 已安装并且环境变量有效。

即使您通过 windows 安装程序安装了 Java and/or ant,您仍然需要 set your environment variables. 确保链接 java JDK 而不是 JRE,我作为新手的猜测是,当您编译自己的代码时,您应该始终使用 JDK,因为它具有您需要的所有工具。

只是回答让你标记为已解决。

即使您已经使用 oracle 安装程序安装 Java,您仍然需要创建环境变量。