Writing and Executing a Java Program

De la WikiLabs
Versiunea din 22 octombrie 2013 08:59, autor: Rhobincu (discuție | contribuții) (Pagină nouă: 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. T...)
(dif) ← Versiunea anterioară | Versiunea curentă (dif) | Versiunea următoare → (dif)
Jump to navigationJump to search

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

În consolă, se navighează folosind comenzile prezentate mai sus până în directorul în care a fost salvat fișierul sursă. Apoi, folosind executabilul javac (Java Compiler), se compilează:

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


Regulă: Întotdeauna, compilatorul Java va lua ca argument numele fișierului (cu tot cu extensie) care trebuie compilat.

Dacă execuția compilatorului nu a afișat nici o eroare, atunci compilarea s-a încheiat cu succes și a fost generat fișierul executabil pentru mașina virtuală, cu extensia .class.

Dacă compilatorul nu a putut fi găsit, atunci verificați variabila de sistem PATH, conform instrucțiunilor de instalare pentru JDK.

Dacă compilatorul a afișat erori, citiți-le cu atenție și reparați problemele din codul sursă Java.

Execuția programului în mașina virtuală

În locul unde s-a compilat codul sursă, folosind executabilul java (mașina virtuală), se execută programul:

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


Regulă: Întotdeauna, mașina virtuală Java va lua ca argument numele clasei (fără extensie) care trebuie executat.


Regulă: O clasă Java nu poate fi rulată în mașina virtuală decât dacă conține metoda main, exact cum este defintă în specificație: public static void main(String[])