Appendix C

Compile and Run Simple Single-File Programs in One Step

image

In Chapter 1, you were shown how to compile a Java program into bytecode using the javac compiler and then run the resulting .class file(s) using the Java launcher java. This is how Java programs have been compiled and run since Java’s beginning, and it is the method that you will use when developing applications. However, beginning with JDK 11, it is possible to compile and run some types of simple Java programs directly from the source file without having to first invoke javac. To do this, pass the name of the source file, using the .java file extension, to java. This causes java to automatically invoke the compiler and execute the program.

For example, the following automatically compiles and runs the first example in this book:

java Example.java

In this case, the Example class is compiled and then run in a single step. There is no need to use javac. Be aware, however, that no .class file is created. Instead, the compilation is done behind the scenes. As a result, to rerun the program, you must execute the source file again. You can’t execute its .class file, because one wasn’t created.

One use of the source-file launch capability is to facilitate the use of Java programs in script files. It can also be useful for short one-time-use programs. In some cases, it makes it a little easier to run simple example programs when you are experimenting with Java. It is not, however, a general-purpose substitute for Java’s normal compilation/execution process.

Although this new ability to launch a Java program directly from its source file is appealing, it comes with some restrictions. First, the entire program must be contained in a single source file. However, most real-world programs use multiple source files. Second, it will always execute the first class it finds in the file, and that class must contain a main( ) method. If the first class in the file does not contain a main( ) method, the launch will fail. This means that you must follow a strict organization for your code, even if you would prefer to organize it otherwise. Third, because no .class files are created, using java to run a single-file program does not result in a class file that can be reused, possibly by other programs. As a result of these restrictions, using java to run a single-file source program can be useful, but it constitutes what is, essentially, a special-case technique.

As it relates to this book, it is possible to use the single source-file launch feature to try many of the examples; just be sure that the class with the main( ) method is first in your file. That said, it is not, however, applicable or appropriate in all cases. Furthermore, the discussions (and many of the examples) in the book assume that you are using the normal compilation process of invoking javac to compile a source file into bytecode and then using java to run that bytecode. This is the mechanism that is used for real-world development, and understanding this process is an important part of learning Java. It is imperative that you are thoroughly familiar with it. For these reasons, when trying the examples in this book, it is strongly recommended that in all cases you use the normal approach to compiling and running a Java program. Doing so ensures that you have a solid foundation in the way Java works. Of course, you might find it fun to experiment with the single source-file launch option!

NOTE

It is possible to execute a single-file program from a file that does not use the .java extension. To do so, you must specify the --source APIVer option, where APIVer specifies the JDK version number.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.66.67