官术网_书友最值得收藏!

2.4 How to Run the First Java Program

Now we finish the first program. But we do not know how tor runs this application.

Steps to follow:

(1)Install the JDK (Java Development Kit) any version above 1.2 which is freely downloadable from the http://java.sun.com/j2se/1.5.0/download.jsp in to your computer and run it.//Sun公司官網上下載JDK程序包

(2)Check and set the path where the jdk/bin directory is and check if the Java compiler (javac) is running well. //檢查和配置jdk/bin路徑

(3)Open the command and type “javac” to check the Java compiler is running, if not, check the path again..//打開命令行,輸入javac 檢測Java編譯器是否能夠運行

(4)Application or applet program must be saved on the main class name and extension .java(“dot”java). //Java應用程序必須以.java為后綴保存

(5)To compile the program type javac and the program name (javac HelloWorldApp as per the case here) and enter. It would compile the Java file and create the .class file.//通過javac來編譯Java源程序,生成.class字節碼程序

(6)The result or out put could be viewed by writing “java” and the file name (java Hello World App in this case) and enter. You can see the result.//通過java來運行.class文件得到相應結果

Compiling the program

To compile the first program, execute the compiler, javac, specifying the name of the source file on the command line, as shown here:

C:\>javac first.java

The javac compiler creates a file called first.class that contains the bytecode version of the program. As discussed earlier, the Java bytecode is the intermediate representation of your program that contains instructions the Java interpreter will execute. Thus, the output of javac is not code that can be directly executed. //將源文件編譯成字節碼程序

To actually run the program, you must use the Java Interpreter, called java. To do so, pass the class name first as a command-line argument, as shown here:

C:\>java first //運行這個字節碼程序

When the program is run the following output is displayed:

Hello World

source code→Compiler→byte-code file→Interpreter→output screen

first.java javac first.class java Hello World When Java source code is compiled, each individual class is put into its own output file named after the class and using the .class extension.

當Java源代碼被編譯后,每個獨立的類被編譯后的輸出文件名稱由類名和.class擴展名組成。

This is why it is a good idea to give your Java source files the same name as the class they contain-the name of the source file will match the name of the .class file.

給出的Java源代碼文件名與它包容的類名相同,這樣Java源代碼文件名將與編譯后的.class名匹配。

When you execute the Java interpreter as just shown, you are actually specifying the name of the class that you want the interpreter to execute. It will automatically search for a file by that name that has the .class extension. If it finds the file, it will execute the code contained in the specified class. The Figure 2.4 shows life cycle of Java Program:

Figure 2.4 Presentation of Java Programming Life Cycle

主站蜘蛛池模板: 无极县| 冕宁县| 克东县| 江都市| 汽车| 汝阳县| 合肥市| 尼玛县| 揭西县| 泰州市| 隆安县| 子洲县| 安福县| 弥勒县| 抚宁县| 山西省| 麦盖提县| 金昌市| 鄂托克前旗| 海晏县| 宜川县| 中宁县| 秦皇岛市| 昌黎县| 云南省| 城市| 崇文区| 临江市| 金山区| 南安市| 金阳县| 冕宁县| 公安县| 湖北省| 新乡市| 江孜县| 祁连县| 舒城县| 武乡县| 新丰县| 邵阳市|