eclipse中ant build 控制台乱码解决解决方法(ant执行java)

有如下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.xml
Trying to override old definition of task dex-helper
compile:
    [javac] F:\androidWorkspace\Packers\custom_rules.xml:59: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
shanhy:
     [java] ����: �Ҳ������޷��������� Test

BUILD FAILED
F:\androidWorkspace\Packers\custom_rules.xml:64: Java returned: 1

Total 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.xml
Trying to override old definition of task dex-helper
compile:
    [javac] F:\androidWorkspace\Packers\custom_rules.xml:59: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
shanhy:
     [java] 错误: 找不到或无法加载主类 Test

BUILD FAILED
F:\androidWorkspace\Packers\custom_rules.xml:64: Java returned: 1

Total time: 1 second

我们现在将 classname 修改正确,如下:

Buildfile: F:\androidWorkspace\Packers\build.xml
Trying to override old definition of task dex-helper
compile:
    [javac] F:\androidWorkspace\Packers\custom_rules.xml:59: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
shanhy:
     [java] 单红宇
BUILD SUCCESSFUL
Total 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))