Lesson 1: The Java World
Lesson 1: The Java World
UNIX
Linux
Windows
MAC OS
MS-DOS
Language Processors
In order to run /to execute/, any program
written in HLL (incl. Java) should be
transformed to executable form
Three ways to convert source code into
executable form:
Compiler – generate object code – see slide+1
Interpreter – interpret source code – see+2
Compiler of hybrid (semi-interpreting) type - +3
9
data
10
Data
Compile time
Run time
11
data
12
How Java works
Java compiled to intermediate form – byte code.
When a Java program is compiled, the .java files are fed to the
compiler which produces a .class file for each .java file.
Java byte code is like machine language, but it is intended for the Java
Virtual Machine, not for a specific processor.
15
16
Executing a Java program
17
JVM emulation run on a physical machine
18
JVM handles translations
19
Java Components of special interest
Pure Java includes 3 software facilities:
JDK
20
JRE
23
JDK contents
java – the loader for Java applications. This tool is an interpreter and
can interpret the class files generated by the javac compiler.
javac – the Java compiler, which converts source code into Java
bytecode
jar – the archiver, which packages related class libraries into a single
JAR file. This tool also helps manage JAR files.
25
JDK contents (full list of utilities)
appletviewer – this tool can be used to run and debug Java applets
without a web browser
apt – the annotation-processing tool4
extcheck – a utility which can detect JAR-file conflicts
idlj – the IDL-to-Java compiler. This utility generates Java bindings from
a given Java IDL file.
java – the loader for Java applications. This tool is an interpreter and
can interpret the class files generated by the javac compiler. 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.
Full list of utilities is too long and is not a subject of this lesson
26
A Simple Java Program
Listing 1.1
//This program prints message “Welcome to Java!“
// braces in end-line style
27
A Simple Java Program
Listing 1.1
//This program prints message “Welcome to Java!“
// braces in new-line style
28
Creating, Compiling, and
Running Java Programs
Create/Modify Source Code
Result
29
If runtime errors or incorrect result
Running JAVA
in
Windows Environment
Creating, Compiling & Running
Java Programs
31
To open command window
Click Start
Select All Programs >
Select Accessories
Click Command Prompt
32
Compiling and Running Java programs
Using any text editor , /notepad, edit, write/ type the source text
of a Java demo program like this:
33
Compiling and Running Java programs
Run compiler with statement like
this:
javac prog4.java
http://www.jgrasp.org
http://www.jgrasp.org/tutorials187/02_Getting_Started.pdf
35
jGRASP – introduction
jGRASP is a lightweight development environment,
implemented in Java, and runs on all platforms with a
Java Virtual Machine (Java version 1.5 or higher).
36
jGRASP – JGrasp02_Getting_Started.pdf
37
jGRASP – JGrasp02_Getting_Started.pdf
2.2 Opening a program, compiling and running
File > Open > select a file in a folder
Build > Compile
Build > Run |> Run as Application |> Run as Applet
39
jGRASP – JGrasp02_Getting_Started.pdf
40
jGRASP – JGrasp02_Getting_Started.pdf
41
Exercises/Tasks
Type, compile, run a program: Lesson01_a.java
} // end of main
} // end of class
42
Exercises/Tasks
Write a Java program Proba3.java:
44
Exercises/Tasks
import java.util.Scanner;
45
Exercises/Tasks
import java.util.Scanner;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
for(int i=1; i<=5; i++)
System.out.println("Good luck, dear ICoSCIS student!!");
46
Creating, Compiling & Running
Java Programs
48
To create an IDE project:
1. Start NetBeans IDE.
2. In the IDE, choose File > New Project…
(Ctrl+Shift+N), as shown in the figure below.
49
To create an IDE project:
3. In the New Project wizard, expand the Java
category and select Java Application as shown
in the figure below. Then click Next.
50
To create
an IDE
project:
5. Click Finish.
The project is created and opened in the IDE. You should see the
following components:
The Projects window, which contains a tree view of the components of
the project, including source files, libraries that your code depends on,
and so on.
The Source Editor window with a file called <ClassName>.java open.
The Navigator window, which you can use to quickly navigate between
elements within the selected class.
The Tasks window, which lists compilation errors as well other tasks
that are marked with keywords. 52
Adding Code to the Generated
Source File
Because you have left the Create Main Class checkbox selected in the New
Project wizard, the IDE has created a skeleton main class for you. You can
add the "Hello World!" message to the skeleton code by replacing the line:
The file should look something like the following code sample.
53
Adding Code to the Generated Source File
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package helloworldapp;
/**
*
* @author <your name>
*/
public class HelloWorldApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
}
} 54
Compiling and running a project
Because of the IDE's Compile on Save feature, you do
not have to manually compile your project in order to
run it in the IDE. When you save a Java source file,
the IDE automatically compiles it. (how to switch this
option off, see Project > Properties > Compiling).
To run the program:
Choose Run > Run Main Project (F6).
The figure below shows what you should now see.
55
Compiling and running a project
If there are compilation errors, they are marked with
red glyphs in the left and right margins of the Source
Editor.
The glyphs in the left margin indicate errors for the
corresponding lines.
The glyphs in the right margin show all of the areas of the
file that have errors, including errors in lines that are not
visible.
You can mouse over an error mark to get a description
of the error.
You can click a glyph in the right margin to jump to the
line with the error.
56
Compiling and running a project
This is unit testing in Java using JUnit framework.
For more details see lesson 7.
57
Compiling and running a project
58
Compiling and running a project
59
Compiling and running a project
60
Compiling and running a project
61
Building and deploying the application (.jar file)
Once you have written and test run your application, you can use
the Clean and Build command to build your application for
deployment. When you use the Clean and Build command, the
IDE runs a build script that performs the following tasks:
Deletes any previously compiled files and other build outputs.
Recompiles the application and builds a JAR file containing the compiled
files.
You can view the build outputs by opening the Files window and
expanding the <PrjName> node. The compiled bytecode file
<PrjName>.class is within the build/classes/<ClassName>
subnode. A deployable JAR file that contains the
<PrjName>.class is within the dist node.
62
Building and deploying the application (.jar file)
.
.
63
Create javadoc
To be discussed in separate lesson
64
Tips for NetBeans users
The Compile on Save feature can be turned off in the
Project Properties window. Right-click your project,
select Properties. In the Properties window, choose
the Compiling tab. The Compile on Save checkbox is
right at the top.
Note that in the Project Properties window you can
configure numerous settings for your project: project
libraries, packaging, building, running, etc.
65
Exercises/Tasks
Write a Java program to test the options for
the main menu Run command:
To display 5 times the string
“Good luck, dear ICoSCIS student!!”
To enter two numeric integer/real values and
To display their sum and product.
66
Exercises/Tasks
Write a Java program to test the command-line
arguments option
How to specify arguments
Right-click your project, select Properties. In the Properties
window, choose the Run tab. Enter arguments
How to access arguments
69
Running JAVA
in
UNIX/Linux Environment
Logging In to UNIX/Linux
Practice
• Login
– PuTTY IP address: 194.141.86.251
– PuTTY Host Name: webmonitor.swu.bg
• Basic UNIX commands
– ls, pwd, mkdir, cd, nano-editor, man, cp, mv, rm
• Java related commands
– Java compiler: javac
– Java Virtual Machine: java
– Java utilities: jar, javadoc, jdb
Thank You
For
Your Attention!
73