Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
COS240 O-O Languages
AUBG, COS dept
Lecture 10b
Title:
Running Programs & IDEs
(Java)
Reference: COS240 Syllabus
5/5/2017
Assoc. Prof. Stoyan Bonev
1
Lecture Contents:
• Java applications classified
• Processing Java source texts
– How Java works
– IDEs to run Java
5/5/2017
Assoc. Prof. Stoyan Bonev
2
Java applications classified
Java programs may be applications or applets.
Applications are standalone programs, similar to .NET
Console and Windows applications.
Applets are similar to applications, but they do not run as
standalone programs.
- Instead, applets adhere to a set of conventions that
lets them run within a Java-compatible browser (clientside).
- You can only run an applet from an HTML page.
5/5/2017
Assoc. Prof. Stoyan Bonev
3
Java, C#, C++ - High Level PL
• In order to run /to execute/, any program
written in HLL should be transformed to
executable form
• Three ways to convert source code into
executable form:
– Compiler – generate object code
– Interpreter – interpret the source code
– Compiler of semi-interpreting type
5/5/2017
Assoc. Prof. Stoyan Bonev
4
data
Source
program
Object
program
compiler
Compile time
5/5/2017
Assoc. Prof. Stoyan Bonev
Executing
computer
Results
Run time
5
Data
Interpreter
Source
program
Results
Compile time
Run time
5/5/2017
Assoc. Prof. Stoyan Bonev
6
data
Source
program
Object
program in
IL form
compiler
Compile1 time
Interpreter
Results
Compile2 time
Run time
5/5/2017
Assoc. Prof. Stoyan Bonev
7
Processing HLL programs
• Java is compiled to intermediate form – bytecode.
Bytecode processed by interpreter named JVM.
• C# and C++ (managed code) are compiled to
intermediate form – MSIL code. MSIL code
processed by interpreter named CLR.
• C++ (native, unmanaged code) is directly
compiled to executable native machine code. No
intermediate code, no need of interpreter.
5/5/2017
Assoc. Prof. Stoyan Bonev
8
How Java works
5/5/2017
Assoc. Prof. Stoyan Bonev
9
How Java Works
• Java's platform independence is achieved by the use of
the Java Virtual Machine (JVM).
• A Java program consists of one or more files with a
.java extension
• these are just text (i.e. source) files.
• When a Java program is compiled, the .java files are fed
to the compiler which produces a .class file for each
.java file.
• The .class file contains Java bytecode.
• Java bytecode is like machine language, but it is intended
for the Java Virtual Machine, not a specific processor.
5/5/2017
Assoc. Prof. Stoyan Bonev
10
11
Executing a Java program
12
JVM emulation run on a physical machine
13
JVM handles translations
14
Java Popularity
• Pure Java includes 3 software facilities:
• JRE
• JVM
• JDK
15
JRE
• The Java Runtime
Environment (JRE) provides
– the libraries,
– the Java Virtual Machine, and
– other components to run
applets and applications written
in Java.
16
JVM - Overview
• Java Virtual Machine is a program which executes certain
other programs, namely those containing Java bytecode
instructions.
• JVM is distributed along with Java Class Library, a set of
standard class libraries (in Java bytecode) that implement
the Java application programming interface (API). These
libraries, bundled together with the JVM, form the Java
Runtime Environment (JRE).
• JVMs are available for many hardware and software
platforms. The use of the same bytecode for all JVMs on all
platforms allows Java to be described as a write once, run
anywhere programming language, versus write once,
compile anywhere, which describes cross-platform compiled
languages.
• Oracle Corporation, the owner of the Java trademark,
produces the most widely used JVM, named HotSpot, that is
written in the C++ programming language.
17
JVM
• JVM architecture.
Source code is compiled
to Java bytecode, which
is verified, interpreted or
JIT-compiled for the
native architecture.
The Java APIs and JVM
together make up the
Java Runtime Environment
(JRE).
18
JDK contents
The JDK has
a collection
of programming tools,
including:
19
JDK contents
• java – the loader for Java applications. This tool is an
interpreter and can interpret the class files generated by the
javac compiler. Now a single launcher is used for both
development and deployment. The old deployment launcher,
jre, no longer comes with Sun JDK, and instead it has been
replaced by this new java loader.
• javac – the Java compiler, which converts source code into
Java bytecode
• javadoc – the documentation generator, which automatically
generates documentation from source code comments
• jar – the archiver, which packages related class libraries into
a single JAR file. This tool also helps manage JAR files.
20
A Simple Java Program
Listing 1.1
//This program prints message “Welcome, Java!“
// braces in new-line style
public class Welcome
{
public static void main(String[] args)
{
System.out.println("Welcome to Java!");
}
}
21
A Simple Java Program
Listing 1.1
//This program prints message “Welcome, Java!“
// braces in end-line style (recommended)
public class Welcome {
public static void main(String[] args) {
}
}
System.out.println("Welcome to Java!");
22
Creating, Compiling, and
Running Java Programs
Create/Modify Source Code
Source code (developed by the programmer)
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Byte code (generated by the compiler for JVM
to read and interpret, not for you to understand)
…
Method Welcome()
0 aload_0
…
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return
Saved on the disk
Source Code
Compile Source Code
i.e., javac Welcome.java
If compilation errors
stored on the disk
Bytecode
Run Byteode
i.e., java Welcome
Result
23
If runtime errors or incorrect result
Compiling Java Source Code
You can port a source program to any machine with appropriate
compilers. The source program must be recompiled, however,
because the object program can only run on a specific machine.
Nowadays computers are networked to work together. Java was
designed to run object programs on any platform. With Java, you
write the program once, and compile the source program into a
special type of object code, known as bytecode. The bytecode
can then run on any computer with a Java Virtual Machine, as
shown below. Java Virtual Machine is a software that interprets
Java bytecode.
Java Bytecode
Java Virtual
Machine
Any
Computer
24
Approaches to run Java
• How to Compile/Run Java Programs
– javac, java
• Java IDEs
– TextPad, jGRASP, BlueJ, NetBeans, Eclipse
• Java Development Tools
– javadoc, jar, jdb
Java Programming: From Problem Analysis to Program Design, 3e
25
Creating, Compiling & Running Java
From
the Command Window
From
within IDE
5/5/2017
Assoc. Prof. Stoyan Bonev
26
Creating, Compiling and Running
Java from Command Line Window
First: to open a Command Window
– Click Start, All Programs, Accessories, Command Prompt
Second: to create a separate directory/folder on C: drive or Q:
drive where to save .java and .class files
– md COS240Progs
– cd COS240Progs
Third: to Set path to JDK bin directory
– Already done by OCC
Fourth: to create and save Java source file
– Notepad/Write Welcome.java
Fifth: to Compile
– javac Welcome.java
Sixth: to Run
– java Welcome
Creating and Editing Using editors like
Notepad, WordPad, etc
To use NotePad, type
notepad Welcome.java
from the DOS prompt.
To use WordPad, type
write Welcome.java
from the DOS prompt.
5/5/2017
Assoc. Prof. Stoyan Bonev
28
Compiling, and Running Java Programs
In
order to compile your Java program, you
should type
javac Welcome.java
In
order to run your compiled to bytecode
Java program, you should type
java
Welcome
5/5/2017
Assoc. Prof. Stoyan Bonev
29
Compiling, and Running Java Programs
public class Welcome {
public static void main(String args[]) {
int i=1;
while (i<5) {
System.out.println("Greeting to Blagoevgrad!");
i++;
} // end of while loop
} // end of method main
} // end of Welcome class
5/5/2017
Assoc. Prof. Stoyan Bonev
30
Compiling and Running Java
from IDEs
Java
IDEs:
– TextPad
– jGRASP
– BlueJ
– NetBeans – Open source by Sun
– Eclipse – Open source by IBM
5/5/2017
Assoc. Prof. Stoyan Bonev
31
Compiling and Running Java
from TextPad
.
5/5/2017
Assoc. Prof. Stoyan Bonev
32
Compiling and Running Java from
TextPad
Edit the .java file
The edited file should be saved before the coming
commands to take effect.
Subjects of interest:
– Select tools, click run (user is expected to type
command and parameters)
– Command:javac
Parameter:Welcome.java
– Command:java
Parameter:Welcome
– Select view, click tool output
5/5/2017
Assoc. Prof. Stoyan Bonev
33
Compiling and Running Java from
TextPad
public class COS240 {
public static void main(String args[]) {
int i=1;
for (i=1; i<=5; i++)
System.out.println("Greeting from COS dept");
}
}
5/5/2017
Assoc. Prof. Stoyan Bonev
34
Compiling and Running Java
from jGRASP
5/5/2017
Assoc. Prof. Stoyan Bonev
35
Compiling and Running Java from jGRASP
Subjects of interest:
Select Build, Click Compile
Select Build, Click Run
Or
Select Build, Click Run as Applet
Or
Select Build, Click Run as Application
CheckBox:
5/5/2017
run in MS-DOS window
Assoc. Prof. Stoyan Bonev
36
Compiling and Running Java from
jGRASP
import java.util.Scanner;
public class COS240Proba {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int pom = cin.nextInt();
float pom2 = cin.nextFloat();
double pom3 = cin.nextDouble();
System.out.println(" " + pom + " " + pom2 + " " + pom3);
}
}
5/5/2017
Assoc. Prof. Stoyan Bonev
37
Compiling and Running Java
from BlueJ
5/5/2017
Assoc. Prof. Stoyan Bonev
38
Compiling and Running Java
from BlueJ
5/5/2017
Assoc. Prof. Stoyan Bonev
39
Compiling and Running
Java from NetBeans
5/5/2017
Assoc. Prof. Stoyan Bonev
40
Compiling and Running
Java from NetBeans
Subjects of interest:
The concept of a project
The user is expected:
To choose project (Categories: Java, Projects: Java application)
To name the project and to locate the project
To create Main class and to set as Main project
To type the Java source text
To select Run, click Run Main Project (F6)
Or
To select Run, click Build Main Project(F11), click Run Main Project (F6)
Or
To select Run, click Clean and Build Main Project(Shift+F11), click Run Main Project (F6)
5/5/2017
Assoc. Prof. Stoyan Bonev
41
Compiling and Running
Java from Eclipse
5/5/2017
Assoc. Prof. Stoyan Bonev
42
Compiling and Running
Java from Eclipse
Subjects
of interest:
The concept of a project
Compilation activated when File_save activity
take place
List of steps to follow:
5/5/2017
Assoc. Prof. Stoyan Bonev
43
Compiling and Running Java from Eclipse
Open the Java perspective:
– In main menu select Window > Open Perspective > Java
Create a Java project
– In main toolbar Click New Java Project btn, Enter HelloWorld for
name, click Finish
Create your Helloworld class
– In main toolbar Click New Java class btn, select xx\src as a source
folder, enter HelloWorld for class name, click check box to create
the main() method, click Finish
Add a println statement
– Type stmts, then save changes, the class will automatically
compile upon saving
Run your java application
– Right click on your class in the Package Explorer, and then select
Run As>Java application
5/5/2017
Assoc. Prof. Stoyan Bonev
44
Eclipse
Downloads:
– http://www.eclipse.org/downloads/
Tutorials:
– http://www.vogella.de/articles/Eclipse/article.html
– http://eclipsetutorial.sourceforge.net/totalbeginner.html
5/5/2017
Assoc. Prof. Stoyan Bonev
45
Java Development Tools
Brief
overview on Java Development Tools:
(Detailed discussion in lecture 29)
–
–
–
–
–
java
javac
javadoc
jar
jdb
5/5/2017
Assoc. Prof. Stoyan Bonev
46
Running java
Synopsis
java [interpreter options] classname [program arguments]
java [interpreter options] -jar jarfile [program arguments]
Description
Common
5/5/2017
Options
Assoc. Prof. Stoyan Bonev
47
Running javac
Synopsis
– javac [options] files
Description
Common
5/5/2017
Options
Assoc. Prof. Stoyan Bonev
48
Running javadoc
Synopsis
javadoc [options] @list package… sourcefiles…
Description
Common
5/5/2017
Options
Assoc. Prof. Stoyan Bonev
49
Running jar
Synopsis
jar c|t|u|x [jarfile] [manifest] [-C directory] [input files]
jar I [jarfile]
Description
Common
5/5/2017
Options
Assoc. Prof. Stoyan Bonev
50
Running jdb
Synopsis
jdb [options] class [program options]
Description
Common
5/5/2017
Options
Assoc. Prof. Stoyan Bonev
51
Practical session
• Task: write a program to input two integral
values and to compute and display the
result of their addition, subtraction,
multiplication and division
• Language: Java
• IDE: Eclipse
5/5/2017
Assoc. Prof. Stoyan Bonev
52
Practical session
• Task: write a program to input two integral
values and to compute and display the
result of their addition, subtraction,
multiplication and division
• Language: Java
• IDE: NetBeans
5/5/2017
Assoc. Prof. Stoyan Bonev
53
Practical session
• Task: write a program to input two integral
values and to compute and display the
result of their addition, subtraction,
multiplication and division
• Language: Java
• IDE: jGRASP
5/5/2017
Assoc. Prof. Stoyan Bonev
54
Practical session
Problem:
To accumulate sum of stream of input
integers terminated by sentinel value
• Language: Java
• IDE: Eclipse
5/5/2017
Assoc. Prof. Stoyan Bonev
55
Practical session
Problem:
To accumulate sum of stream of input
integers terminated by sentinel value
• Language: Java
• IDE: NetBeans
5/5/2017
Assoc. Prof. Stoyan Bonev
56
Practical session
Problem:
To accumulate sum of stream of input
integers terminated by sentinel value
• Language: Java
• IDE: jGRASP
5/5/2017
Assoc. Prof. Stoyan Bonev
57
Thank You
for
Your attention!