Java Virtual Machine

Java and the Virtual Machine

  • Many programming tools, like C++, need to compile the source code into an intermediary form, object-file. This process is called compiling. This object-file will then be linked with other object-files into an executable program with help of a Linker program.
  • Other types of programming tools, such as JavaScript, called an interpreter, meaning that the compile and execution is done at the same time as the program reads the source code.
  • Java is both a compiled and an interpreted programming language.
  • Java source code files are compiled into a format called bytecode, which can then be executed by a Java interpreter.
  • The interpreter can be run as a separate program, or it can be embedded in another software, such as a browser.
  • The compiled bytecode can run on most computers because Java interpreters and runtime environments, known as Java Virtual Machines (VMs), exist in most operating systems, including UNIX, the Macintosh OS, and Windows.

Compiling and Running Java Programs:

  • Java JDK is a free software that can be downloaded from Oracle's website.
  • Inside the JDK-software you will find, among others, the following tools:
    Tools Use
    javac Java compiler, creates bytecode
    java Java interpreter, used to run compiled program
    appletviewer Used for displaying the applet as it would be seen by the browser
    jdb Java debugger
    javap Decompiler
    jar Create jar files that contains a lot of bytecode files
    javadoc Documentation generator
  • The process of running a java application are at least:
    1. Create all Java source files where each file has a name with the .java extension.
    2. Each Java source file must define at least a public class or interface with the same name as the file name (exclusive the extension).
    3. If you are building a Java Application one of the Java source files must include a main() method which will bee the start point for your program.
    4. We must then compile the Java source files with the javac tool to create .class files, which results in bytecode files.
    5. A .class file will have the same name as the name of corresponding .java file.
    6. At last we will run the program with the java interpreter.
    Here is a program example:
    // my first program in Java   (this is a comment)
    public class HelloJava {
          // Almost everything in Java is
          // contained in a class of some kind
      public static void main( String[] args ) {
          // The main function where all Java applications start their execution 
        System.out.println("Hello, Java! ");
          // System consol output in Java with new line 
    // { }  are the braces which delineate a sequence of program codes. 

The Java compiler

Javac reads .java files and compile classes and interfaces contained in these files to bytecode files with a .class extension
usage of javac:
 javac [ options ] [ sourcefiles ] [ classes ] [ @argfiles ] 
Command-line options (see table below).
One or more source files to be compiled (all separated with space)
One or more classes to be processed for annotations.
One or more files that list options and source files. The -J options are not allowed in these files.
Here is some of the options:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings
  -verbose                   Output messages about what the compiler is doing
  -deprecation               Output source locations where deprecated APIs are used
  -classpath <path>          Specify where to find user class files
  -cp <path>                 Specify where to find user class files
  -sourcepath <path>         Specify where to find input source files
  -bootclasspath <path>      Override location of bootstrap class files
  -extdirs <dirs>            Override location of installed extensions
  -endorseddirs <dirs>       Override location of endorsed standards path.
  -d <directory>             Specify where to place generated class files.
                                   The directory must already exist;
  -encoding <encoding>       Specify character encoding used by source files
  -source <release>          Provide source compatibility with specified release
  -target <release>          Generate class files for specific VM version
  -version                   Version information
  -help                      Print a synopsis of standard options
  -X                         Print a synopsis of nonstandard options
  -J<flag>                   Pass <flag> directly to the runtime system
You will find more options here
This example will places the class file into the directory " ../build/classes":
 >javac -d ../build/classes

The Java interpreter

The java command starts a Java application. It does this by starting the Java Runtime Environment (JRE), loading the specified class, and calling that class's main() method.
usage of javac:
java [options] classname [args]  (to execute a class)
       or  java [options] -jar filename [args]  (to execute a jar file)
       or  javaw [options] classname [args]  (to execute a class)
       or  javaw [options] -jar filename [args]  (to execute a jar file)
Command-line options separated by spaces (see table below).
Command-line options separated by spaces (all separated with space)
The name of the Java Archive (JAR) file to be called. Used only with the -jar option..
The name of the Java Archive (JAR) file to be called. Used only with the -jar option..
The javaw command is identical to java , except that with javaw there is no associated console window. Use javaw when you do not want a command prompt window to appear.
Here is some of the options:
  -cp                     <class search path of directories and zip/jar files>
  -classpath              <class search path of directories and zip/jar files>
                          A ; separated list of directories, JAR archives,
                          and ZIP archives to search for class files.
  -D<name>=<value>        set a system property
  -verbose[:class|gc|jni] enable verbose output
  -version                print product version and exit
  -version:<value>        require the specified version to run
  -showversion            print product version and continue
  -? -help                print this help message
  -X                      print help on non-standard options
You will find more options here
This example will start your, HelloJava, application in the directory " ../build/classes" :
 >java –cp ../build/classes HelloJava
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.