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