发表于: java/j2ee | 作者: | 日期: 2009/5/06 03:05

习惯于GUI工具的快捷与方便之后突然想重温一下最原始的Java程序编写方法。OK,废话少说,开始。

1、
新建HelloWorld.java文件,使用文本编辑器编辑该文件,输入如下内容:

class HelloWorld{
public static void main(String[] args){
System.out.println(“Hello World”);
}
}

2、
打开一个CMD窗口,进入HelloWorld.java所在目录,编译该文件:

E:\javawork\begin>javac HelloWorld.java

编译完毕,运行该文件:

E:\javawork\begin>java HelloWorld
Hello World

呵呵,一切顺利。

3、
接下来看一下如何编译打包之后的Java代码,在HelloWorld.java文件首行添加一行package代码,如下:

package com.darkmi.test;

class HelloWorld{
public static void main(String[] args){
System.out.println(“Hello World”);
}
}

编译:
E:\javawork\begin>javac -d . HelloWorld.java

编译完毕,运行:
E:\javawork\begin>java com.darkmi.test.HelloWorld
Hello World

4、
以下是sun官方文档对-d选项的解释:
-d directory
Set the destination directory for class files. The destination directory must already exist; javac will not create the destination directory. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d c:\myclasses and the class is called com.mypackage.MyClass, then the class file is called c:\myclasses\com\mypackage\MyClass.class.
If -d is not specified, javac puts the class file in the same directory as the source file.

Note that the directory specified by -d is not automatically added to your user class path.

原文链接:http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/javac.html

: https://blog.darkmi.com/2009/05/06/856.html

本文相关评论 - 1条评论都没有呢
Post a comment now » 本文目前不可评论

No comments yet.

Sorry, the comment form is closed at this time.