2006年6月26日

http://www.csit.fsu.edu/~burkardt/data/data.html
可以从中找到大部分文件格式的相关信息。例如BMP,Obj等等。

http://www.csit.fsu.edu/~burkardt/data/obj/obj.html
介绍OBJ文件格式,关键是当中提供了读写和转换OBJ文件的工具建议。

posted @ 2006-10-31 00:11 cjren 阅读(261) | 评论 (0)编辑 收藏
 

 helloWorldEg.jpg
就一幅图,仔细看看和联想一下,也许会有点得益:-)

posted @ 2006-07-23 20:14 cjren 阅读(257) | 评论 (0)编辑 收藏
 
What do you know The Life Cycle of a Thread?
threads-states.gif
Can you say something about the life cycle of a thread, it's creating, starting, running, not runnale, and stopping? If you do want to know about more, please check it out: http://java.sun.com/docs/books/tutorial/essential/threads/lifecycle.html
posted @ 2006-07-23 20:10 cjren 阅读(312) | 评论 (0)编辑 收藏
 

(1)
The Art of Programming Language
《计算机程序设计艺术》 清华大学出版社

(2)
Introduction to Algorithms, Second Edition
Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein
MIT
《算法导论,影印版》高等教育出版社

(3)
Intruduction to The Design and Analysis of Algorithms
(US)Anany Levitin
算法设计与分析基础
潘彦 译 
清华大学出版社

其实第一本我没有看过,只是提及算法方面的书籍,这一本好像都应该为人所知道似的。第二本的课程资料可以在mit的open course ware页面下载。第三本的译本很不错,感觉很有点幽默有趣,而且书的结构挺新颖,不至于让人马上对算法这东西觉得恐怖和烦闷。推荐第三本。

posted @ 2006-07-23 19:38 cjren 阅读(730) | 评论 (0)编辑 收藏
 

移动 -- 只有GSM,GPRS是GSM的升级版,134-139 手机使用移动的SIM卡       

联通 -- GSM  130,131,132       手机使用联通SIM卡
         -- CDMA 133 (联通新时空),手机使用UIM卡


GSM比较简单。所谓的GSM 900/1800/1900是指GSM手机的工作频率,分别是中国移动支持的900MHz,1900MHz和美国常用的1900MHz。在国内用支持900MHz和1800MHz的GSM 900/1800双频手机就可以了。如果要出国到美国用的,就要用支持1900MHz的三频手机了。

CDMA比GSM的通话质量、手机辐射小和保密性方面都好,但是技术!=市场。
CDMA -- CDMA800
            -- CDMA 1X 现在联通CDMA网络所采取的技术。与真正的CDMA2000相比,CDMA 1x就像我刚才说的,只能支持到153.6kbps的数据速度,因此被称为是2.5G的技术,还不是真正3G的技术。
            -- CDMA2000 一种3G的标准。


3G 有三个标准
            -- WCDMA 有利于中国移动,设备由欧洲进口,容易升级到WCDMA。这套系统能够架设在现有的GSM网络上,对于系统提供商而言可以较轻易地过渡,而GSM系统相当普及的亚洲对这套新技术的接受度预料会相当高。因此W-CDMA具有先天的市场优势。
 
             -- CDMA2000 利于联通,设备由美国进口,容易升级到CDMA2000。目前使用CDMA的地区只有日、韩和北美,所以CDMA2000的支持者不如W-CDMA多。不过CDMA2000的研发技术却是目前各标准中进度最快的,许多3G手机已经率先面世。

             -- TD-SCDMA 中国自己有独立知识产权的产品。采用这一标准,对于国防等重要领域国民经济安全有利。据说中国电信和中国网通都在申请移动运营牌照,他们倾向于TDS-CDMA。
       至于现在中国采用哪一种现在还没有定论。毕竟需要市场和企业来最终决定,记住技术优秀的并不代表最终就能胜出。

posted @ 2006-07-23 19:28 cjren 阅读(540) | 评论 (0)编辑 收藏
 

"Computer science is no more about computers than astronomy is about telescopes."

                                                  E. W. Dijkstra
计算机是一个工具,一个人造的工具,人制造工具的目的是什么?协助自己完成生产劳动,或确切地说是完成自己生存和生活的需要。由于需要完成大量的运算而产生的计算机,即使是到现在也局限于信息的处理,包括简单的运算和信息(数字)的存储。

而在学习当中,“sprint,struct”等这些名字听起来真的很大,而我常问自己“我需要学习它吗?”。是否不懂它们就不算懂java了呢?我从4月份开始觉得java离自己越来越远了,因为我上许多的关于java的论坛,看到的极大部分都是讨论框架,而我对此没有什么概念。“需求驱动学习”是我的信条,没有这个需求,你就没有学习那相应知识的必要,更没有动力。也许这是我懒的借口:-) 大概是自己尚且是学生,没有投身都企业生产当中因此对这些企业开发的知识没有概念吧。反正觉得现在没有学这个的必要,谁知道两年后是否有什么summer or XXname的框架出现呢?

这个暑假其实自己还是想把握时间看看书的,反正又没有找到实习工作。例如data structure & algorithm,算法分析这门课没有学过,真的遗憾,只能自己补补;还有那Developing Games in Java的part 3,当中讲的是3d的renderring,而不涉及opengl以及java3d或opengl binding等API,自己还是很有兴趣的。而昨天在国外的大学的网站上下载了一个OS课程的lecture notes,正好复习一下操作系统的概念。

posted @ 2006-07-08 10:25 cjren 阅读(632) | 评论 (1)编辑 收藏
 
     摘要: 2006-7-7 CP2003 -- Principles of Programming Languages 1997 This course (subject) comes frome http://www.cs.jcu.edu.au/Subjects/cp2003/1997/lecturenotes.html, and you can fin...  阅读全文
posted @ 2006-07-07 10:49 cjren 阅读(601) | 评论 (0)编辑 收藏
 

上午看某文章时候涉及缓冲区溢出的问题,谈到C的栈和堆,有所不懂于是baidu了一下发现论坛上的解释都较为凌乱,google一下后发现国外大学的Lecture Notes 中有不少的说明,下面简单的摘录三段,一是c中的,二是对于java的,三是从os角度看的。

Stack vs Heap Allocation
How the memory of the computer is organized for a running program? When a program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment, and heap segment. The text segment (sometimes also called the code segment) is where the compiled code of the program itself resides. This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system.

The remaining two areas of system memory is where storage may be allocated by the compiler for data storage. The stack is where memory is allocated for automatic variables within functions. A stack is a Last In First Out (LIFO) storage device where new storage is allocated and deallocated at only one ``end'', called the Top of the stack. This can be seen in Figure 14.13. 
 figure14.13.gif

When a program begins executing in the function main(), space is allocated on the stack for all variables declared within main(), as seen in Figure 14.13(a). If main() calls a function, func1(), additional storage is allocated for the variables in func1() at the top of the stack as shown in Figure 14.13(b). Notice that the parameters passed by main() to func1() are also stored on the stack. If func1() were to call any additional functions, storage would be allocated at the new Top of stack as seen in the figure. When func1() returns, storage for its local variables is deallocated, and the Top of the stack returns to to position shown in Figure 14.13(c). If main() were to call another function, storage would be allocated for that function at the Top shown in the figure. As can be seen, the memory allocated in the stack area is used and reused during program execution. It should be clear that memory allocated in this area will contain garbage values left over from previous usage.

The heap segment provides more stable storage of data for a program; memory allocated in the heap remains in existence for the duration of a program. Therefore, global variables (storage class external), and static variables are allocated on the heap. The memory allocated in the heap area, if initialized to zero at program start, remains zero until the program makes use of it. Thus, the heap area need not contain garbage.
小结:
Stack: automatic variables within functions
Heap: global variables (storage class external), and static variables
============================
In java 情况如下
(1)
 The stack is the program memory area, so all your primitive type variables and the memory adress of your objects are written on the stack. It is a fast access valuable memory area.
The heap is where the VM keeps the objects, and it is a huge amount of memory. When you create an object, the VM puts the object in the HEAP and puts the adress of the object created on the STACK.
(2)
 There are two kinds of memory used in Java. These are called stack memory and heap memory. Stack memory stores primitive types and the addresses of objects. The object values are stored in heap memory. An object reference on the stack is only an address that refers to the place in heap memory where that object is kept.
 It is useful to know that these two different kinds of memory exist in Java. Stack memory is the program's memory, and heap memory resides outside of the program.这好像有点跟C的不同(相反)。
引入一点垃圾回收机制的知识
 When you need a new object, Java allocates the required memory. When you are done with an object, the memory is reclaimed for you automatically via Java's garbage collection facility.
 Garbage collection runs as a thread in the background, looking for objects that no longer have a usable reference. When it finds them, it destroys them and reclaims the memory.
 The implementation of garbage collection varies between Java Virtual Machines. They generally follow the same process, however. First, the garbage collector gets a snapshot of all running threads and all loaded classes. Then, all objects that are referred to by this thread set are marked as current. The process stops when all objects that it is possible to reach have been marked and the rest have been discarded.
 In order to help the Virtual Machine, it is a good idea to remove your references to unneeded objects. This is often done by simply setting your reference to null:
 Test t = new Test();
 t.someAction();
 // all done
 t = null;
小结:
Stack: Primitive data types(primitive types), the addresses of objects(=references).
Heap:  objects.

===============================================
从系统的角度看 stack(栈)和heap(堆)
Dynamic Data Structures: The Heap
A typical personal computer or workstation today has somewhere between 16 and 64 megabytes of RAM installed. Using a technique called virtual memory, the system can swap pieces of memory on and off the machine's hard disk to create an illusion for the CPU that it has much more memory, for example 200 to 500 megabytes. While this illusion is complete as far as the CPU is concerned, it can sometimes slow things down tremendously from the user's perspective. Despite this drawback, virtual memory is an extremely useful technique for "increasing" the amount of RAM in a machine in an inexpensive way. Let's assume for the sake of this discussion that a typical computer has a total memory space of, for example, 50 megabytes (regardless of whether that memory is implemented in real RAM or in virtual memory).
The operating system on the machine is in charge of the 50-megabyte memory space. The operating system uses the space in several different ways, as shown here:

 c-heap.gif
The operating system and several applications, along with their global variables and stack spaces, all consume portions of memory. When a program completes execution, it releases its memory for reuse by other programs. Note that part of the memory space remains unused at any given time.

This is, of course, an idealization, but the basic principles are correct. As you can see, memory holds the executable code for the different applications currently running on the machine, along with the executable code for the operating system itself. Each application has certain global variables associated with it. These variables also consume memory. Finally, each application uses an area of memory called the stack, which holds all local variables and parameters used by any function. The stack also remembers the order in which functions are called so that function returns occur correctly. Each time a function is called, its local variables and parameters are "pushed onto" the stack. When the function returns, these locals and parameters are "popped." Because of this, the size of a program's stack fluctuates constantly as the program is running, but it has some maximum size.

As a program finishes execution, the operating system unloads it, its globals and its stack space from memory. A new program can make use of that space at a later time. In this way, the memory in a computer system is constantly "recycled" and reused by programs as they execute and complete.

In general, perhaps 50 percent of the computer's total memory space might be unused at any given moment. The operating system owns and manages the unused memory, and it is collectively known as the heap. The heap is extremely important because it is available for use by applications during execution using the C functions malloc (memory allocate) and free. The heap allows programs to allocate memory exactly when they need it during the execution of a program, rather than pre-allocating it with a specifically-sized array declaration.

posted @ 2006-07-06 17:30 cjren 阅读(4415) | 评论 (3)编辑 收藏
 


在尝试建立自己的可重用package的时候,对package和import语句做了些试验,记录结果如下。

使用说明:
(1)对于src\com\cjren\util\ClassA.java这个提供给其他类使用的class.
package com.cjren.util;
public class ClassA {
  ...
}
编译时:
javac -d . ClassA.java  // -d . 表示了以当前目录为package语句生成的相对根目录。
结果是在ClassA.java所在的目录下生成了com.cjren.util包,里面含ClassA.class,这个包可以复制剪贴到其他的地方供其他类使用。
Notes:
必须使用"-d .",否则ClassA.class并不会放在com.cjren.util包里面,而是生成在和ClassA.java相同的目录下。


(2)对于要使用ClassA的ClassB.java.
import com.cjren.util.ClassA;
public class ClassB {
  ... // use ClassA here
}

To compile: javac ClassB.java
To run:     假设com包放在和ClassB.java的当前目录
            java ClassB       
            // or java -cp . ClassB // here use the "-cp ." as the current directory to find the package com.
            假设com包放在D:\Program Files\Java\mypackages下
            java -cp D:\Program Files\Java\mypackages ClassB

Notes:
错误一:
package com.cjren.util;
public class ClassA {
  ...
}
编译后直接把com.cjren.util包里面的ClassA.class文件拷贝到和ClassB.java相同的目录下直接使用,
public class ClassB {
  ... // use ClassA here
}
To compile: javac ClassB.java
错误信息是
"ClassB.java:某一行: 无法访问 ClassA
 错误的类文件: .\ClassA.class
 类文件包含错误的类: com.cjren.util.ClassA
 请删除该文件或确保该文件位于正确的类路径子目录中。"
想一想为什么出错信息是说:“错误的类文件: .\ClassA.class, 类文件包含错误的类: com.cjren.util.ClassA”呢?因为"错误的类文件: .\ClassA.class"当中包含了"错误的类: com.cjren.util.ClassA"这个信息,而这个"com.cjren.util.ClassA"是在ClassA.java中的"package com.cjren.util;"语句所造成的。
正确的做法是把ClassA.class所在的包整个复制粘贴到ClassB.java所在的目录下,或者在ClassB.java所在的目录下人为手动的建立com\cjren\util\目录,然后直接把ClassA.class文件复制粘贴到此。这暗示了代码生成的包和手动建立的目录可能是一样。
可见,当ClassA.java的源代码中含"package com.cjren.util;"语句,则生成com.cjren.util包里面的ClassA.class文件里面应该就也包含了自己这个class文件所属的包信息,所以这个ClassA.class只能放在这个代码中已经指定的特定包的路径下使用,而不能把这个ClassA.class独立出来使用,因为这样的话将ClassA.class文件属性中的的包信息不相符。当然当这个ClassA.java没有显式地制定package语句信息时候,我怀疑(也觉得应该)是默认在ClassA.class文件当中包含了包的信息就是".",可以理解为默认的加入了"package .;"语句。

错误二:
public class ClassA {
  ...
}
编译后这个ClassA.class文件中含有的包的信息是"."当前目录就包!
把这个ClassA.class文件,移到以ClassB.java所在的目录为当前父目录的人为手动建立的com\cjren\util目录下。
import com.cjren.util.ClassA;
public class ClassB {
  ... // use ClassA here
}

To compile: javac ClassB.java
错误信息是
"ClassB.java:某一行: 无法访问 com.cjren.swing.ClassA
 错误的类文件: .\com\cjren\util\ClassA.class
 类文件包含错误的类: ClassA
 请删除该文件或确保该文件位于正确的类路径子目录中。"
想一想为什么出错信息是说:“错误的类文件: .\com\cjren\util\ClassA.class,类文件包含错误的类: ClassA”呢?因为你这个com.cjren.util包中的ClassA.class文件本来所默认的包信息是".",在ClassA.java中并没有使用"package"语句指定ClassA应该属于包com\cjren\util这个信息。

小结:
在尝试过程当中发现,
public class ClassA {
  ...
}

package com.cjren.util;
public class ClassA {
  ...
}
这两个只差了一个package语句的class文件大小有十几到几十k上的区别,这可能进一步证明了.java文件中的package语句会在.class文件中加入相应的包信息,这个包信息决定了这个class被使用时候的方式(是否需要import特定的包)。
还有一点要注意的是:
package com.cjren.util;
public class ClassA {
  ...
}
之后无论是使用 javac ClassA.java 或者 javac -d . ClassA.java编译都不会影响ClassA.class文件当中所含有的包信息,这两种编译方式所造成的区别在上面的使用说明中已经说过了。


 

posted @ 2006-07-06 09:00 cjren 阅读(809) | 评论 (3)编辑 收藏
 

2006-6-24

尝试以一个例子来说明搭建一个 project 时候的目录构建情况。

 

情况一

下面以一个地图编辑器的例子为例

project mapeditor

父目录是
”project mapeditor”,

.\Image 程序使用到的图片。

.\Maps 程序运行时用户保存的自定义地图。

.\com 主程序 MapEditor.java 将会是用到的 package 的源文件。

.\com\cjren\swing\MyFilter.java

MapEditor.java 主程序源文件,代码中没有 ”package” 语句。

runMapEditor.bat 完成编译和运行所需的所有工作。

 

下面主要看看 runMapEditor.bat 的内容:

@echo off

rem output the date and time

date /T

time /T

echo welcome to my map editor :-)

 

rem make the directory for class files

mkdir .\build\classes

 

rem compile the source files

javac -d .\build\classes MapEditor.java

 

rem run the application,

rem but you must avoid this: java.\build\classes\MapEditor,

rem which will be an error

java -cp .\build\classes MapEditor

 

rem clean everything in the build folder

rmdir .\build /S /Q

 

pause

 

从上面的具体代码中可以看出在编译和运行的时候,所有的 class 文件都回被放在 .\build\classes 这个文件夹中,这个文件夹是动态生成并且最后会被清楚掉的。

 

这里有三个值得注意的地方。

一,通过 ” javac -d .\build\classes MapEditor.java” ,不单只是 MapEditor.java 所定义的 class 都被放于 .\build\classes 中,甚至连 .\com MyFilter.java 所对应的 class 也都在 .\build\classes\com\cjren\swing 中,而 com 包在 .\bulid\classes 下是自动被准确生成的。也就是说所有的 .class 文件都被放在 .\bulid\classes 文件夹中了。

二,当使用 ”java -cp .\build\classes MapEditor” 来运行程序的时候,并没有因为 Image 文件夹不在 .\build\classes 而出错。这说明了 .java 文件中的代码已经决定了它所使用到的 Image 文件夹的位置,这个位置是相对于 .java 文件所在的位置来说的,而不是 .class

三,我尝试了在 d 盘下新建了一个 a 目录,然后把

javac -d .\build\classes MapEditor.java

java -cp .\build\classes MapEditor

换成

javac -d d:\a MapEditor.java

java -cp d:\a MapEditor

之后,其他 ”project mapeditor” 下的所有东西都没有改变,程序依然成功执行。这就进一步证明了 .java 文件中的代码决定了所有其他目录或文件元素的相对位置,这个位置以 .java 文件所在的目录为标准。而与 .class 文件的路径无关。但是:别混淆了 A.class 它所使用到的包的 .class 的路径是以 A.class 文件的路径为标准的!!!这就与上面 MapEditor.class 也和(必须和) com 包同处于 bulid\classes 目录下没有矛盾。

 

情况二

目的是把上面情况一中所有的文件和文件夹都包含在 src 这个文件夹中,而 src 本身就是父目录 project mapeditor2 下的一个文件夹。而且我希望编译时候生成的 build 文件夹和 src 文件夹的关系是:

\project mapeditor2\src

\project mapeditor2\build



src
文件夹的内容如下:

pj2-src
注意批处理文件
runMapEditor.bat src 文件夹下,而且在此情况下, runMapEditor.bat 的内容将有所变化:

@echo off

rem output the date and time

date /T

time /T

echo welcome to my map editor :-)

 

rem make the directory for class files

mkdir ..\build\classes

 

rem compile the source files

javac -d ..\build\classes MapEditor.java

 

rem run the application,

rem but you must avoid this: java.\build\classes\MapEditor,

rem which will be an error

java -cp ..\build\classes MapEditor

 

rem clean everything in the build folder

rmdir ..\build /S /Q

 

pause

 

情况一和情况二的差别不大,只是源代码文件多一层的文件夹的包裹。而且最大的共同点是 runMapEditor.bat 和源代码文件在同一个文件夹下。

 

情况三

尝试在情况二的基础上把 runMapEditor.bat 提到和 src 以及 build 相同的父目录下。假设父目录是 project mapeditor3 ,则:

pj3
现在
runMapEditor.bat 的内容是:

@echo off

rem output the date and time

date /T

time /T

echo welcome to my map editor :-)

 

rem make the directory for class files

mkdir .\build\classes

 

rem go into the src directory

cd src

 

rem compile the source files

javac -d ..\build\classes MapEditor.java

 

rem run the application,

rem but you must avoid this: java.\build\classes\MapEditor,

rem which will be an error

java -cp ..\build\classes MapEditor

 

rem clean everything in the build folder

rmdir ..\build /S /Q

 

pause

 

在尝试工程当中,发现以下的做法将找不到 Image 文件夹:

cd src

javac -d ..\build\classes MapEditor.java

cd ..

java -cp .\build\classes MapEditor

这种修改所造成的区别是运行 ”java” 命令的位置不同了。可以猜想是需要在 src 文件夹内运行 java 命令,而 src 文件夹中含有所有程序所需的代码和 image 素材。

 

下面提供一个稍微不同的批处理文件, runMapEditor2.bat ,它也同样处于 project mapeditor3 这个父目录下:

pj3-

runMapEditor2.bat
的内容如下:

@echo off

mkdir d:\a

cd src

javac -d d:\a MapEditor.java

java -cp d:\a MapEditor

rmdir d:\a /S /Q

pause

这个修改过的例子把 class 文件都移到较远的地方 (d:\a) ,但是还可以成功运行。小结一下它能成功运行的特点:

一, src 文件夹中已经包含了所有的元素,包括所需的外部 package 代码,包括 image 文件夹。这些元素都必须使得主程序 .java 能成功的编译。

二,运行 ”java” 命令都在 src 这个目录下。也就是说都在和代码处在相同的文件路径下,而不是其他任何路径。

 

情况一都三的小结

基本满足了在 windows 系统下开发小项目的需求,我在这三个尝试的例子中主程序 MapEditor.java 都没有含有 ”package main” 这类的打包语句。

posted @ 2006-06-26 01:35 cjren 阅读(387) | 评论 (0)编辑 收藏
 

2006-6-24

Installing Ant
The binary distribution of Ant consists of the following directory layout:

  ant
   +--- bin  // contains launcher scripts
   |
   +--- lib  // contains Ant jars plus necessary dependencies
   |
   +--- docs // contains documentation
   |      +--- ant2    // a brief description of ant2 requirements
   |      |
   |      +--- images  // various logos for html documentation
   |      |
   |      +--- manual  // Ant documentation (a must read ;-)
   |
   +--- etc // contains xsl goodies to:
            //   - create an enhanced report from xml output of various tasks.
            //   - migrate your build files and get rid of 'deprecated' warning
            //   - ... and more ;-)

Only the bin and lib directories are required to run Ant. To install Ant, choose a directory and copy the distribution file there. This directory will be known as ANT_HOME.
在安装ant的时候,需要两个环境变量。ANT_HOME指定ant的安装目录,Path指定bin的目录路径。
-------------------------------------

Using Ant
Each Buildfile contains one project and at least one (default) target. Targets contain task elements. Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this.

A project has three attributes: name (required no), default(the default target to use when no target is supplied) (required no), basedir (required no).
Optionally, a description for the project can be provided as a top-level <description> element. It is include in the output of the ant - projecthelp command. The description has no parameters. One example:
<description>
This buildfile is used to build the Foo subproject within
the large, complex Bar project.
</description>

Each project defines one or more targets. A target is a set of tasks you want to be executed. When starting Ant, you can select which target(s) you want to have executed.
A target has the following attributes: name (required yes), depends, if, unless, description.

A task is a piece of code that can be executed.
Tasks have a common structure:
<name attribute1="value1" attribute2="value2" ... />

Example Buildfile
<project name="MyProject" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>
--------------------------------------

Running Ant
Examples
ant
runs Ant using the build.xml file in the current directory, on the default target.

ant -buildfile test.xml
runs Ant using the test.xml file in the current directory, on the default target.

ant -buildfile test.xml dist
runs Ant using the test.xml file in the current directory, on the target called dist.
-------------------------------------------

上面的内容都是从manual中copy出来的,觉得我用到的特征应该不多。会按装、会写简单的build.xml、以及运行ant就可以了。Ant提供的tasks很多,可能大部分都是复杂工程当中所需的,但是对于我个人写的小projiect,这些tasks就显得没有必要了。简单是一种美德:-),这也是懒得借口之一。
--------------------------------------------

Developing with Ant
Tutorials
Hello World with Ant

We want to separate the source from the generated files, so our java source files will be in src folder. All generated files should be under build, and there splitted into several subdirectories for the individual steps: classes for our compiled files and jar for our own JAR-file.
The later directories are created by our buildfile, so we have to create only the src directory.
write this code into src/oata/HelloWorld.java 代码如下:
  package oata;
  public class HelloWorld {
      public static void main(String[] args) {
          System.out.println("Hello World");
      }
  }

The most simplest buildfile describing that would be:写一个最简单的build.xml:
<project>

    <target name="clean">
        <delete dir="build"/>
    </target>

    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>
    </target>

    <target name="jar">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="oata.HelloWorld"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="build/jar/HelloWorld.jar" fork="true"/>
    </target>

</project>

Now you can compile, package and run the application via

ant compile
ant jar
ant run

Or shorter with

ant compile jar run

完善build file:
<project name="HelloWorld" basedir="." default="main">

    <property name="src.dir"     value="src"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="oata.HelloWorld"/>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

</project>

Now it's easier, just do a ant and you will get

Buildfile: build.xml

clean:

compile:
    [mkdir] Created dir: C:\...\build\classes
    [javac] Compiling 1 source file to C:\...\build\classes

jar:
    [mkdir] Created dir: C:\...\build\jar
      [jar] Building jar: C:\...\build\jar\HelloWorld.jar

run:
     [java] Hello World

main:

BUILD SUCCESSFUL

========这下面是我尝试了这例子后在cmd中的输出==========
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

D:\doctemp\project helloworld using ant>ant
Buildfile: build.xml

clean:
   [delete] Deleting directory D:\doctemp\project helloworld using ant\build

compile:
    [mkdir] Created dir: D:\doctemp\project helloworld using ant\build\classes
    [javac] Compiling 1 source file to D:\doctemp\project helloworld using ant\b
uild\classes

jar:
    [mkdir] Created dir: D:\doctemp\project helloworld using ant\build\jar
      [jar] Building jar: D:\doctemp\project helloworld using ant\build\jar\Hell
oWorld.jar

run:
     [java] Hello World

main:

BUILD SUCCESSFUL
Total time: 2 seconds
D:\doctemp\project helloworld using ant>

=====================================================

此后还讲了使用外部libraries的例子,这里省去。

 

posted @ 2006-06-26 01:27 cjren 阅读(708) | 评论 (0)编辑 收藏
 

2006-6-24
小叙
之前写很小的程序的时候,几个源文件和编译后的class文件统一都放在同一个目录之下,倒也相安无事而且省事。但是在做毕设(坦克对战游戏)的时候,多则二十个的源文件,再连同那些class文件都放在一个目录下,显得非常的杂乱。于是觉得是时候考虑一下一个project的目录结构了,例如把源文件和class文件分开,把测试文件也统一在另一个独立的目录下。

回顾和插曲
之前用C语言程序学习编写小的编译器的时候,我就曾不解怎么把一个程序分为几个独立的文件。那时的做法很傻但简单,就是尽量不用.h头文件,只使用.c文件,然后一层一层的include。后来看《The C Programming Language》第四章head files,才较为清楚地知道了怎样通过.h文件来把一个程序分割为几个小的源代码文件。
这里有一个小的插曲,在3月末的研究生复式中,其中一道编程题是实现stack数据结构,提供push,pop和get的函数实现,并提供一到两个测试例子。我是用C实现的,在实现过程中分为了三个文件,其中两个.c文件和一个.h文件。分别是main.c stack.c and stack.h。
stack.h:定义了main.c and stack.c公用的stack struct变量,以及对main.c将需要用到的和在stack.c里由于函数定义顺序的关系需要到的函数进行了声明(区别于定义)。
stack.c:include “stack.h”,定义了一系列函数,可以为其它文件中所调用(这也是为什么这当中的一些函数需要在stack.h中声明的原因)。
main.c:include “stack.h”,使用到了stack.c中的函数,也就是执行了两个测试例子而已。
stack的具体操作实现细节这个略过了。考试的结果得了A,想必这种源代码文件的组合关系也另评分的老师觉得清晰和有序:-)

原则
这方面的工具(build tool: 名字是构建工具)最流行的好像是ant,在看《Developing Games in Java》的时候,作者也是通过提供build.xml给ant来完成相应的工作,于是我尝试把build.xml看明白了之后,第一印象就是它可以很好的处理一个project的各种文件的目录结构:-),第二印象是又得使用不熟悉的工具了:-(。我觉得自己很懒,懒在不想学一些可能使用不多或不大有用的工具,同时觉得怕,怕在于这个ant是人家的,我害怕一个东西以我不清楚的方法和细节完成工作。这涉及一个学习新工具时候的原则问题:一,它好用吗,可以meet my need吗;二,它是怎么完成的啊,我想“看”清楚它的做法;三,够简单吗。
ant是一个现成的魔术,而我希望自己可以使用现掌握的简单的知识做一个最简单的而且合乎我要求的魔术,而最要紧的是我知道它到底做了什么,它是怎么做了。

工作
现在我有必要自己来完成两件事情:一:明确在一个project目录下有什么目录;二,我怎么实现把各种文件(如.java, .class, .jar, .html)存放到相应的目录并且让它们和调的协作呢?我现在只懂编译时候的-d参数和运行时候的-cp参数。

(1)
尝试学习ant的最简的使用方法,记录在"mynotes about ant -ant的最简单使用方法.txt"文件中。
(2)
下面尝试自己来构建目录结构,而不使用ant。记录在"2006-06-24build project.doc"文件中。


----------------------------
附录一:bat批处理文件中常用的命令
date /t
time /t

mkdir or md
del /Q or earse /Q
rmdir or rd

echo
@echo off
rem
pause

date /t > a.txt | type a.txt //间接地建立一个a.txt文件并把文件的内容显示出来

posted @ 2006-06-26 01:25 cjren 阅读(346) | 评论 (0)编辑 收藏