/**
topic javac java javadoc jar commands instructions
@author ziwen@163.com
@since 1.0
@date 2011 05 01
*//**JDK文档说明
可以在安装之后的JDK目录中找到以下内容:
①java虚拟机 这个东西实现了java程序在各种平台下面的运行,这个也就是所谓的扩平台了
②jdk类库 提供了最基础的java类库以及相应的实用类,例如java.lang java.util java.io java.awt java.swing java.sql等等类库.
③在bin目中 jdk为我们提供了java javac javadoc jar等开发工具,他们的后缀名通常都是.exe
*//**环境变量的设置
通常设置环境变量有三种方式(WINDOWS平台),分别是:
①在java javac javadoc等命令中通过选项进行设置
②通过set 环境变量=变量值 命令进行环境变量的设置
③通过操作系统的"环境变量"选项卡进行系统环境变量的设置
与之对应的,JVM来得到当前环境变量的顺序是:
①JVM首先会去输入的java javac等命令中去找-classpath -sourcepath等等环境变量相关的选项,找到了就用命令中的环境变量,如果找不到就去下一步找.
②JVM去当前的环境变量中去找环境变量的值,如果当前环境变量存在,那么久用当前环境变量的值,如果找不到就去下一步找
③JVM去系统的环境变量中去找环境变量的值.
环境变量的设置说明:
CLASSPATH:
这个是一个非常重要的选项,JVM会从CLASSPATH所确定的目录中去找到java的类文件.可以在"环境变量"选项卡中对CLASSPATH进行设置
例如E:/myjavaroot/classes或者当前路径等.
在环境变量中,两个环境变量之间的连接是靠";"进行连接的.
JAVA_HOME:
这个是java_home的安装路径,值是相应的jdk的安装路径,只需要到bin或者lib的上一级就可以了.
PATH:
设置这个路径的目的是为了我们可以直接在dos环境下使用java javac javadoc jar等开发工具命令
所以我们需要将%JAVA_HOME%/bin目录添加到系统的环境变量当中
*//**编译java源文件(javac命令说明)
E:/CmdJavaPractice>javac
Usage: javac
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath
Specify where to find user class files
-sourcepath
Specify where to find input source files
-bootclasspath
Override location of bootstrap class files
-extdirs
Override location of installed extensions -d
Specify where to place generated class files -encoding
Specify character encoding used by source files -source
Provide source compatibility with specified release -target
Generate class files for specific VM version -help Print a synopsis of standard options 常用的javac命令: -nowarn 不显示警告信息,警告信息并不是错误信息, 只是那些可以通过编译,但是可能存在错误的信息. -verbose 英文意思是冗长的啰嗦的,设置这个选项的目的是输出那些编译过程的信息 -deprecation 输出在源代码中那些地方用到了不提倡使用的API(deprecated) -classpath
指定JAVA到什么地方去找用户的类文件 -d
告诉jvm把编译之后的class放到什么地方去 -sourcepath
告诉jvm到什么地方去找java的源文件 */
/**运行java类文件(java命令说明)
E:/CmdJavaPractice>java
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.
-cp
-classpath
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D
=
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-version:
require the specified version to run -showversion print product version and continue -jre-restrict-search | -jre-no-restrict-search include/exclude user private JREs in the version search -? -help print this help message -X print help on non-standard options -ea[:
...|:
] -enableassertions[:
...|:
] enable assertions -da[:
...|:
] -disableassertions[:
...|:
] disable assertions -esa | -enablesystemassertions enable system assertions -dsa | -disablesystemassertions disable system assertions */
/**制作javadoc文档(javadoc命令说明)
E:/CmdJavaPractice>javadoc -help
usage: javadoc [options] [packagenames] [sourcefiles] [classnames] [@files]
-overview
Read overview documentation from HTML file
-public Show only public classes and members
-protected Show protected/public classes and members (default)
-package Show package/protected/public classes and members
-private Show all classes and members
-help Display command line options and exit
-doclet
Generate output via alternate doclet
-docletpath
Specify where to find doclet class files
-sourcepath
Specify where to find source files
-classpath
Specify where to find user class files -exclude
Specify a list of packages to exclude -subpackages
Specify subpackages to recursively load -breakiterator Compute 1st sentence with BreakIterator -bootclasspath
Override location of class files loaded by the bootstrap class loader -source
Provide source compatibility with specified release -extdirs
Override location of installed extensions -verbose Output messages about what Javadoc is doing -locale
Locale to be used, e.g. en_US or en_US_WIN -encoding
Source file encoding name -J
Pass
directly to the runtime system Provided by Standard doclet: -d
Destination directory for output files -use Create class and package usage pages -version Include @version paragraphs -author Include @author paragraphs -docfilessubdirs Recursively copy doc-file subdirectories -splitindex Split index into one file per letter -windowtitle
Browser window title for the documenation -doctitle
Include title for the overview page -header
Include header text for each page -footer
Include footer text for each page -bottom
Include bottom text for each page -link
Create links to javadoc output at
-linkoffline
Link to docs at
using package list at
-excludedocfilessubdir
:.. Exclude any doc-files subdirectories with given name. -group
:
.. Group specified packages together in overview page -nocomment Supress description and tags, generate only declarations. -nodeprecated Do not include @deprecated information -noqualifier
:
:... Exclude the list of qualifiers from the output. -nosince Do not include @since information -nodeprecatedlist Do not generate deprecated list -notree Do not generate class hierarchy -noindex Do not generate index -nohelp Do not generate help link -nonavbar Do not generate navigation bar -quiet Do not display status messages to screen -serialwarn Generate warning about @serial tag -tag
:
:
Specify single argument custom tags -taglet The fully qualified name of Taglet to register -tagletpath The path to Taglets -charset
Charset for cross-platform viewing of generated documentation. -helpfile
Include file that help link links to -linksource Generate source in HTML -stylesheetfile
File to change style of the generated documentation -docencoding
Output encoding name */
/**将程序打包(jar命令说明) E:/CmdJavaPractice>jar -help 非法选项:h 用法:jar {ctxu}[vfm0Mi] [jar-文件] [manifest-文件] [-C 目录] 文件名 ... 选项: -c 创建新的存档 -t 列出存档内容的列表 -x 展开存档中的命名的(或所有的〕文件 -u 更新已存在的存档 -v 生成详细输出到标准输出上 -f 指定存档文件名 -m 包含来自标明文件的标明信息 -0 只存储方式;未用ZIP压缩格式 -M 不产生所有项的清单(manifest〕文件 -i 为指定的jar文件产生索引信息 -C 改变到指定的目录,并且包含下列文件: 如果一个文件名是一个目录,它将被递归处理。 清单(manifest〕文件名和存档文件名都需要被指定,按'm' 和 'f'标志指定的相同顺序。 示例1:将两个class文件存档到一个名为 'classes.jar' 的存档文件中: jar cvf classes.jar Foo.class Bar.class 示例2:用一个存在的清单(manifest)文件 'mymanifest' 将 foo/ 目录下的所有 文件存档到一个名为 'classes.jar' 的存档文件中: jar cvfm classes.jar mymanifest -C foo/ . *//**实现一个挑选动物的小程序A sample 目的:实现一个挑选小动物的小程序,用户输入数字则挑选相应的小动物 设计:Animal抽象类,Trigger实现类,AnimalFactory利用反射得到小动物,SelectAnimal 1 设计项目的目录结构 2 编码实现Animal Trigger AnimalFactory SelectAnimal类 3 编译该项目 4 运行该项目 5 生成该项目的JAVADOC 6 将该项目打包成可以运行的jar */ // 设计项目的目录结构
/** mkdir src/com/chenzw/animal/impl src/com/chenzw/appmain src/com/chenzw/animalfactory lib classes doc/api deploy 运行结果如下: E:/CmdJavaPractice>tree /f . 卷 WorkSpace 的文件夹 PATH 列表 卷序列号为 00630069 0C2E:A02B E:/CMDJAVAPRACTICE ├─classes ├─deploy ├─doc │ └─api ├─lib └─src └─com └─chenzw ├─animal │ └─impl ├─animalfactory └─appmain */ // 编码实现Animal
Trigger AnimalFactory SelectAnimal类 package com.chenzw.animal;
/** *动物类接口 *@author ziwen@163.com *@version 1.0 *@since 1.0 */public abstract class Animal{
/** *成员变量name */ private String name;
public Animal(){ }
public Animal(String name){ this.setName(name); }
/** *setName方法 *@param String name */public void setName(String name){ this.name=name; }
/** *getName方法 *@return String */public String getName(){
return this.name; }
/** *方法未实现isSelected,实现该类的方法可以进行System输出 *@param String name */public abstract void isSelected(String name); } package com.chenzw.animal.impl; import com.chenzw.animal.Animal;
/** *老虎类,继承自Animal抽象类 *@author ziwen@163.com *@version 1.0 *@since 1.0 */public class
Trigger extends Animal{
/** *实现抽象类Ainmal的抽象方法isSelect可以对Select动作进行反应 *@param String name */publicTrigger(){ System.out.println("
Trigger"); }
public void isSelected(String name){ System.out.println("
您选中了一只老虎,并且给他起了一个好听的名字叫做:"+name); } } package com.chenzw.animalfactory; import java.io.*; import java.util.*; import com.chenzw.animal.Animal; import com.chenzw.animal.impl.*;
/** *动物工厂,利用反射得到动物的示例 *@author ziwen@163.com *@version 1.0 *@since 1.0 */public class AnimalFactory{
/** *定义一个静态变量Properties用来加载animal.properties配置文件 */ private static Properties animals=new Properties();
/** *静态方法,在加载类的时候,会去将配置文件中的内容加载到内存当中 */ static{ try{ InputStream
in=AnimalFactory.class.getResourceAsStream("
animals.properties"); animals.
load(
in); }catch(
Exception e){ //将该异常封装为一个运行时异常抛出 throw new RuntimeException(e); } }
/** *利用反射得到动物的示例 *@return Animal *@param int type */public static Animal getAnimal(
int type){ try{ String animalName=(String)animals.
get(String.valueOf(type));
return (Animal)Class.forName(animalName).newInstance(); }catch(
Exception e){
returnnull; } } } package com.chenzw.appmain; import java.io.*; import java.util.*; import com.chenzw.animal.Animal; import com.chenzw.animal.impl.*; import com.chenzw.animalfactory.AnimalFactory;
/** * 该方法作为程序的入口 *@author ziwen@163.com *@version 1.0 *@since 1.0 */public class SelectAnimal{
/** *@throws Exception NumberFormatException 数据格式不匹配错误 *@author ziwen@163.com */public void
select() throws
Exception{
while(
true){ System.out.println("
请输入你要选择的Animal!输入bye可以退出"); BufferedReader br=new BufferedReader(new InputStreamReader(System.
in)); String str=(String)br.readLine();
if("
bye".equals(str)){
break; }
else{
int type=
Integer.parseInt(str); Animal animal=(Animal)AnimalFactory.getAnimal(type);
if(
null!=animal){ System.out.println("
请你为这个动物起一个好听的名字吧!"); String name=(String)br.readLine(); animal.isSelected(name); }
else{ System.out.println("
你选择了一个错误的动物类型"); } } } }
public static void main(String[] args) throws
Exception{ new SelectAnimal().
select(); } }
/******************************************************************************************************************************** // 编译该项目 E:/CmdJavaPractice>javac -classpath E:/CmdJavaPractice/classes -sourcepath E:/CmdJavaPractice/src -d E:/CmdJavaPractice/classes E:/CmdJavaPractice/src/com/chenzw/animal/*.java E:/CmdJavaPractice/src/com/chenzw/animal/impl/*.java E:/CmdJavaPractice/src/com/chenzw/animalfactory/*.java E:/CmdJavaPractice/src/com/chenzw/appmain/*.java E:/CmdJavaPractice>javac -classpath E:/CmdJavaPractice/classes -sourcepath E:/CmdJavaPractice/src -d E:/CmdJavaPractice/classes E:/CmdJavaPractice/src/com/chenzw/animal/*.java E:/CmdJavaPractice/src/com/chenzw/animal/impl/*.java E:/CmdJavaPractice/src/com/chenzw/animalfactory/*.java E:/CmdJavaPractice/src/com/chenzw/appmain/*.java // 运行该项目 E:/CmdJavaPractice>java -classpath E:/CmdJavaPractice/classes com.chenzw.appmain.SelectAnimal // 生成该项目的JAVADOC E:/CmdJavaPractice>javadoc -private -splitindex -version -author -d E:/CmdJavaPractice/doc/api -sourcepath E:/CmdJavaPractice/src com.chenzw.animal com.chenzw.animal.impl com.chenzw.appmain com.chenzw.animalfactory // 将该项目打包成可以运行的jar ① 在classes文件夹下面使用jar -cvf打包成jar包 E:/CmdJavaPractice/classes>jar -cvf E:/CmdJavaPractice/deploy/SelectAnimal.jar *.* 结果如下: 标明清单(manifest) 增加:com/(读入= 0) (写出= 0)(存储了 0%) 增加:com/chenzw/(读入= 0) (写出= 0)(存储了 0%) 增加:com/chenzw/animal/(读入= 0) (写出= 0)(存储了 0%) 增加:com/chenzw/animal/Animal.class(读入= 499) (写出= 313)(压缩了 37%) 增加:com/chenzw/animal/impl/(读入= 0) (写出= 0)(存储了 0%) 增加:com/chenzw/animal/impl/Trigger.class(读入= 681) (写出= 474)(压缩了 30%) 增加:com/chenzw/animalfactory/(读入= 0) (写出= 0)(存储了 0%) 增加:com/chenzw/animalfactory/AnimalFactory.class(读入= 1487) (写出= 802)(压缩了 46%) 增加:com/chenzw/animalfactory/animals.properties(读入= 34) (写出= 36)(压缩了 -5%) 增加:com/chenzw/appmain/(读入= 0) (写出= 0)(存储了 0%) 增加:com/chenzw/appmain/SelectAnimal.class(读入= 1359) (写出= 843)(压缩了 37%) ② 可以将生成的jar包放到环境变量中进行运行 E:/CmdJavaPractice/classes>java -classpath E:/CmdJavaPractice/deploy/SelectAnimal.jar com.chenzw.appmain.SelectAnimal 请输入你要选择的Animal!输入bye可以退出 1 Trigger 请你为这个动物起一个好听的名字吧! ③ 可以使用jar -xvf将jar包解压开 E:/CmdJavaPractice/deploy>jar -xvf SelectAnimal.jar 创建:META-INF/ 展开:META-INF/MANIFEST.MF 创建:com/ 创建:com/chenzw/ 创建:com/chenzw/animal/ 展开:com/chenzw/animal/Animal.class 创建:com/chenzw/animal/impl/ 展开:com/chenzw/animal/impl/Trigger.class 创建:com/chenzw/animalfactory/ 展开:com/chenzw/animalfactory/AnimalFactory.class 展开:com/chenzw/animalfactory/animals.properties 创建:com/chenzw/appmain/ 展开:com/chenzw/appmain/SelectAnimal.class 看一下MANIFEXT.MF里面的内容: Manifest-Version: 1.0 Created-By: 1.4.2_08 (Sun Microsystems Inc.) ④ 如果希望得到可以直接执行的jar包文件,可以在jar包中建立一个Manifext.txt文件,里面定义Main-Class E:/CmdJavaPractice/classes>echo Main-Class: com.chenzw.appmain.SelectAnimal>>Manifext.txt 需要编辑一下,最后以回车键结束,在Main-Class:与com.chenzw.app..中间有一个空格,否则会报告错误: java.io.IOException: invalid header field at java.util.jar.Attributes.read(Attributes.java:387) at java.util.jar.Manifest.read(Manifest.java:167) at java.util.jar.Manifest.
(Manifest.java:52) at sun.tools.jar.Main.run(Main.java:124) at sun.tools.jar.Main.main(Main.java:904) ⑤ 通过第四步得到的jar包可以直接运行 E:/CmdJavaPractice/classes>jar -cvfm E:/CmdJavaPractice/deploy/SelectAnimal.jar Manifext.txt *.* 标明清单(manifest) 增加:com/(读入= 0) (写出= 0)(存储了 0%) 增加:com/chenzw/(读入= 0) (写出= 0)(存储了 0%) 增加:com/chenzw/animal/(读入= 0) (写出= 0)(存储了 0%) 增加:com/chenzw/animal/Animal.class(读入= 499) (写出= 313)(压缩了 37%) 增加:com/chenzw/animal/impl/(读入= 0) (写出= 0)(存储了 0%) 增加:com/chenzw/animal/impl/Trigger.class(读入= 681) (写出= 474)(压缩了 30%) 增加:com/chenzw/animalfactory/(读入= 0) (写出= 0)(存储了 0%) 增加:com/chenzw/animalfactory/AnimalFactory.class(读入= 1487) (写出= 802)(压缩了 46%) 增加:com/chenzw/animalfactory/animals.properties(读入= 34) (写出= 36)(压缩了 -5%) 增加:com/chenzw/appmain/(读入= 0) (写出= 0)(存储了 0%) 增加:com/chenzw/appmain/SelectAnimal.class(读入= 1359) (写出= 843)(压缩了 37%) 增加:Manifext.txt(读入= 45) (写出= 43)(压缩了 4%) 运行打包之后的jar文件: E:/CmdJavaPractice/deploy>java -jar SelectAnimal.jar 请输入你要选择的Animal!输入bye可以退出 1 Trigger 请你为这个动物起一个好听的名字吧! ****************************************************************************/