Raspberry Pi_Eng_23.7.3 Writing and Running Java language Program


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


23.7.3  Writing and Running Program

 

23.7.3.1    Writing Program Source Code

 

A typical Java program usually has the following form. Class is defined in the first line of program, and "main" function corresponds to the body of program.

 

public class ClassName

{

    public static void main(String args[])

    {

 

 

    }

}

 

Here we will use a simple printing program. We will write and use the following program code. The class name was defined as "HelloWorld".

 

public class HelloWorld

{

    public static void main(String args[])

    {

 

        java.lang.System.out.println("Hello World.");

       java.lang.System.out.println("This is test program.");

 

    }

}

 

The created program is saved as a file. A source program written in Java language is stored in a file where file name is same as class name and extension is ".java". We will store it in the "~/program_test/ HelloWorld.java" file. Now, if you check the folder contents where the file is located, it will show the results as follows.

 

pi@raspberrypi ~ $ cd program_test
pi@raspberrypi ~/program_test $ ls l
-rw-r--r-- 1 pi pi  152 May  9 13:21 HelloWorld.java

 

23.7.3.2    Compiling and Running Program

 

   Compiling program and creating class file

 

To run a program that has been completely writed, you must compile the program and create a class file. To do this, use "javac" command.

 

[Command Format]

javac   [option]     <source-file>   

 

[Command Overview]               

    This compiles a program and creates a class file.

    User privilege           -- Normal user.

 

[Detail Description]

    None

 

[Used Example]

Let's compile the program that is written. If there is an error in the program, error will be displayed as follows.

 

pi@raspberrypi ~/program_test $ javac HelloWorld.java
HelloWorld.java:5: error: unclosed string literal
        java.lang.System.out.println("Hello World.);
                                     ^
HelloWorld.java:5: error: ';' expected
        java.lang.System.out.println("Hello World.);
                                                    ^
HelloWorld.java:6: error: illegal start of expression
        java.lang.System.out.println("This is test program.");
            ^
HelloWorld.java:6: error: ';' expected
        java.lang.System.out.println("This is test program.");
                 ^
4 errors

 

If there is no error in the developed program code, no message is output and a class file is created. The following is confirming the result.

 

pi@raspberrypi ~/program_test $ ls l
-rw-r--r-- 1 pi pi  448 May  9 13:09 HelloWorld.class
-rw-r--r-- 1 pi pi  152 May  9 13:21 HelloWorld.java

 

 

   Executing class program

 

Use the following command to execute with class file.

 

[Command Format]

java   [option]     <class-file-name>   

 

[Command Overview]               

    This runs a program using class file.

    User privilege           -- Normal user.

 

[Detail Description]

    None

 

[Used Example]

The class file created above is "HelloWorld.class". Now let's run the program.

 

pi@raspberrypi ~/program_test $ java HelloWorld
Hello World.
This is test program. 

 

You can see that the program is normally operated and printing commands are normally processed.