3 - Running a Java Program

aniseedsplashΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 4 χρόνια και 10 μήνες)

236 εμφανίσεις


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Running a Java Program

Source Code Stage


Byte Code Stage

Executable Stage

The JRE, JVM and JDK

Platform Independence

Lesson 2


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
2



Process 1
-

Writing the Source Code



Before we continue learning the basic syntax of the language we’ll
be having a pause to understand the stages that our Java program
goes through from the moment it is coded until it is run.




Remember our Dog/Bone example in the previous lesson.




The first step to creating this program was to
code

it. This is the first
step in the process. The output is
source code
(a file with
.java
extension). We can use any editor of our taste to create source code.

Coding

(Using an editor
e.g.
NetBeans
)

Source Code

DogsBones
.java


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
3



Process 1
-

Writing the Source Code (
NetBeans
)

In NetBeans, the source files are shown in the Projects Window.

In the file system, we can navigate to

<project root>
\
src
\
mygreatapp
\

Here we can see all the source files that the project MyGreatApp has.


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
4



Process 2


Compiling the Source Code



The source code is a language for us humans. It is English
-
like so
that it would not be so difficult to program. Before this code is run it
needs to be converted to other languages.




First, the source code is converted into
byte code
. This process is
done by the Java Compiler (a program called
javac.exe
). The output
file, containing the byte code, has a
.class
extension.


Coding

(Using an editor
e.g.
NetBeans
)

Source Code

DogsBones
.java

Compilation

(using javac.exe)


ByteCode

DogsBones
.class


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
5



Process 2
-

Compiling the Source Code (
NetBeans
)

In NetBeans, once we finish our source
code we can right
-
click on the project and
click
Build
. When we do this, NetBeans will
invoke the compiler (javac.exe) to compile
the source code into class files.


Note that when we
Run

a project straight
away, this process is done automatically.

To find the compiled files we can
navigate to:

<project root>
\
build
\
classes
\
mygreatapp
\


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
6



Process 3


Interpreting the Byte Code



The byte code is not the type of code which is actually run. There’s
another final process involved.




The byte code is
interpreted
by the Java Interpreter (a program
called
java.exe
). The output file is executable code, this time in a
language that the processor (definitely not humans!) can understand.
This output can then be executed.


Coding

(Using an editor
e.g.
NetBeans
)

Source Code

DogsBones
.java

Compilation

(using javac.exe)


ByteCode

DogsBones
.class

Interpretation

(using java.exe)


Executable
Code


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
7



Process 3


Interpreting the Byte Code (
NetBeans
)

In NetBeans, we can finally click on
Run/Run File

in order to run the program.
NetBeans will feed the class file into
java.exe for interpretation and to the JVM
for execution.


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
8



Interpreter and Compiler



Note that in the described processes we have a compilation stage
and an interpretation stage. You do not need to understand the
difference between compilation and interpretation. Just understand
that they are
2 different ways of how one type of code can be
translated/converted into another
.


Coding

(Using an editor
e.g.
NetBeans
)

Source Code

DogsBones
.java

Compilation

(using javac.exe)


ByteCode

DogsBones
.class

Interpretation

(using java.exe)


Executable
Code


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
9



Platform Independency



The process described in the previous slide explains why we say
that a program written in Java is said to be
platform independent
.




When a developer creates a program, and wants to distribute his
application, he
distributes the .class
(compiled) files. This means that
Process 1 and 2 (writing of source code and compilation) occur on
the developer’s machine, while Process 3 (interpretation) occurs on
the machine of the user of the software.



Coding

(Using an editor
e.g.
NetBeans
)

Source Code

DogsBones
.java

Compilation

(using javac.exe)


ByteCode

DogsBones
.class

Interpretation

(using java.exe)


Executable
Code


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
10



The Java Runtime Environment (JRE)

Before running the software written in Java, the user has to ensure
that on his machine there’s an installation of the JRE (Java Runtime
Environment). The interpreter will convert the byte code to
executable code, which will then be run on the machine.

The
JRE contains:



the
interpreter

(java.exe)



the
JVM

(JAVA Virtual Machine) within which the interpreted code is
run



the
API

classes (the standard Java classes, e.g. String, JOptionPane…)



JDK

Coding

(Using an editor
e.g.
NetBeans
)

Source Code

DogsBones
.java

Compilation

(using javac.exe)


ByteCode

DogsBones
.class

Interpretation

(using java.exe)


Executable
Code

JRE (java.exe + JVM + API)


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
11



The Java Virtual Machine (JVM)


The JVM is
platform dependent
, i.e. we have a different one for each
of the platforms (Mac, Linux, Windows, etc…). It must be platform
dependent because its output is machine dependent (executable) code.


Therefore a program written in Java is
platform independent
because
it can be run on any platform. That is, any platform that has a JVM
written for it.



We can term this as “
write once and run everywhere
”.



Disadvantage: Interpretation is done
every time
the program runs!



JDK

Coding

(Using an editor
e.g.
NetBeans
)

Source Code

DogsBones
.java

Compilation

(using javac.exe)


ByteCode

DogsBones
.class

Interpretation

(using java.exe)


Executable
Code

JRE (java.exe + JVM + API)


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
12



Java


Platform Independency


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
13

What have you learnt today?

Questions


MCAST ICT 2011
-

OOP in Java (1HND Software & Networking)


Slide
14

References

http://www.roseindia.net/java/java
-
introduction/javatools/java
-
compiler.shtml


http://www.javabeat.net/qna/67
-
wat
-
is
-
the
-
difference
-
between
-
jrejvm
-
and
-
jdk/


http://www.java.com/en/download/faq/techinfo.xml


http://www.javabeginner.com/learn
-
java/introduction
-
to
-
java
-
programming