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 20. Edit - compile - execute Slide presentation Create comment in forum

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

Figure 21. Editing Java files Slide presentation Create comment in forum
Editing Java™ files

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 how to use an editor. A basic command is sufficient. Both nano and vi(m) are good choices on Linux/UNIX systems due to their widespread distribution. Windows and Apple users have different choices as well.

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


We shed some light on Java class definitions:

Figure 22. Defining class HelloWorld Slide presentation Create comment in forum
// Filename HelloWorld.java 

public class HelloWorld  {

  public static void main(String[] args)  {
    System.out.println("Hello, world"); 
  }

}

The text file HelloWorld.java contains a class definition. Classes are Java's main building blocks. Real world Java applications consist of up to thousands of interacting classes.

... public class HelloWorld ... actually defines a so called Java class of name HelloWorld. We will refer to it as class HelloWorld for short. You'll learn about classes starting from Objects and Classes. The following restrictions apply:

  1. Class names must obey Java rules for so called identifiers. Spaces, operators like + and a lot of other characters are disallowed. More details are to be found in Figure 111, “Variable names (identifier) ”.

  2. A Java file's base name must match the primary (the one being defined using the public modifier) class name it contains. In the given example the file's base name is HelloWorld. Defining e.g. ... public class Hello {... inside a file HelloWorld.java would be flagged as a compile time error.

public static void main(String[] args) is a so called class method definition.

For the time being we present a rule of thumb: Every Java program to become executable requires at least one class containing (nearly) exactly the following main(..) method definition:

public static void main(String[] args)

A 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 23. Compiling Java file Slide presentation Create comment in forum
Compiling Java™ file

The text file HelloWorld.java containing our HelloWorld class.

The Java compiler is a piece of software belonging to a JDK. It will read the input filename HelloWorld.java and create a resulting output filename HelloWorld.class.

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

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


Figure 24. Shell compiling Java file Slide presentation Create comment in forum
~/tmp$ ls -al HelloWorld.class 
ls: cannot access 'HelloWorld.class': No such file or directory

~/tmp$ javac HelloWorld.java 

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

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

Figure 25. Java bytecode file Slide presentation Create comment in forum
Êþº¾^@^@^@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^@^OHelloWorld.java^L^@^G^@^H^G^@^W^L^@^X^@^Y^A^@^LHello, world^G^@^Z^L^@^[^@\
  ^\^A^@
HelloWorld^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 26. Sourcecode vs. bytecode Slide presentation Create comment in forum
HelloWorld.java HelloWorld.class
  • Human readable (kind of 😆).

  • High abstraction level.

  • Text file

  • Machine readable instructions.

  • Non-editable.

  • Binary file.


Figure 27. Execute Java bytecode file Slide presentation Create comment in forum
Execute Java™ bytecode file

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

java HelloWorld

Caution

The .class extension must be omitted! Trying java HelloWorld.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 preconnected 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 28. Shell Java bytecode file execution Slide presentation Create comment in forum
> java HelloWorld 
Hello, world

Figure 29. JDK installation options Slide presentation Create comment in forum

Extending your vendor's set of repositories to be searched for software packages.

Refresh software package metadata descriptions.

The package oracle-java10-installer contains a JDK of version 10.

The package oracle-java10-set-default is optional: In case of multiple JDK being installed on your system this one will become the default.

Updates will be installed by usual package updates:

apt-get update 
apt-get upgrade