您的位置:首页 > 编程语言 > Java开发

java中的关键字package和命令行的编译方法

2012-07-11 12:57 239 查看
java中用于封包的关键字:package 以及在命令行编译、测试的方法

package cn.wzb;

public class TestAssert {
public TestAssert() {
// TODO Auto-generated constructor stub
}

public static void main(String[] args) {
TestAssert test = new TestAssert();

try {
assert false; // to trigger an AssertionError when using java -ea option

test.myAssert(true);
test.myAssert(false);
}catch(AssertionError ae) {
System.out.println("There is an AssertionError here, please check!");
ae.printStackTrace();
}catch(Throwable t){
System.out.println("There is an unknown Error is thrown here");
t.printStackTrace();
}

}

public void myAssert(boolean b) {
assert b ? true : false;
System.out.println("This is the true conditions !");
}
}


如上面这个源文件封装在包:cn.wzb 中

1. 命令行编译的方法:

 在源文件所在的目录,执行:

javac -d . TestAssert.java其中 -d 用来指定 生成class及其包的目录, . 表示当前目录。

2. 命令行测试的方法:

  在包所在的目录中,执行:

java cn.wzb.TestAssert说明: 必须加包名在class前,否则,会报ClassNotFoundException 。

3. javac 命令行选项参数说明:

F:\wzb>javac
用法:javac <选项> <源文件>
其中,可能的选项包括:
-g 生成所有调试信息
-g:none 不生成任何调试信息
-g:{lines,vars,source} 只生成某些调试信息
-nowarn 不生成任何警告
-verbose 输出有关编译器正在执行的操作的消息
-deprecation 输出使用已过时的 API 的源位置
-classpath <路径> 指定查找用户类文件和注释处理程序的位置
-cp <路径> 指定查找用户类文件和注释处理程序的位置
-sourcepath <路径> 指定查找输入源文件的位置
-bootclasspath <路径> 覆盖引导类文件的位置
-extdirs <目录> 覆盖安装的扩展目录的位置
-endorseddirs <目录> 覆盖签名的标准路径的位置
-proc:{none,only} 控制是否执行注释处理和/或编译。
-processor <class1>[,<class2>,<class3>...]要运行的注释处理程序的名称;绕过默认的搜索进程
-processorpath <路径> 指定查找注释处理程序的位置
-d <目录> 指定存放生成的类文件的位置
-s <目录> 指定存放生成的源文件的位置
-implicit:{none,class} 指定是否为隐式引用文件生成类文件
-encoding <编码> 指定源文件使用的字符编码
-source <版本> 提供与指定版本的源兼容性
-target <版本> 生成特定 VM 版本的类文件
-version 版本信息
-help 输出标准选项的提要
-Akey[=value] 传递给注释处理程序的选项
-X 输出非标准选项的提要
-J<标志> 直接将 <标志> 传递给运行时系统


4. java 命令行选项说明:

F:\wzb>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 <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
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[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息