Using plain Java

We outline the process of:

  1. Editing source code.

  2. Compile Java source code thereby generating so called class files.

  3. Executing class files by virtue of a piece of software being called the Java Virtual Machine.

    Main idea: Write once, run everywhere.

Figure 31. Edit - compile - execute Slide presentation

We now examine these three steps in greater detail starting with editing source code:

The process of creating a text file from scratch or editing an existing one is called editing. It requires a text editor like nano, vi(m), atom or similar.

Tip

You should learn quickly using a text editor of your choice. A basic command is sufficient. Both nano and vi(m) are good choices on Linux/UNIX and Apple systems due to their widespread distribution. Windows users have different choices like e.g. Notepad++.

The resulting files contain just plain text. Unlike office documents they won't contain any layout or formatting like e.g. different font sizes or background colours.

We shed some light on Java class definitions:

Figure 32. Defining class Hello Slide presentation
// Filename HelloWorld.java 
void main()  {
    IO.println("Hello world"); 
}

The text file Hello.java contains an executable main(...) method.

void main() is a so called class method's definition.

For the time being we present a rule of thumb: Every Java program to become executable requires at least one class containing this or a similar void main() definition as an entry point for execution.

A print statement example. This is our toy example's actual payload: Only this line of code has an observable effect on execution. All surrounding text is just boilerplate code being required to keep the Java environment happy.


Prior to execution we require a compilation step:

Figure 33. Compiling Java file Slide presentation
Compiling Java™ file Sourcecode Java compiler Executable bytecode file

The text file Hello.java containing our main() method.

The Java compiler is a piece of software belonging to a JDK. It will read the input filename Hello.java and generate a corresponding byte code output file Hello.class from it.

A Java bytecode file contains instructions to be interpreted by a Java run time system (JRE), see next step. Byte code files are not meant to be viewed or edited.

The main advantage of bytecode files is becoming independent of operating systems. Thus a given Hello.class file may be executed on Linux, Windows, Mac-OS or any other JRE providing system in exactly the same way.


Figure 34. Command line Java file compilation Slide presentation
~/tmp$ ls -al Hello.class 
ls: cannot access 'Hello.class': No such file or directory

~/tmp$ javac Hello.java 

~/tmp$ ls -al Hello.class 
-rw-r--r-- 1 goik fb1prof 419 Sep 23 15:44 Hello.class

Besides human readable portions bytecode files mainly contain binary data providing instructions to be executed:

Figure 35. Java byte code file Hello.class Slide presentation
Êþº¾^@^@^@6^@^]
^@^F^@^O        ^@^P^@^Q^H^@^R
^@^S^@^T^G^@^U^G^@^V^A^@^F<init>^A^@^C()V^A^@^DCode^A^@^OLineNumberTable^A^@^Dmain^A^@^V(\
  [Ljava/lang/String;)V^A^@
SourceFile^A^@^OHello.java^L^@^G^@^H^G^@^W^L^@^X^@^Y^A^@^LHello, world^G^@^Z^L^@^[^@\
  ^\^A^@
Hello^A^@^Pjava/lang/Object^A^@^Pjava/lang/System^A^@^Cout^A^@^ULjava/io/PrintStream;\
  ^A^@^Sjava/io/PrintStream^A^@^Gprintln^A^@^U(Ljava/lang/String;)V^@!^@^E^@^F^@^@^@^@^@^\
  B^@^A^@^G^@^H^@^A^@    ^@^@^@^]^@^A^@^A^@^@^@^E*·^@^A±^@^@^@^A^@
^@^@^@^F^@^A^@^@^@^B^@  ^@^K^@^L^@^A^@  ^@^@^@%^@^B^@^A^@^@^@   ²^@^B^R^C¶^@^D±^@^@^@^A^@
^@^@^@
^@^B^@^@^@^D^@^H^@^E^@^A^@^M^@^@^@^B^@^N

Figure 36. Source code vs. bytecode Slide presentation
Hello.java Hello.class
  • Human readable (kind of 😆).

  • High abstraction level.

  • Text file

  • Machine readable instructions.

  • Non-editable (usually).

  • Binary file.


Figure 37. Executing byte code file Hello.class Slide presentation
Executing byte code file Hello.class

Passing a bytecode file to the Java Runtime System (JRE):

java Hello

Caution

The .class extension must be omitted! Trying java Hello.class will yield an error.

The bytecode input will pass various steps inside the JRE. On successful processing the underlying operating system will eventually execute the desired actions on behalf of the JRE.

Terminal and keyboard are being linked to the operating system by two pre - connected communication channels called standard input and standard output respectively. In our toy example System.out represents the standard output channel and thus allows for execution of println("Hello, world").


Figure 38. Command line byte code file Hello.class execution Slide presentation
> java Hello
Hello, world

Remark: This executes Hello.class, not Hello.java.


Figure 39. Wrap up: The edit - compile - execute cycle Slide presentation
  • Edit your code using a text editor resulting in a file Hello.java:

    void main() {
       IO.println("Hello world");
    }
  • Compile to byte file Hello.class:

    javac Hello.java
  • Execute Hello.class using your Java virtual machine (JVM):

    java Hello
    Hello world