How can the answer be improved?
< Java Programming
Java Programming Compilation |
Navigate Getting Started topic: ( v•d•e )
|
In Java, programs are not compiled into executable files; they are compiled into bytecode (as discussed earlier), which the JVM (Java Virtual Machine) then executes at runtime. Java source code is compiled into bytecode when we use the
javac
compiler. The bytecode gets saved on the disk with the file extension .class
. When the program is to be run, the bytecode is converted, using the just-in-time (JIT) compiler. The result is machine code which is then fed to the memory and is executed.Java code needs to be compiled twice in order to be executed:
- Java programs need to be compiled to bytecode.
- When the bytecode is run, it needs to be converted to machine code.
The Java classes/bytecode are compiled to machine code and loaded into memory by the JVM when needed the first time. This is different from other languages like C/C++ where programs are to be compiled to machine code and linked to create an executable file before it can be executed.
Quick compilation procedure[edit]
To execute your first Java program, follow the instructions below:
1. | Proceed only if you have successfully installed and configured your system for Java as discussed here. | ||
2. | Open your preferred text editor — this is the editor you set while installing the Java platform. | ||
For example, Notepad or Notepad++ on Windows; Gedit, Kate or SciTE on Linux; or, XCode on Mac OS, etc. | |||
3. | Write the following lines of code in a new text document:
| ||
4. | Save the file as HelloWorld.java — the name of your file should be the same as the name of your class definition and followed by the .java extension. This name is case-sensitive, which means you need to capitalize the precise letters that were capitalized in the name for the class definition. | ||
5. | Next, open your preferred command-line application. | ||
For example, Command Prompt on Windows; and, Terminal on Linux and Mac OS. | |||
6. | In your command-line application, navigate to the directory where you just created your file. If you do not know how to do this, consider reading through our crash courses for command-line applications for Windows or Linux. | ||
7. | Compile the Java source file using the following command which you can copy and paste in if you want:
| ||
8. | Once the compiler returns to the prompt, run the application using the following command:
| ||
9. | The above command should result in your command-line application displaying the following result:
|
- Ask for help if the program did not execute properly in the Discussion page for this chapter.
Automatic Compilation of Dependent Classes[edit]
In Java, if you have used any reference to any other java object, then the class for that object will be automatically compiled, if that was not compiled already. These automatic compilations are nested, and this continues until all classes are compiled that are needed to run the program. So it is usually enough to compile only the high level class, since all the dependent classes will be automatically compiled.
Main class compilation |
However, you can't rely on this feature if your program is using reflection to create objects, or you are compiling for servlets or for a 'jar', package. In these cases you should list these classes for explicit compilation.
Main class compilation |
Packages, Subdirectories, and Resources[edit]
Each Java top level class belongs to a package (covered in the chapter about Packages). This may be declared in a
package
statement at the beginning of the file; if that is missing, the class belongs to the unnamed package.For compilation, the file must be in the right directory structure. A file containing a class in the unnamed package must be in the current/root directory; if the class belongs to a package, it must be in a directory with the same name as the package.
The convention is that package names and directory names corresponding to the package consist of only lower case letters.
Top level package[edit]
A class with this package declaration
Code section 2.1: Package declaration |
has to be in a directory named
example
.Subpackages[edit]
A class with this package declaration
Code section 2.2: Package declaration with sub-packages |
has to be in a directory named
en
which has to be a sub-directory of wikibooks
which in turn has to be a sub-directory of org
resulting in org/wikibooks/en
on Linux or orgwikibooksen
on Windows.Java programs often contain non-code files such as images and properties files. These are referred to generally as resources and stored in directories local to the classes in which they're used. For example, if the class
com.example.ExampleApp
uses the icon.png
file, this file could be stored as /com/example/resources/icon.png
. These resources present a problem when a program is compiled, because javac
does not copy them to wherever the .class
files are being compiled to (see above); it is up to the programmer to move the resource files and directories.Filename Case[edit]
The Java source file name must be the same as the public class name that the file contains. There can be only one public class defined per file. The Java class name is case sensitive, as is the source file name.
The naming convention for the class name is for it to start with a capital letter.
Compiler Options[edit]
Debugging and Symbolic Information[edit]
To do: Complete this section. |
To debug into Java system classes such as String and ArrayList, you need a special version of the JRE which is compiled with 'debug information'. The JRE included inside the JDK provides this info, but the regular JRE does not. Regular JRE does not include this info to ensure better performance.
Symbolic Information : Symbolic resolution is done at class loading time at linking resolution step.It is the process of replacing symbolic references from the type with direct references. It is done by searching into method area to locate the referenced entity
The JIT compiler[edit]
The Just-In-Time (JIT) compiler is the compiler that converts the byte-code to machine code. It compiles byte-code once and the compiled machine code is re-used again and again, to speed up execution. Early Java compilers compiled the byte-code to machine code each time it was used, but more modern compilers cache this machine code for reuse on the machine. Even then, java's JIT compiling was still faster than an 'interpreter-language', where code is compiled from high level language, instead of from byte-code each time it was used.
The standard JIT compiler runs on demand. When a method is called repeatedly, the JIT compiler analyzes the bytecode and produces highly efficient machine code, which runs very fast. The JIT compiler is smart enough to recognize when the code has already been compiled, so as the application runs, compilation happens only as needed. As Java applications run, they tend to become faster and faster, because the JIT can perform runtime profiling and optimization to the code to meet the execution environment. Methods or code blocks which do not run often receive less optimization; those which run often (so called hotspots) receive more profiling and optimization.
Java Programming Compilation |
Retrieved from 'https://en.wikibooks.org/w/index.php?title=Java_Programming/Compilation&oldid=3538101'
The javac command in Java compiles a program from a command prompt. It reads a Java source program from a text file and creates a compiled Java class file. The basic form of the javac command is
For example, to compile a program named HelloWorld.java, use this command:
Normally, the javac command compiles only the file that you specify on the command line, but you can coax javac into compiling more than one file at a time by using any of the following techniques:
- If the Java file you specify on the command line contains a reference to another Java class that’s defined by a java file in the same folder, the Java compiler automatically compiles that class, too.
- You can list more than one filename in the javac command. The following command compiles three files:
- You can use a wildcard to compile all the files in a folder, like this:
- If you need to compile a lot of files at the same time but don’t want to use a wildcard (perhaps you want to compile a large number of files but not all the files in a folder), you can create an argument file, which lists the files to compile. In the argument file, you can type as many filenames as you want, using spaces or line breaks to separate them. Here’s an argument file named TestPrograms that lists three files to compile:You can compile all the programs in this file by using an @character, followed by the name of the argument file on the javac command line, like this:
The javac command has a gaggle of options that you can use to influence how it compiles your programs.
Option | Description |
---|---|
-bootclasspath <path> | Overrides locations of bootstrap class files. (The bootstrap class files are the classes that implement the Java runtime. You will rarely use this option.) |
-classpath <path> | Specifies where to find user class files. Use this option if your program makes use of class files that you’ve stored in a separate folder. |
-cp <path> | Same as classpath. |
-d <directory> | Specifies where to place generated class files. |
-deprecation | Outputs source locations where deprecated APIs (features that are considered obsolete) are used. Use this option if you want the compiler to warn you whenever you use API methods that have been deprecated. |
-encoding <encoding> | Specifies character encoding used by source files. |
-endorseddirs <dirs> | Overrides location of endorsed standards path. |
-extdirs <dirs> | Overrides locations of installed extensions. |
-g | Generates all debugging info. |
-g:{lines,vars,source} | Generates only some debugging info. |
-g:none | Generates no debugging info. |
-help | Prints a synopsis of standard options. |
-J<flag> | Passes <flag> directly to the runtime system. |
-nowarn | Generates no warnings. |
-source <release> | Provides source compatibility with specified release. |
-sourcepath <path> | Specifies where to find input source files. |
-target <release> | Generates class files for specific virtual machine version. |
-verbose | Outputs messages about what the compiler is doing. |
-version | Provides version information. |
M | Prints a synopsis of nonstandard options. |
A class file is a compiled Java program that can be executed by the java command. The Java compiler reads source files and creates class files.
To use one or more of these options, type the option before or after the source filename. Either of the following commands, for example, compiles the HelloApp.java file with the -verbose and -deprecation options enabled: