操作系统:windows xp
1.jdk安装
1)到官方http://www.oracle.com/technetwork/java/javase/downloads/index.html下载最新的jdk包,有exe格式,也有zip格式,最新版本好像对于windows平台都是exe格式,我用的还是zip,直接解压到本地目录(如D:\Java\jdk1.6.0_17)
2)设置目录(我的电脑-属性-高级-环境变量-系统变量)
JAVA_HOME=D:\Java\jdk1.6.0_17
新建CLASSPATH=.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\jre\lib\dt.jar
追加path=;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
3)试运行,
在控制台下(运行-cmd)输入javac -version 和 java -version,出现以下类似字符串,表示安装成功。
javac 1.6.0_17
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)
写一个helloworld,文件名为HelloWorld.java(一定要这样)
public class HelloWorld{
public static void main(String[]args){
int a;
HelloWorld h=new HelloWorld();
a=h.abs(-7);
System.out.println("-7 的绝对值是"+a);
}
public int abs(int n){
//if (n>=0) return n;
//else return -n;
return n>=0?n:(-n);
}
}
2.junit安装
1)到https://github.com/KentBeck/junit/downloads官网下载junit-XXX.zip文件,解压到本地文件夹(如D:\Java\junit4.8.2),该目录下有doc、javadoc、junit、org文件夹,bulid.xml、junit-XXX.jar、junit-XXX-src.jar等文件,其中XXX是版本号,要把junit-XXX.jar改为junit.jar。
2)配置目录
JUNIT_HOME=D:\Java\junit4.8.2
CLASSPATH追加;%JUNIT_HOME%\junit.jar;%JUNIT_HOME%\src.jar; 、
3)试运行
在控制台输入:java org.junit.runner.JUnitCore,出现以下字符表示安装成功。
JUnit version 4.8.2
Time: 0
OK (0 tests)
3.ant安装
1)在http://ant.apache.org/bindownload.cgi下载最新的Binary Distributions的.zip压缩包,解压到本地磁盘(如F:\apache-ant-1.8.1),该目录下有bin、docs、etc、lib目录和fetch.xml、get-m2.xml等文件
2)配置路径
新建ANT_HOME=F:\apache-ant-1.8.1
追加path=;%ANT_HOME%\bin
3)试运行
在控制台下(运行-cmd)输入ant,出现以下字符串,表示安装成功。
Buildfile: build.xml does not exist!
Build failed
4.一个简单的测试程序:
1)被测源程序 D:\Banyan\hello\src\example\HelloWorld.java
内容是:
public class HelloWorld{
public static void main(String[]args){
int a;
HelloWorld h=new HelloWorld();
a=h.abs(-7);
System.out.println("-7 的绝对值是"+a);
}
public int abs(int n){
//if (n>=0) return n;
//else return -n;
return n>=0?n:(-n);
}
}
2)测试源程序 D:\Banyan\hello\src\example\HelloWorld.java
内容如下:
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class HelloWorldTest {
private HelloWorld hi=new HelloWorld();
@Before
public void setUp() throws Exception {
}
@Test
public void testAbs() {
assertEquals(8, hi.abs(-8));
}
}
3)ant的build文件D:\Banyan\hello\src\example\build.xml
其内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<project name="HelloWorld" default="junit" basedir=".">
<property name="src" value="."/>
<property name="dest" value="."/>
<property name="report" value="."/>
<target name="init">
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}"/>
</target>
<target name ="junit" depends ="compile">
<tstamp/>
<junit printsummary ="true">
<formatter type ="xml"/>
<test name="HelloWorldTest" todir="."></test>
</junit>
<junitreport todir=".">
<fileset dir=".">
<include name="*.xml"/>
</fileset>
<report format="frames" todir="."/>
</junitreport>
</target>
</project>
4)在命令行切换到该目录 (cd /d D:\Banyan\hello\src\example\)后,输入
ant,有如下内容:
D:\Banyan\hello\src\example>ant
Buildfile: D:\Banyan\hello\src\example\build.xml
init:
compile:
[javac] D:\Banyan\hello\src\example\build.xml:9: warning: 'includeantruntime
' was not set, defaulting to build.sysclasspath=last; set to false for repeatabl
e builds
[javac] Compiling 1 source file to D:\Banyan\hello\src\example
junit:
[junit] Running HelloWorldTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.266 sec
[junitreport] the file D:\Banyan\hello\src\example\TESTS-TestSuites.xml is not a
valid testsuite XML document
[junitreport] the file D:\Banyan\hello\src\example\build.xml is not a valid test
suite XML document
[junitreport] Processing D:\Banyan\hello\src\example\TESTS-TestSuites.xml to C:\
DOCUME~1\kortide\LOCALS~1\Temp\null281622628
[junitreport] Loading stylesheet jar:file:/F:/apache-ant-1.8.1/lib/ant-junit.jar
!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform. time: 1829ms
[junitreport] Deleting: C:\DOCUME~1\kortide\LOCALS~1\Temp\null281622628
BUILD SUCCESSFUL
Total time: 6 seconds
5)也可以不用ant,直接用junit测试,过程如下,切换到该目录(cd /d D:\Banyan\hello\src\example\)后,编译软程序,直接测试。
javac HelloWorld.java
javac HelloWorldTest.java
执行后,在D:\Banyan\hello\src\example\下会出现HelloWorld.class 和HelloWorldTest.class文件,然后再在命令行里输入
java org.junit.runner.JUnitCore HelloWorldTest ,出现如下字符
JUnit version 4.8.2
.
Time: 0.016
OK (1 test)
测试完成。