JAVA Complete
JAVA Complete
Java
Oa
k
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 2
Systems
Introduction
●
It is a general purpose concurrent object oriented
language, with a syntax similar to C and C++, but
omitting features that are complex and unsafe.
C++ Java
Backward compatile con C Backward compatibility with
previous Java versions
Execution efficiency Trusts the Developer productivity Protects
programmer the programmer
Arbitrary memory access possible Memory access through objects
Concise expression Explicit operation
Can arbitrarily override types Type safety Object oriented
Procedural or object oriented Meaning of operators immutable
Operator overloading
Compiler
Interpreter
(JVM)
Compiler
Interpreter
(JVM)
$ javac HelloWorld.java
$ ls HelloWorld.class HelloWorld.java
●
Java provides ten fundamental types:
int x;
double f = 0.33;
char c = ’a’;
String s = "abcd";
x = 55;
●
The boolean values are true and false:
●
Java provides a rich set of expressions:
– Arithmetic
– Bit level
– Relational
– Logical
– Strings related
●
Java provides the usual set of arithmetic operators:
–
addition (+)
– subtraction (-)
– division (/)
– multiplication
– (*) modulus
(%)
class Arithmetic {
public static void main(String[] args) { int x = 12;
int y = 2 * x; System.out.println(y); int z = (y -
x) % 5;
System.out.println(z); nal oat pi = 3.1415F;
oat f = pi / 0.62F; System.out.println(f);
}
}
$ java Arithmetic 24
2
5.0669355
●
Shorthand operators are provided:
class ShortHand {
public static void main(String[] args) { int x = 12;
x += 5; // x = x + 5 System.out.println(x);
x *= 2; // x = x * 2 System.out.println(x);
}
}
$ java ShortHand 17
34
●
Pre and post operators are also provided:
class Increment {
public static void main(String[] args) { int x = 12,y = 12;
$ java Increment 12 13 13 13
●
Java provides the following relational operators:
–
equivalent (==) not
– equivalent (!=) less than
– (<) greater that (>)
– less than or equal (<=)
– greater than or equal (>=)
–
●
Important: relational expressions always return a
boolean value.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 20
Systems
Relational Expressions
class Boolean {
public static void main(String[] args) { int x =
12,y = 33;
●
Java provides the following operators:
–
and (&) or (|) not(˜)
– shift left (<<)
– shift right with sign extension (>>)
– shift right with zero extension (>>>).
–
class Bits {
public static void main(String[] args) {
int x = 0x16; //
int y = 0x33; 00000000000000000000000000010110
//
System.out.println(x & y); // 00000000000000000000000000010010
00000000000000000000000000110011
System.out.println(x | y); // 00000000000000000000000000110111
System.out.println(˜x); // 11111111111111111111111111101001
●
Java provides the following operators:
–
and
– (&&) or
– (||)
not(!)
● Important: The logical operators can only
be applied to boolean expressions and
return a boolean value.
class Logical {
public static void main(String[] args) { int x = 12,y
= 33;
double d = 2.45,e = 4.54;
●
Java provides many operators for Strings:
– Concatenation (+)
– many more...
class Strings {
public static void main(String[] args) {
class Strings2 {
public static void main(String[] args) {
System.out.println(s1.equals(s2)); System.out.
println(s1.equals("Hi"));
}
}
class TestNarrow {
public static void main(String[] args) {
long a = 34; int b =
(int)a; // a is a long
double d = 3.45; // d is a double
} oat f = (oat)d;
}
if ((c >= ’a’ && c <= ’z’) || (c >= ’A’ && c <= ’Z’)) System.out.
println("letter: " + c);
else
if (c >= ’0’ && c <= ’9’) System.out.println("digit: " + c);
else {
System.out.println("the character is: " + c); System.out.
println("it is not a letter"); System.out.println("and it is not
a digit");
}
}
}
$ java If letter: x
$ java Arrays
d[1]=0.5 d[3]=0.25 d[5]=0.16666667 d[7]=0.125 d[9]=0.1
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 42
Systems
Command line
arguments
● We have seen that the method main has to
be defined as follows:
b3 = new Book();
●
Inside a class it is possible to define:
– data elements, usually called instance variables
– functions, usually called methods
●
Class Book with instance variables:
title
"Thinking in Java"
class Book { String title; author
"Bruce Eckel"
String author;
number
int numberOfPages; fPages 1129
} O
b
class ExampleBooks {
public static void main(String[] args) {
class ExampleBooks2 {
public static void main(String[] args) {
Book b = new Book("Thinking in Java","Bruce Eckel",1129); System.
out.println(b.title + " : " + b.author +
" : " + b.numberOfPages);
}
}
b = new Book();
title title
"Thinking in Java" "Thinking in Java"
author author
"Bruce Eckel" "Bruce Eckel"
a number b number
fPages 1129 fPages 1129
O ISBN "unknown" O ISBN "0-13-027363-5"
$ java ExampleBooks3
Thinking in Java : Bruce Eckel : 1129 : unknown Thinking in Java : Bruce
Eckel
Carlos Kavka : 1129 : 0-13-027362-5
First Latin American Workshop on Distributed Laboratory Instrumentation 54
Systems
Methods
...
title
"Java network programming"
author
"Elliot Rusty Harold"
number
OfPages 649
ISBN "unknown"
a
Initials: E.R.H.
Book b1,b2;
if (b1 == b2)
System.out.println("The two books are the same"); else
System.out.println("The two books are different");
}
}
$ java ExampleBooks6
The two books are different
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 60
Systems
Equality and equivalence
title title
"Thinking in Java" "Thinking in Java"
author author
"Bruce Eckel" "Bruce Eckel"
b1 number b2 number
fPages 1129 fPages 1129
O ISBN "unknown" O ISBN "0-13-027363-5"
if (b1 == b2)
System.out.println("The two books are the same"); else
System.out.println("The two books are different");
Book b1,b2;
if (b1 == b2)
System.out.println("The two books are the same"); else
System.out.println("The two books are different");
}
}
$ java ExampleBooks6a
The two books are the same
title
"Thinking in Java"
author
"Bruce Eckel"
b1 number
fPages 1129
O ISBN "unknown" b2
if (b1 == b2)
System.out.println("The two books are the same"); else
System.out.println("The two books are different");
...
Book b1,b2;
if (b1.equals(b2))
System.out.println("The two books are the same"); else
System.out.println("The two books are different");
}
}
$ java ExampleBooks7
The two books are the same
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 65
Systems
Class variables
...
Book b1,b2;
b1 = new Book("Thinking in Java","Bruce Eckel",1129);
b2 = new Book("Java in a nutshell","David Flanagan",353);
b1.setOwner("Carlos Kavka");
System.out.println("Owner of book b1: " + b1.getOwner());
System.out.println("Owner of book b2: " + b2.getOwner());
}
}
$ java ExampleBooks8
Owner of book b1: Carlos Kavka Owner of book b2: Carlos Kavka
...
$ java ExampleBooks9
Book instances can store information on books Book instances can store
information on books
references false
null
class InitialValues {
public static void main(String[] args) {
class InitialValues2 {
public static void main(String[] args) { Values v = new
Values("hello"); System.out.println("" + v.x + "\t" + v.f);
System.out.println("" + v.s + "\t" + v.b);
}
}
B.E.
Carlos Kavka
●
The class Book has two constructors:
Book(String tit,String aut,int num) {
title = tit; author = aut; numberOfPages = num; ISBN =
"unknown";
}
Book(String tit,String aut,int num,String isbn) { title = tit;
author = aut; numberOfPages = num; ISBN = isbn;
}
●
The second can be defined in terms of the first one:
= r;
im = i;
}
public double getReal() { double realPart = a.getReal()
return real;
}
public double getImaginary() { double imPart = a. getImmaginary()
return im;
} }
Complex c = a.add(b);
Complex d = c.sub(a);
❶ ❷ ❸
1.33 3.33 9.56
a 4.64 a 4.64 a 4.64
●
We can define a new constructor to avoid the problem:
Complex(Complex x) {
this(x.real,x.im);
}
❶ ❷
1.33 1.33
a 4.64 e 4.64
ScienticBook
Object
ScienticBook Novel
ScienceFiction Crime
ScienticBook sb;
sb = new ScienticBook(
"Neural Networks",
"Simon Haykin",696,"0-02-352761-7",
"Articial Intelligence");
ScienticBook sb;
S.H.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 92
Systems
Inheritance (overriding methods)
class ScienticBook extends Book { String
area; Book
boolean proceeding = false;
ScienticBook(String tit, String aut, int num, equals(...)
String isbn, String a) {
super(tit,aut,num,isbn);
ScienticBook
area = a;
}
equals(...)
public boolean equals(ScienticBook b){ return
super.equals(b) &&
area.equals(b.area) &&
proceeding == b.proceeding;
} }
●
Two possible solutions:
public boolean equals(ScienticBook b){ return super.
equals(b) && area.equals(b.area)
&& proceeding == b.proceeding;
}
public boolean equals(ScienticBook b) {
return (title.equals(b.title) && author.equals(b.author) &&
numberOfPages == b.numberOfPages
&& ISBN.equals(b.ISBN) && area.equals(b.area) &&
proceeding == b.proceeding;
}
$ java TestScienticBooks
S.H.false
ScienticBook instances can store information on scientic books
True
●
getClass() returns the runtime class of an object:
System.out.println(b1.getClass().getName());
Book
System.out.println(b1.getClass().getName()); System.out.
println(sb1.getClass().getName()); System.out.println(b1 instanceof
Book); System.out.println(sb1 instanceof Book); System.out.println(b1
instanceof ScienticBook); System.out.println(sb1 instanceof
ScienticBook);
}
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 101
Systems
Packages
class TestDate {
public static void main(String[] args) { System.out.
println(new Date());
}
}
$ java TestDate
Wed Oct 25 09:48:54 CEST 2006
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 102
Systems
Packages
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 103
Systems
Packages
it
infn
Example of use:
ts
import it.infn.ts.Book;
package it.infn.ts; class TestBook {
class Book { ...
... Book b = new Book(...);
} ...
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 104
Systems
Access control
– private
– protected
ScienticBook Novel
ScienceFiction Crime
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 105
Systems
Access control
●
or by just accessing the data member:
sb1.proceeding = true;
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 106
Systems
Access control
sb1.setProceeding(); // ne
sb1.proceeding = true; // wrong
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 107
Systems
Access control
●
The same access control can be applied to methods.
class ScienticBook extends Book { private String area;
private boolean proceeding = false;
...............
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 108
Systems
Final and
abstract
● The modifiers final and abstract can be applied
to classes and methods:
–
final:
● A final class does not allow subclassing.
●
A final method cannot be redefined in a subclass.
– abstract:
● An abstract class is a class that cannot be
instantiated.
●
An abstract method has no body, and it must
be redefined in a subclass.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 109
Systems
Final and
abstract
●
An example: the class IOBoard and its subclasses.
IOBoard
IOEthernetBoard IOSerialBoard
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 110
Systems
Final and
abstract
abstract class IOBoard { String name;
int numErrors = 0;
IOBoard(String s) { System.out.println("IOBoard
constructor"); name = s;
}
nal public void anotherError() { numErrors++;
}
nal public int getNumErrors() { return numErrors;
}
abstract public void initialize(); abstract public void
read(); abstract public void write(); abstract public
void close();
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 111
Systems
Final and
abstract
class IOSerialBoard extends IOBoard { int port;
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 114
Systems
Polymorphism
class TestBoards2 {
public static void main(String[] args) { IOBoard[]
board = new IOBoard[3];
board[0] = new IOSerialBoard("my rst port",0x2f8);
board[1] = new IOEthernetBoard("my second port",0x3ef8dda8);
board[2] = new IOEthernetBoard("my third port",0x3ef8dda9);
for(int i = 0;i < 3;i++)
rst second
board[i].initialize();
for(int i = 0;i < 3;i++)
board[i].read(); third
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 116
Systems
Interfaces
●
An interface for specifying IO boards behavior:
interface IOBoardInterface {
public void initialize();
public void read();
public void write();
public void close();
}
●
An interface for specifying nice behavior:
interface NiceBehavior { public
String getName(); public String
getGreeting(); public void
sayGoodBye();
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 117
Systems
Interfaces
class IOSerialBoard2 implements IOBoardInterface { int port;
●
A class can implement more than one interface.
class IOSerialBoard2 implements IOBoardInterface,
NiceBehavior {
....
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 119
Systems
Exceptions
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 120
Systems
Exceptions
●
The exception can be trapped:
class TestExceptions2 {
public static void main(String[] args) {
String s = "Hello"; try {
System.out.print(s.charAt(10));
} catch (Exception e) { System.out.println("No
such position");
}
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 121
Systems
Exceptions
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 122
Systems
Exceptions
●
It is possible to send messages to an exception object:
class TestExceptions4 {
public static void main(String[] args) {
String s = "Hello"; try {
System.out.print(s.charAt(10));
} catch (StringIndexOutOfBoundsException e) { System.
out.println("No such position"); System.out.
println(e.toString());
}
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 123
Systems
Exceptions
●
We can add multiple catch blocks and a finally clause:
class MultipleCatch {
public void printInfo(String sentence) { try {
// get rst and last char before the dot char rst = sentence.
charAt(0);
char last = sentence.charAt(sentence.indexOf(".") - 1);
String out = String.format("First: %c Last: %c",rst, System.out. last);
println(out);
} catch (StringIndexOutOfBoundsException e1) {
System.out.println("Wrong sentence, no dot?");
} catch (NullPointerException e2) { System.out.println("Non valid
string");
} nally {
} System.out.println("done!");
}
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 124
Systems
Exceptions
class MultipleCatch {
public void printInfo(String sentence) { try {
// get rst and last char before the dot char rst = sentence.charAt(0);
char last = sentence.charAt(sentence.indexOf(".") - 1);
String out = String.format("First: %c Last: %c",rst, last); System.out.
println(out);
} catch (StringIndexOutOfBoundsException e1) {
System.out.println("Wrong sentence, no dot?");
} catch (NullPointerException e2) { System.out.println("Non valid string");
} nally {
System.out.println("done!");
}
}
} String sentence = "A test sentence.";
MultipleCatch mc = new MultipleCatch(); mc. First: A Last: e done!
printInfo(sentence);
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 125
Systems
Exceptions
class MultipleCatch {
public void printInfo(String sentence) { try {
// get rst and last char before the dot char rst = sentence.charAt(0);
char last = sentence.charAt(sentence.indexOf(".") - 1);
String out = String.format("First: %c Last: %c",rst, last); System.out.
println(out);
} catch (StringIndexOutOfBoundsException e1) {
System.out.println("Wrong sentence, no dot?");
} catch (NullPointerException e2) { System.out.println("Non valid string");
} nally {
System.out.println("done!");
}
}
} String sentence = "A test sentence";
MultipleCatch mc = new MultipleCatch(); mc. Wrong sentence, no dot?
printInfo(sentence); done!
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 126
Systems
Exceptions
class MultipleCatch {
public void printInfo(String sentence) { try {
// get rst and last char before the dot char rst = sentence.charAt(0);
char last = sentence.charAt(sentence.indexOf(".") - 1);
String out = String.format("First: %c Last: %c",rst, last); System.out.
println(out);
} catch (StringIndexOutOfBoundsException e1) {
System.out.println("Wrong sentence, no dot?");
} catch (NullPointerException e2) { System.out.println("Non valid string");
} nally {
System.out.println("done!");
}
}
} String sentence = null;
MultipleCatch mc = new MultipleCatch(); mc. Non valid string done!
printInfo(sentence);
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 127
Systems
Exceptions
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 128
Systems
Input - Output
●
Input output in Java is rather complicated.
●
However, input output from files, devices, memory
or web sites is performed in the same way.
●
It is based on the idea of streams:
–
An input stream is a data source that can be accessed
in order to get data.
– An output stream is a data sink, where data can be
written.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 129
Systems
Input - Output
●
Streams can be classified in:
– byte streams
● provides support also for fundamental types.
– character streams
● Unicode, but with OS character support.
●
Streams can be:
– non buffered
– buffered
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 130
Systems
Input - Output
class WriteBytes {
public static void main(String[] args) { int data[] = { 10,20,30,40,255 };
FileOutputStream f; try {
f = new FileOutputStream("le1.data");
for(int i = 0;i < data.length;i++) f.write(data[i]);
f.close();
} catch (IOException e) {
System.out.println("Error with les:"+e.toString());
}
}
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 131
Systems
Input - Output
import java.io.*; byte oriented stream
class ReadBytes {
public static void main(String[] args) {
FileInputStream f; try {
f = new FileInputStream("le1.data");
int data;
while((data = f.read()) != -1) System.out.println(data);
f.close();
} catch (IOException e) {
System.out.println("Error with les:"+e.toString());
}
}
}
class WriteArrayBytes {
public static void main(String[] args) { byte data[] = {
10,20,30,40,-128 };
FileOutputStream f; try {
f = new FileOutputStream("le1.data");
f.write(data,0,data.length); f.close();
} catch (IOException e) {
System.out.println("Error with les:"+e.toString());
}
}
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 133
Systems
Input - Output
import java.io.*; buffered byte oriented stream
class WriteBufferedBytes {
public static void main(String[] args) { int data[] = { 10,20,30,40,255 };
FileOutputStream f;
BufferedOutputStream bf;
try {
f = new FileOutputStream("le1.data"); bf = new
BufferedOutputStream(f); for(int i = 0;i < data.length;i++)
bf.write(data[i]); bf.close();
} catch (IOException e) {
System.out.println("Error with les:"+e.toString());
}
}
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 134
Systems
Input - Output
import java.io.*; buffered byte oriented stream
class ReadBufferedBytes {
public static void main(String[] args) { FileInputStream f;
BufferedInputStream bf; try {
f = new FileInputStream("le1.data"); bf = new
BufferedInputStream(f);
int data;
while((data = bf.read()) != -1) System.out.println(data);
bf.close();
} catch (IOException e) {
System.out.println("Error with les:"+e.toString());
}
}
}
readDouble()
Carlos Kavka
Systems
writeDouble(double)
First Latin American Workshop on Distributed Laboratory Instrumentation 136
Input - Output
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 137
Systems
Input - Output
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 139
Systems
Input - Output
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 140
Systems
Input - Output
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 141
Systems
Input - Output
try {
Scanner
int sum =sc0;= new Scanner(System.in);
while (sc.hasNextInt()) { int $ java ReadW ithScanner
anInt = sc.nextInt(); sum += 11
anInt;
9
}
System.out.println(sum); ^D 20
} catch (IOException e) {
System.out.println("Error with standard input");
}
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 143
Systems
Threads
●
themselves Their access to shared data can be
synchronized.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 144
Systems
Threads
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 145
Systems
Threads
class TestThreads {
public static void main(String[] args) {
CharThread t1 = new CharThread(’a’);
CharThread t2 = new CharThread(’b’);
t1.start();
t2.start();
}
}
$ java TestThreads a
b
a b
...
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 146
Systems
Threads
●
A typical producer - consumer application:
class ProducerConsumer {
public static void main(String[] args) {
Buffer buffer = new Buffer(10);
Producer prod = new Producer(buffer);
Consumer cons = new Consumer(buffer);
prod.start();
cons.start();
}
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 147
Systems
Threads
●
Insertion and removal of elements in the buffer:
head tail head tail
5
initial situation inserting a 5
5 8 8
inserting a 8 removing
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 148
Systems
Threads
●
Going beyond the limit of the buffer:
head tail head tail
4 7 2 4 7 1 8
1 8 inserting a 2
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 149
Systems
Threads
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 150
Systems
Threads
numElements--;
return value;
} However... it do es not work
}
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 151
Systems
Threads
●
The implementation does not work!
●
There is a need for synchronization.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 152
Systems
Threads
synchronized insert()
synchronized delete()
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 153
Systems
Threads
●
Threads are synchronized with wait and notify:
–
The message wait puts the calling thread to sleep,
releasing the lock.
– The message notify awakens a waiting thread on
the corresponding lock.
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 154
Systems
Threads
synchronized delete()
synchronized insert()
notify() synchronized delete()
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 155
Systems
Threads
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 156
Systems
Threads
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 157
Systems
Jar files
Buffer.class Consumer.
class ProducerConsumer.
class Producer.class
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 158
Systems
Jar files
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 159
Systems
Jar files
●
The creation of the JAR file can be done as follows:
●
The application can be executed as follows:
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 160
Systems
Jar files
●
It contents can be displayed as follows:
$ jar tf ProducerConsumer.jar META-INF/
META-INF/MANIFEST.MF
ProducerConsumer.class Producer.class Consumer.
class Buffer.class
●
Note that a manifest file was added:
Manifest-Version: 1.0
Main-Class: ProducerConsumer
Created-By: 1.2.2 (Sun Microsystems Inc.)
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 161
Systems
Ant
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 162
Systems
Ant
●
An example of a build.xml file:
<?xml version="1.0"?>
<!-- rst build le -->
<project name="HelloWorld" default="build" basedir=".">
<target name="build">
<javac srcdir="." />
</target>
</project>
# ant
Buildle: build.xml build:
[javac] Compiling 1 source le
BUILD SUCCESSFUL
Total time: 3 seconds
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 163
Systems
Ant
●
A project specifies three elements
–
name
– target
– basedir
●
A target specifies five elements:
–
name
– depends if
– unless
– description
–
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 164
Systems
Ant
●
Properties are like variables:
<javac srcdir="${src-dir}"/>
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 165
Systems
Ant
●
An example: four
tasks:
Complex
● clean
src ● init
● build
Complex.java TestComplex.java ● dist
build
Complex.class TestComplex.class
dist
complex.jar
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 166
Systems
Ant
<?xml version="1.0"?>
<mkdir dir="${build-dir}"/>
</target>
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 167
Systems
Ant
<mkdir dir="${dist-dir}"/>
</project>
$ ant
Buildle: build.xml init:
[mkdir] Created dir: ComplexNumbers/build build:
[javac] Compiling 2 source les to ComplexNumbers/build
dist:
[mkdir] Created dir: ComplexNumbers/dist
[jar] Building jar: ComplexNumbers/dist/complex.jar BUILD
SUCCESSFUL
Total time: 11 seconds
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 169
Systems
Java on the TINI
❹
execution
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 170
Systems
Java on the TINI
●
Step 1: compilation
$ javac HelloWorld.java
●
Step 2: conversion
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 171
Systems
Java on the TINI
●
Step 3: transfer
$ ftp tini Connected to tini.
220 Welcome to slush. (Version 1.17) Ready for user login.
User (tini:(none)): root
331 root login allowed. Password required. Password:
230 User root logged in. ftp> bin
200 Type set to Binary
ftp> put HelloWorld.tini
200 PORT Command successful.
150 BINARY connection open, putting HelloWorld.tini
226 Closing data connection.
ftp: 183 bytes sent in 0.00 Seconds. ftp> bye
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 172
Systems
Java on the TINI
●
Step 4: execution
# telnet tini Connected to tini.
Escape character is ’ˆ]’.
Welcome to slush. (Version 1.17)
tini00a93c login: root tini00a93c
password:
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 173
Systems
TiniAnt
Carlos Kavka First Latin American Workshop on Distributed Laboratory Instrumentation 174
Systems
TiniAnt