Writing and Executing a Java Program

De la WikiLabs

In order to develop a Java program, there are two operations that need to be performed: the compilation of a source code and the execution of the program inside the virtual machine. The source code can be written in any text editor. For Microsoft Windows, a good suggestion is Notepad++. For Linux, depending on the X interface, you can use either Kate for KDE or gedit for Gnome/ Unity/ Cinnamon. During this lab, we will be using Kate. You can run it by clicking Applications -> Accessories -> Kate.

Command Line Tutorial

Before compiling and executing a Java program, we will need a short introduction for using the console in both MS Windows and Linux.

MS Windows cmd

In Windows, the console is stated by pressing "winkey + r", then typing cmd. It looks like this:

Consola sistemului de operare Windows

A few useful commands:

  • if the files are on another partition, like D, or E, the command that changes the path to the D partition, for example, is:
c:\Users\Echo> d:
d:\>
  • for changing the current directory, the cd command is used (change directory):
c:\Users\Echo> cd Documents
c:\Users\Echo\Documents> cd work
c:\Users\Echo\Documents\work> cd ..
c:\Users\Echo\Documents>
  • for displaying the content of the current directory, you can use the dir command:
C:\Users\Echo\Documents\work\poo> dir
 Volume in drive C has no label.
 Volume Serial Number is 9C96-4AC6

 Directory of C:\Users\Echo\Documents\work\poo

18-Jul-12  10:42    <DIR>          .
18-Jul-12  10:42    <DIR>          ..
18-Jul-12  10:43               117 MainClass.java
               1 File(s)            117 bytes
               2 Dir(s)  22,145,564,672 bytes free

C:\Users\Echo\Documents\work\poo>
  • for creating a new directory, use md (make directory):
C:\Users\Echo\Documents\work\poo> md test

C:\Users\Echo\Documents\work\poo> dir
 Volume in drive C has no label.
 Volume Serial Number is 9C96-4AC6

 Directory of C:\Users\Echo\Documents\work\poo

18-Jul-12  10:45    <DIR>          .
18-Jul-12  10:45    <DIR>          ..
18-Jul-12  10:43               117 MainClass.java
18-Jul-12  10:45    <DIR>          test
               1 File(s)            117 bytes
               3 Dir(s)  22,145,433,600 bytes free

C:\Users\Echo\Documents\work\poo>

Linux Bash

In Linux, depending on the distribution and interface, you can start the terminal by either pressing "Alt + Ctrl + T", or by selecting Terminal from Applications -> Accessories:

Consola din Linux

A few useful commands:

  • to display the current directory, use pwd (print working directory):
student@olympus07:~$ pwd 
/home/student
student@olympus07:~$
  • for changing the current directory, the cd command is used (change directory):
student@olympus07:~$ cd work
student@olympus07:~/work$ pwd 
/home/student/work
student@olympus07:~/work$ cd ..
student@olympus07:~$ pwd 
/home/student
student@olympus07:~$
  • for displaying the content of the current directory, you can use the ls -l command (list structure, long listing format):
student@olympus07:~/work/poo$ ls -l
total 4
-rw-rw-r-- 1 student student 110 Jul 18 11:08 MainClass.java
student@olympus07:~/work/poo$
  • to create a new directory, use mkdir (make directory):
student@olympus07:~/work/poo$ mkdir test
student@olympus07:~/work/poo$ ls -l
total 8
-rw-rw-r-- 1 student student  110 Jul 18 11:08 MainClass.java
drwxrwxr-x 2 student student 4096 Jul 18 11:10 test
student@olympus07:~/work/poo$

Writing, Compiling and Executing a Java Program

Editing the Source Code

The first step in writing a Java program, is editing the source code. For this, use your favorite editor and save the file with a .java extension.

Rule: A Java source code will always have a .java extension.


Rule: The name of a class contained in a source file must always have the same identical name with the file. I.e.: The class MainClass must be saved in a file named MainClass.java. Attention: Java is case-sensitive!!!

For example, we will write the following class:

public class MainClass{

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

}

This class must be saved in a file called MainClass.java.

Compiling the Source

Open a console, navigate to the directory where the source file has been saved. Then, use the javac executable (Java Compiler) to compile the code:

student@olympus07:~/work/poo$ javac MainClass.java
student@olympus07:~/work/poo$ ls -l
total 8
-rw-rw-r-- 1 student student 424 Jul 18 11:22 MainClass.class
-rw-rw-r-- 1 student student 110 Jul 18 11:08 MainClass.java


Rule: The Java compiler will always take as argument the file name (extension included) which needs to be compiled.

If the compilation did not display any message, then it has completed successfully and the executable file for the virtual machine has been created (.class extension).

If the compiler could not be found, then check the PATH environment variable as per the JDK installation instructions.

If the compiler displayed error messages, then read them carefully and fix the Java source code problems.

Program Execution Inside the Virtual Machine

In the directory where the source code has been compiled, using the java executable (the virtual machine), the program is executed:

student@olympus07:~/work/poo$ java MainClass
Hello world!
student@olympus07:~/work/poo$


Rule: The JVM will always take as an argument the name of the class (without the extension) which needs to be executed.


Rule: A Java class can only be ran in the virtual machine if it contains, as a member, the main method, exactly how it is defined in the specification: public static void main(String[])