Chapter 2 Java Program Compilation and Runtime Environment
---(by Yuan Xiaochun)
2.1 JDK Environment
Java not only ?copy;s a rich language and runtime environment, but also ?copy;s a free set of Java development tools (Java Developers Kits, abbreviated JDK). Programmers and end users can use these tools to develop java programs or invoke Java content. JDK includes the following tools: javac Java language compiler, the output result is Java bytecode java Java bytecode interpreter javap Disassembeler: Java bytecode disassembler, this program returns information such as member variables and methods of a Java program. javaprof resource analysis tool, used to analyze which ?copy;resources a Java program calls during execution, including the number and time of class and method calls, as well as the memory usage of various data types. javah C code processing tool, used to call C++ code from Java classes java Applet Viewer small application browsing tool, used to test and run Java applets java Debugger API Java debugging tool API Prototype Debugger Java debugging tool prototype
The Java development environment also includes Java class libraries (including I/O libraries, user interface libraries, network libraries, etc.) and the HotJava WWW browser. Among them, the HotJava browser ?copy;s a runtime system for running Java code in a WWW environment, and also ?copy;s a Java development framework for WWW developers. The Java interpreter is an independent runtime system for Java programs. It can run those ?copy;platform-independent Java bytecodes in a stable, high-performance way, while the Java compiler is used to generate these ?copy;bytecodes.
2.1.1 Compilation of Java Programs
The compiler for Java programs is javac.exe. The javac command compiles Java programs into bytecode, and then you can use the java interpreter command to interpret and execute this ?copy;Java bytecode. Java program source code must be stored in files with the .java suffix. For each class in a Java program, javac will generate a file with the same name as the class but with the .class suffix. The compiler places the .class file in the same directory as the .java file, unless you use the -d option. When you reference some ?copy;user-defined classes, you must specify the directories where they are stored, and this requires using the environment variable parameter CLASSPATH. The environment variable CLASSPATH consists of some ?copy;path names separated by semicolons. If the class definitions referenced in the source file passed to the javac compiler cannot be found in this file or the other passed files, then the compiler will search according to the paths defined by CLASSPATH. For example:
CLASSPATH = .;C:\java\classes then the compiler first searches the current directory; if it does not find it, it continues searching the C:\java\classes directory. Note that the system always appends the directory of system classes to the end of CLASSPATH by default, unless you compile with the -classpath option. javac_g is an unoptimized compiler used for debugging, with the same functions and usage as javac. The usage of javac is as follows:
javac file.java...
The following is an explanation of each option.
Option explanation:
-classpath path defines the path for javac to search for classes. It overrides the default CLASSPATH environment variable setting. The path consists of some ?copy;path names separated by commas, and the general format is as follows: .; For example: .;C:\java\doc\classes;C:\tools\java\classes means that when the compiler encounters a new class, it first looks for its definition in this file. If not found, it looks for its definition in other files in the directory where this file is located. If still not found, it continues searching all files in the C:\java\doc\classes directory, and so on.
-d directory specifies the root directory of the class hierarchy, in the following format:
javac -d MyProgram.java
This will store the generated .class files from the MyProgram.java program in the my_dir directory.
-g compile with debugging information. The debugging information includes line numbers and local variable information used when using java debugging tools. If compilation is done without the -O optimization option, only line number information is included.
-nowarn turns off warning messages, and the compiler will not display any warning information.
-O optimizes compilation of static, final, and private functions. Note that your class files may become larger.
-verbose
Makes the compiler and interpreter display the names of the source files being compiled and the names of the classes being loaded.
Environment variable
CLASSPATH is used to ?copy;the system with the default path for searching user-defined classes. The paths are separated by semicolons, for example:
.;C:\java\doc\classes;C:\tools\java\classes means that when the compiler encounters a new class, it first looks for its definition in this file. If not found, it looks for its definition in other files in the directory where this file is located. If still not found, it continues searching all files in the C:\java\doc\classes directory, and so on.
2.1.2 Debugging Java Programs Using the java Debugger
A guided tour of jdb
In early pre-Betal versions, the Java debugger jdb was command-line based, like Sun's dbx debugger. To use jdb to debug a Java application, before debugging ?reg;, make sure your application was compiled with the -g flag. For example: javac -g HelloWorld.java
The help command will display a list of available commands in jdb.
>help threads --list threads thread - - set default thread Suspend - - suspend threads resume - - restart threads where |a1|-- print thread stack threadgroups--list thread group numbers threadgroup -- set current thread group print - - print objects or fields dump - - print all object information locals- - print all local variables in the current stack classes- - list currently known classes methods - - list the member functions of a class stop in .-- set a breakpoint in a member function stop at :-- set a breakpoint at a line up - - move up in the thread stack down - - move down in the thread stack clear :-- clear a breakpoint step - - execute the current line cont- - continue execution from a breakpoint catch - - interrupt for a specified condition ignor -- ignore a specified condition list - - print source code use - - display or change source path memeory- - report memory usage load classname- - load a Java class for debugging run - - start executing the loaded class !!- - repeat the above command help(?)- - list all commands exit( or quit)- - leave the debugger
2.1.3 Execution of Java Programs
java - java language interpreter java command interprets java bytecode
Syntax: java classname java_g classname
Description: the java command runs Java bytecode output by the java compiler javac.
The classname parameter is the name of the class to execute. Note that any parameters after the class name will be passed to the main function of the class being executed.
java exits after executing the main function, unless the main function creates one or more threads. If the main function creates other threads, java always waits until the last thread exits before exiting.
Options:
-cs, -checksource when a compiled class is loaded, this option compares the modification time of the bytecode with the modification time of the source file. If the source file modification time is later, it recompiles this class and loads the new class.
-classpath path defines the path for javac to search for classes. It overrides the default CLASSPATH environment variable setting. The path consists of some ?copy;path names separated by commas, and the general format is as follows: .; For example: .;C:\java\doc\classes;C:\tools\java\classes means that when the interpreter encounters a new class, it first looks for its definition in this file. If not found, it looks for its definition in other files in the directory where this file is located. If still not found, it continues searching all files in the C:\java\doc\classes directory, and so on.
-mx x sets the maximum memory allocation pool size to x, where x must be greater than 1000bytes. The default is 16 MB.
-ms x sets the size of the garbage collection heap to x, where x must be greater than 1000bytes. The default is 1 MB.
-noasyncgc turns off asynchronous garbage collection. After this option is turned on, garbage memory is not collected unless explicitly called or the program runs out of memory. If this option is not turned on, the garbage collection thread executes asynchronously and simultaneously with other threads.
-ss x each Java thread has two stacks, one is the java code stack, and one is the C code stack. The -ss option sets the stack used by the thread for C code to a maximum of x.
-oss x each Java thread has two stacks, one is the java code stack, and one is the C code stack. The -oss option sets the stack used by the thread for java code to a maximum of x.
-v, -verbose makes the java interpreter print corresponding information to standard output each time a class is loaded.
Environment variable
CLASSPATH is used to ?copy;the system with the default path for searching user-defined classes. The paths are separated by semicolons, for example:
.;C:\java\doc\classes;C:\tools\java\classes means that when the interpreter encounters a new class, it first looks for its definition in this file. If not found, it looks for its definition in other files in the directory where this file is located. If still not found, it continues searching all files in the C:\java\doc\classes directory, and so on.
2.2 JWS Environment
Java WorkShop is a new SUN product. It is an integrated java language development environment, and it includes the following tools:
l Portfolio and Project manager l source file editor l Build management tool l debugger l project testing l ?copy;extended online hypertext links to help files
These ?copy;tools all have webpage-like hyperlinks on the first page of Java WorkShop, as shown in the figure:
Note that Java WorkShop adopts the interface style of today's browsers. Whatever you want to do, you only need to find the corresponding hyperlink. The specific compilation and debugging functions are implemented using Applets embedded in HTML documents. Therefore, for users accustomed to browsing in the internet style, this kind of interface is very easy to accept.
2.2.1 Portfolio and Project Manager
Protfolios are collections of some ?copy;java applications or Applets. It lets you manage more projects better. A project is a subset within a portfolio, and it contains the following information:
1. How to compile this project 2. How to debug and browse this project 3. How to run this project 4. How to publish this project
2.2.2 Source File Editor
The source file editor can be entered from hyperlinks in the build manager, debugger, and source file browser. In this module, you can enter source files.
2.2.3 Build Management Tool
This module is the project's compiler. You can click the build button to enter this module directly. If a file has an error, the error information will ?copy;a hyperlink directly pointing to the location in the source file where the error occurred.
2.2.4 Visul Java (graphical interface builder ?copy;
As the name suggests, this module lets you visually build some ?copy;complex interfaces. If you have used Visual Basic, you will find them very similar.
2.2.5 Debugger
The debugger lets you very conveniently trace program execution and discover program errors.
Summary of this chapter:
The Java language has two development environments. One is the free JDK, which is command-line based. There is also Java WorkShop, which is an integrated environment for developing java programs. This chapter briefly introduced how to use them.
---(by Yuan Xiaochun)
2.1 JDK Environment
Java not only ?copy;s a rich language and runtime environment, but also ?copy;s a free set of Java development tools (Java Developers Kits, abbreviated JDK). Programmers and end users can use these tools to develop java programs or invoke Java content. JDK includes the following tools: javac Java language compiler, the output result is Java bytecode java Java bytecode interpreter javap Disassembeler: Java bytecode disassembler, this program returns information such as member variables and methods of a Java program. javaprof resource analysis tool, used to analyze which ?copy;resources a Java program calls during execution, including the number and time of class and method calls, as well as the memory usage of various data types. javah C code processing tool, used to call C++ code from Java classes java Applet Viewer small application browsing tool, used to test and run Java applets java Debugger API Java debugging tool API Prototype Debugger Java debugging tool prototype
The Java development environment also includes Java class libraries (including I/O libraries, user interface libraries, network libraries, etc.) and the HotJava WWW browser. Among them, the HotJava browser ?copy;s a runtime system for running Java code in a WWW environment, and also ?copy;s a Java development framework for WWW developers. The Java interpreter is an independent runtime system for Java programs. It can run those ?copy;platform-independent Java bytecodes in a stable, high-performance way, while the Java compiler is used to generate these ?copy;bytecodes.
2.1.1 Compilation of Java Programs
The compiler for Java programs is javac.exe. The javac command compiles Java programs into bytecode, and then you can use the java interpreter command to interpret and execute this ?copy;Java bytecode. Java program source code must be stored in files with the .java suffix. For each class in a Java program, javac will generate a file with the same name as the class but with the .class suffix. The compiler places the .class file in the same directory as the .java file, unless you use the -d option. When you reference some ?copy;user-defined classes, you must specify the directories where they are stored, and this requires using the environment variable parameter CLASSPATH. The environment variable CLASSPATH consists of some ?copy;path names separated by semicolons. If the class definitions referenced in the source file passed to the javac compiler cannot be found in this file or the other passed files, then the compiler will search according to the paths defined by CLASSPATH. For example:
CLASSPATH = .;C:\java\classes then the compiler first searches the current directory; if it does not find it, it continues searching the C:\java\classes directory. Note that the system always appends the directory of system classes to the end of CLASSPATH by default, unless you compile with the -classpath option. javac_g is an unoptimized compiler used for debugging, with the same functions and usage as javac. The usage of javac is as follows:
javac file.java...
The following is an explanation of each option.
Option explanation:
-classpath path defines the path for javac to search for classes. It overrides the default CLASSPATH environment variable setting. The path consists of some ?copy;path names separated by commas, and the general format is as follows: .; For example: .;C:\java\doc\classes;C:\tools\java\classes means that when the compiler encounters a new class, it first looks for its definition in this file. If not found, it looks for its definition in other files in the directory where this file is located. If still not found, it continues searching all files in the C:\java\doc\classes directory, and so on.
-d directory specifies the root directory of the class hierarchy, in the following format:
javac -d MyProgram.java
This will store the generated .class files from the MyProgram.java program in the my_dir directory.
-g compile with debugging information. The debugging information includes line numbers and local variable information used when using java debugging tools. If compilation is done without the -O optimization option, only line number information is included.
-nowarn turns off warning messages, and the compiler will not display any warning information.
-O optimizes compilation of static, final, and private functions. Note that your class files may become larger.
-verbose
Makes the compiler and interpreter display the names of the source files being compiled and the names of the classes being loaded.
Environment variable
CLASSPATH is used to ?copy;the system with the default path for searching user-defined classes. The paths are separated by semicolons, for example:
.;C:\java\doc\classes;C:\tools\java\classes means that when the compiler encounters a new class, it first looks for its definition in this file. If not found, it looks for its definition in other files in the directory where this file is located. If still not found, it continues searching all files in the C:\java\doc\classes directory, and so on.
2.1.2 Debugging Java Programs Using the java Debugger
A guided tour of jdb
In early pre-Betal versions, the Java debugger jdb was command-line based, like Sun's dbx debugger. To use jdb to debug a Java application, before debugging ?reg;, make sure your application was compiled with the -g flag. For example: javac -g HelloWorld.java
The help command will display a list of available commands in jdb.
>help threads --list threads thread - - set default thread Suspend - - suspend threads resume - - restart threads where |a1|-- print thread stack threadgroups--list thread group numbers threadgroup -- set current thread group print - - print objects or fields dump - - print all object information locals- - print all local variables in the current stack classes- - list currently known classes methods - - list the member functions of a class stop in .-- set a breakpoint in a member function stop at :-- set a breakpoint at a line up - - move up in the thread stack down - - move down in the thread stack clear :-- clear a breakpoint step - - execute the current line cont- - continue execution from a breakpoint catch - - interrupt for a specified condition ignor -- ignore a specified condition list - - print source code use - - display or change source path memeory- - report memory usage load classname- - load a Java class for debugging run - - start executing the loaded class !!- - repeat the above command help(?)- - list all commands exit( or quit)- - leave the debugger
2.1.3 Execution of Java Programs
java - java language interpreter java command interprets java bytecode
Syntax: java classname java_g classname
Description: the java command runs Java bytecode output by the java compiler javac.
The classname parameter is the name of the class to execute. Note that any parameters after the class name will be passed to the main function of the class being executed.
java exits after executing the main function, unless the main function creates one or more threads. If the main function creates other threads, java always waits until the last thread exits before exiting.
Options:
-cs, -checksource when a compiled class is loaded, this option compares the modification time of the bytecode with the modification time of the source file. If the source file modification time is later, it recompiles this class and loads the new class.
-classpath path defines the path for javac to search for classes. It overrides the default CLASSPATH environment variable setting. The path consists of some ?copy;path names separated by commas, and the general format is as follows: .; For example: .;C:\java\doc\classes;C:\tools\java\classes means that when the interpreter encounters a new class, it first looks for its definition in this file. If not found, it looks for its definition in other files in the directory where this file is located. If still not found, it continues searching all files in the C:\java\doc\classes directory, and so on.
-mx x sets the maximum memory allocation pool size to x, where x must be greater than 1000bytes. The default is 16 MB.
-ms x sets the size of the garbage collection heap to x, where x must be greater than 1000bytes. The default is 1 MB.
-noasyncgc turns off asynchronous garbage collection. After this option is turned on, garbage memory is not collected unless explicitly called or the program runs out of memory. If this option is not turned on, the garbage collection thread executes asynchronously and simultaneously with other threads.
-ss x each Java thread has two stacks, one is the java code stack, and one is the C code stack. The -ss option sets the stack used by the thread for C code to a maximum of x.
-oss x each Java thread has two stacks, one is the java code stack, and one is the C code stack. The -oss option sets the stack used by the thread for java code to a maximum of x.
-v, -verbose makes the java interpreter print corresponding information to standard output each time a class is loaded.
Environment variable
CLASSPATH is used to ?copy;the system with the default path for searching user-defined classes. The paths are separated by semicolons, for example:
.;C:\java\doc\classes;C:\tools\java\classes means that when the interpreter encounters a new class, it first looks for its definition in this file. If not found, it looks for its definition in other files in the directory where this file is located. If still not found, it continues searching all files in the C:\java\doc\classes directory, and so on.
2.2 JWS Environment
Java WorkShop is a new SUN product. It is an integrated java language development environment, and it includes the following tools:
l Portfolio and Project manager l source file editor l Build management tool l debugger l project testing l ?copy;extended online hypertext links to help files
These ?copy;tools all have webpage-like hyperlinks on the first page of Java WorkShop, as shown in the figure:
Note that Java WorkShop adopts the interface style of today's browsers. Whatever you want to do, you only need to find the corresponding hyperlink. The specific compilation and debugging functions are implemented using Applets embedded in HTML documents. Therefore, for users accustomed to browsing in the internet style, this kind of interface is very easy to accept.
2.2.1 Portfolio and Project Manager
Protfolios are collections of some ?copy;java applications or Applets. It lets you manage more projects better. A project is a subset within a portfolio, and it contains the following information:
1. How to compile this project 2. How to debug and browse this project 3. How to run this project 4. How to publish this project
2.2.2 Source File Editor
The source file editor can be entered from hyperlinks in the build manager, debugger, and source file browser. In this module, you can enter source files.
2.2.3 Build Management Tool
This module is the project's compiler. You can click the build button to enter this module directly. If a file has an error, the error information will ?copy;a hyperlink directly pointing to the location in the source file where the error occurred.
2.2.4 Visul Java (graphical interface builder ?copy;
As the name suggests, this module lets you visually build some ?copy;complex interfaces. If you have used Visual Basic, you will find them very similar.
2.2.5 Debugger
The debugger lets you very conveniently trace program execution and discover program errors.
Summary of this chapter:
The Java language has two development environments. One is the free JDK, which is command-line based. There is also Java WorkShop, which is an integrated environment for developing java programs. This chapter briefly introduced how to use them.
REM 喜欢DOS,因为它的简单
REM 喜欢OS/2,因为它不再矫饰
REM 喜欢BASIC,因为它并不幼稚
REM 喜欢GNU,因为它杂乱无章
REM 喜欢OS/2,因为它不再矫饰
REM 喜欢BASIC,因为它并不幼稚
REM 喜欢GNU,因为它杂乱无章



