標籤:
有如下ant的target,為了執行java代碼
<target name="shanhy" depends="compile"><!-- 指明要調用的java類的名稱 --><java classname="Test" fork="true" failonerror="true"><!-- 指明要調用的java類的class路徑 --><classpath path="F:\androidWorkspace\apkPacker\bin"></classpath></java></target>
上面代碼中,classname應該寫java類包括包名的名稱“ com.shanhy.demo.packers.Test ”,我故意寫錯唯寫“ Test ”
在eclipse中使用ant 執行該target 的時候,會出現如下亂碼。
Buildfile: F:\androidWorkspace\Packers\build.xmlTrying to override old definition of task dex-helpercompile: [javac] F:\androidWorkspace\Packers\custom_rules.xml:59: warning: ‘includeantruntime‘ was not set, defaulting to build.sysclasspath=last; set to false for repeatable buildsshanhy: [java] ????: ?????????????????? TestBUILD FAILEDF:\androidWorkspace\Packers\custom_rules.xml:64: Java returned: 1Total time: 1 second
在實際項目開發中,我們可能會用到很多中文的地方,可能會經常出現這樣亂碼的情況,導致我們無法正確的判斷具體的錯誤原因。
解決方案就是 在運行時修改ant 的運行時輸出編碼,我們添加(<sysproperty key="file.encoding" value="UTF-8" />) 後,控制台就可以正常顯示中文了,如下:
<target name="shanhy" depends="compile"><!-- 指明要調用的java類的名稱 --><java classname="Test" fork="true" failonerror="true"> <sysproperty key="file.encoding" value="UTF-8" /><!-- 指明要調用的java類的class路徑 --><classpath path="F:\androidWorkspace\apkPacker\bin"></classpath></java></target>
輸出如下:
Buildfile: F:\androidWorkspace\Packers\build.xmlTrying to override old definition of task dex-helpercompile: [javac] F:\androidWorkspace\Packers\custom_rules.xml:59: warning: ‘includeantruntime‘ was not set, defaulting to build.sysclasspath=last; set to false for repeatable buildsshanhy: [java] 錯誤: 找不到或無法載入主類 TestBUILD FAILEDF:\androidWorkspace\Packers\custom_rules.xml:64: Java returned: 1Total time: 1 second
我們現在將 classname 修改正確,如下:
Buildfile: F:\androidWorkspace\Packers\build.xmlTrying to override old definition of task dex-helpercompile: [javac] F:\androidWorkspace\Packers\custom_rules.xml:59: warning: ‘includeantruntime‘ was not set, defaulting to build.sysclasspath=last; set to false for repeatable buildsshanhy: [java] 單紅宇BUILD SUCCESSFULTotal time: 1 second
測試的Java 類代碼為:
package com.shanhy.demo.packers;public class Test {/** * 測試 * * @param args * @author SHANHY * @date 2015-8-18 */public static void main(String[] args) {System.out.println(args[0]);}}
<target name="shanhy" depends="compile"><!-- 指明要調用的java類的名稱 --><java classname="Test" fork="true" failonerror="true"><!-- 指明要調用的java類的class路徑 --><classpath path="F:\androidWorkspace\apkPacker\bin"></classpath></java></target>
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
eclipse中ant build 控制台亂碼解決解決方案(ant執行java)