2015-09-27 50 views
0

即使通過Windows安裝程序安裝了Java和/或ant,仍需要set your environment variables。確保你將java連接到JDK而不是JRE,作爲一個新手,我猜測你編譯自己的代碼時,應該總是使用JDK,因爲它具有所有你需要的工具。當導致makefile改變目錄並在新目錄中運行ant時出錯「JAVA_HOME未正確定義」

原帖:

這一切發生在Windows上。我打電話給make。它從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 

,因爲這將在打開命令subshel​​l,但它也會在該子shell中運行ant。

這一切工作正常,如果我手動更改到代碼文件夾並運行螞蟻。當我嘗試從一個makefile中完成所有操作時,我只會得到這個JAVA_HOME沒有正確定義的錯誤。

生成文件:

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並且環境變量正常工作。

+0

終端在鍵入時打印什麼:'echo%JAVA_HOME%' –

+0

ok - 我將JAVA_HOME添加爲用戶環境變量。回聲JAVA_HOME打印路徑jre1.8.0_51 現在它的工作原理,但我得到以下 - uname:找不到 基地名稱:找不到 錯誤:無法找到或加載主類org.apache.tools.ant.launch。發射器 –

+0

好的,還得添加ANT_HOME環境變量。現在它說它不能在java home lib文件中找到tools.jar。所以,我的朋友說我需要一個JDK而不是JRE。即時下載它,但是,當然,我們都不知道運行環境和開發工具包在使用螞蟻方面的區別 –

回答

0

只是回答讓你標記爲已解決。

即使您已經使用oracle安裝程序安裝了Java,您仍然需要創建環境變量。

0

即使您通過Windows安裝程序安裝了Java和/或ant,您仍需要set your environment variables.確保您將java鏈接到JDK而不是JRE,但作爲新手,我的猜測是當您編譯時您自己的代碼應始終使用JDK,因爲它具有您需要的所有工具。