Go to http://java.sun.com/products/jdk/1.2/download-windows.html
javac.exe
,
java.exe
,
javadoc.exe
,
etc.) from any directory without having to type the full path of the command.
If you don't set the PATH variable, you need to specify the full path to the
executable every time you run it, such as: C:> \jdkjava\bin\javac MyClass.java
· To set the PATH permanently, open the AUTOEXEC.BAT file and add or change the PATH statement as follows:
PATH C:\WINDOWS\COMMAND; C:\JDKJAVA\BIN
· Capitalization doesn't matter. The PATH can be a series of directories separated by semi-colons (;). Microsoft Windows searches for programs in the PATH directories in order, from left to right. You should only have one bin directory for the Java 2 SDK in the path at a time (those following the first are ignored), so if one is already present, you can update it to 1.2.2.
c:\autoexec.bat
C:\ path
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/index.html
· To learn how to program in Java, start with:
http://java.sun.com/docs/books/tutorial/java/index.html and work your way through the introductions.
· Inside this tutorial, there is a link to The Java Language Environment, a white paper written by James Gosling and Henry McGilton. Do NOT waste your time there if you are just a new starter for Java and you don't know too much about C++.
http://java.sun.com/docs/books/tutorial/native1.1/index.html
http://pandonia.canberra.edu.au/java/jini/tutorial/Jini.xml
(Extracted from http://www.poli.studenti.to.it/info/javatutorial
by M. Campione & K.Walrath)
Start
Here to Create Standalone Java Applications
The most common Java programs are applications and applets. Applications are standalone programs, such as the HotJava browser. Applets are similar to applications, but they don't run standalone. Instead, applets adhere to a set of conventions that lets them run within a Java-compatible browser.
The "Hello World" application has two blocks of comments. The first block, at
the top of the program, uses /**
and
*/
delimiters. Later, a line of code is explained with a comment that's marked by
//
characters. The Java language supports a third kind of comment, as well -- the
familiar C-style comment, which is delimited with /*
and
*/
.
In the Java language, all methods (functions) and variables exist within a class or an object (an instance of a class). The Java language does not support global functions or stray variables. Thus, the skeleton of any Java program is a class definition.
The brain of every Java application is its main()
method.
When you run an application with the Java interpreter, you specify the name of
the class that you want to run. The interpreter invokes the main()
method
defined within that class. The main()
method
controls the flow of the program, allocates whatever resources are needed, and
runs any other methods that provide the functionality for the application.
The other components of a Java application are the supporting objects, classes, methods, and Java language statements that you write to implement the application.
Let's take a look at the first segment of the statement:
System.out.println("Hello World!");
The construct System.out
is
the full name of the out
variable
in the System class. Notice that the application never instantiates the System
class and that out
is
referred to directly from the class name. This is because out
is a class
variable--a variable associated with the class rather than with an instance of
the class. You can also associate methods with a class--class
methods.
Methods and variables that are not class methods or class variables are known as instance methods and instance variables. To refer to instance methods and variables, you must reference the methods and variables from an object.
While System's out
variable
is a class variable, it refers to an instance of the
PrintStream class (a class provided with the Java development environment) that
implements the standard output stream.
When the System class is loaded into the application, it instantiates
PrintStream and assigns the new PrintStream object to the out
class
variable. Now that you have an instance of a class, you can call one of its
instance methods:
System.out.println("Hello World!");
As
you can see, you refer to instance methods and variables similarly to the way
you refer to class methods and variables. You join an object reference
(out
) and the
name of the instance method or variable (println
)
together with a period (".").
The Java compiler allows you to cascade references to class and instance methods and variables together, resulting in constructs like the one that appears in the sample program:
System.out.println("Hello World!");
A class method or class variable is associated with a particular class. The runtime system allocates a class variable once per class, no matter how many instances exist of that class. You access class variables and methods through the class.
An instance method or instance variable is associated with a particular object (an instance of a class). Every time you create an object, the new object gets a copy of every instance variable defined in its class. You access instance variables and methods through objects.
HelloWorld.class
<HTML>
<HEAD>
<TITLE>
A Simple Program </TITLE>
</HEAD>
<BODY>
Here is
the output of my program:
<APPLET CODE="HelloWorld.class" WIDTH=150
HEIGHT=25>
</APPLET>
</BODY>
</HTML>
Here is the output of my program: Hello World!
Now that you've seen a Java applet, you're probably wondering how it works. Java applet is a program that adheres to a set of conventions that allows it to run within a Java-compatible browser.
The code above starts off with two import statements. By importing classes or packages, a class can more easily refer to classes in other packages. In the Java language, packages are used to group classes, similar to the way libraries are used to group C functions.
Every applet must define a subclass of the Applet class. In the "Hello World" applet, this subclass is called HelloWorld. Applets inherit a great deal of functionality from the Applet class, ranging from communication with the browser to the ability to present a graphical user interface (GUI).
Implementing Applet
Methods
The
HelloWorld applet implements just one method, the paint()
method. Every applet must implement at least one of the following methods:
init()
,
start()
, or
paint()
.
Unlike Java applications, applets do not need to implement a
main()
method.
Applets
are meant to be included in HTML pages. Using the <APPLET> tag, you
specify (at a minimum) the location of the Applet subclass and the dimensions of
the applet's onscreen display area. When a Java-capable browser encounters an
<APPLET> tag, it reserves onscreen space for the applet, loads the Applet
subclass onto the computer the browser is executing on, and creates an instance
of the Applet subclass. Next, the browser calls the applet's init()
and
start()
methods, and the applet is off and running.
Completed by Oct. 3, 1999
(Extracted from http://java.sun.com/docs/books/tutorial/native1.1by Beth Stearns)
An example of integrating native code in C with programs written in Java
The JNI allows Java code that runs within a Java Virtual Machine (VM) to operate with applications and libraries written in other languages, such as C, C++, and assembly. In addition, the Invocation API allows you to embed the Java Virtual Machine into your native applications.
Programmers use the JNI to write native methods to handle those situations when an application cannot be written entirely in the Java programming language.
The "HelloWorld.java" example walks you through the steps necessary to integrate native code in C with programs written in Java.
· Write the Java Code called HelloWorld.java
· Compile the Java code:
C:\> javac HelloWorld.java
· Create the HelloWorld.h File
C:\> javah -jni HelloWorld
Java_HelloWorld_displayHelloWorld
.
This implementation is in the file named HelloWorldImp.c
cl -Ic:\jdk1.2.2\include -Ic:\jdk1.2.2\include\win32 -LD HelloWorldImp.c -Fehello.dll
java HelloWorld
output:
Hello World!
(Extracted from http://pandonia.canberra.edu.au/java/jini/tutorial/Jini.xml by Jan Newmarch)
Implementation (Completed by Oct. 10, 1999)
What classes need to be where? (Completed by Oct. 24, 1999)
Comments on JINI (Completed by Oct. 24, 1999)
Discovering A Lookup Service (Completed by Oct. 31, 1999)
Service Registration (Completed by Oct. 31, 1999)
Client Search (Completed by Oct. 31, 1999)
Jini is the name for a distributed computing environment, that can offer ``network plug and play''. A device or a software service can be connected to a network and announce its presence. Clients that wish to use such a service can then locate it and call it to perform tasks.Let's look at a simple problem, implementing it using JINI technique.
Applications often need to work out the type of a file, to see if it is a text file, an HTML document, an executable, etc. by examining the file's name.
A
common file classification is into MIME types such as text/plain
and
image/gif
.
There are tables of ``official'' MIME types (unofficial ones can be added on an
adhoc basis), and there are also tables of mappings from filename
endings to corresponding MIME types. These tables have entries such as
application/postscript ai eps ps
application/rtf rtf
application/zip zip
image/gif gif
image/jpeg jpeg jpg jpe
text/html html htm
text/plain txt
and are stored in files for applications to access.
This storage of tables separate from the applications that would use them is rated as bad from the O/O point of view, since each application would need to have code to interpret the tables. The multiplicity of these tables and the ability of users to modify them makes this a maintenance problem. It would be better to encapsulate at least the filename to MIME type mapping table in an object. We define a MIME class: MIMEType.java and a mapping class: FileClassifier.java.
This
mapping class has no constructors, as it just acts as a lookup table via its
static method getMIMEType()(
FileClassifier.java).
Applications
may make use these classes as they stand, by simply compiling with them and
having the class files available at runtime. This will still result in
duplication throughout JVMs, possible multiple copies of the class files, and
potentially severe maintenance problems if applications need to be re-compiled.
It may be better to have the FileClassifier
as a network service.
So
we wish to make a version of FileClassifier
available across the network. The service design will be the following:
Separate
the interface from the implementation. Make the interface available to the
client, and upload the implementation to the lookup service. Then when the
client asks for an instance object that implements the interface, it will get
the classifier object. This will reduce maintenance: if the client is coded just
in terms of the interface then it will not need recompilation even if the
implementation changes. Note that these words will translate straight into Java
terms: the client knows about a Java interface
,
whereas the server deals in terms of a Java class that implement
's
the interface
.
Completed by Oct. 10, 1999
java -Djava.rmi.codebase=http://hostname/classes \ FileClassifierServer
Completed by Oct. 24,
1999
We have the classes
common.MIMEType
common.FileClassifier
FileClassifierImpl
FileClassifierServer
client.TestFileClassifier
These could be running on up to four different machines
FileClassifier
TestFileClassifier
FileClassifierImpl
to clients What classes need to be known to which machines?
The
server running FileClassifierServer
needs to know the following classes and interfaces
common.FileClassifier
interface
common.MIMEType
option2.FileClassifierServer
option2.FileClassifierImpl
These
classes all need to be in the CLASSPATH
of
the server.
The
class FileClassifierImpl
will need to be accessible to an HTTP server.
The
lookup service does not need to know any of these classes. It just deals with
them in the form of a java.rmi.MarshalledObject
The client needs to know
common.FileClassifier
interface
common.MIMEType
client.TestFileClassifier
In a running Jini system, there are three main players. There is a service, such as a printer, a toaster, a marriage agency, etc. There is a client which would like to make use of this service. Thirdly, there is a lookup service (service locator) which acts as a broker/trader/locator between services and clients. There is an additional component, and that is a network connecting all three of these, and this network will generally be running TCP/IP. The Jini specification is fairly independent of network protocol, but the only current implementation is on TCP/IP.
Code will be moved around between these three pieces, and this is done by serializing the objects (and marshalling them to allow storage without reconstitution), and using Java's socket support to send and receive objects. In addition, objects in one JVM (Java Virtual Machine) may need to invoke methods on an object in another JVM and this will typically be done using RMI (Remote Method Invocation), although the Jini specification does not require this (it may require RMI semantics, but not necessarily implementation.
Both services and clients need to find lookup services. Discovering a lookup service may be done using unicast or multicast protocols. Unicast discovery is a synchronous mechanism. Multicast discovery is an asynchronous mechanism that requires use of a listener to respond when a new service locator is discovered.
When a service locator is discovered, it sends a ServiceRegistrar
object to run in the client or service. This acts as a proxy for the locator.
This object may be queried for information, such as the host the service locator
is on. Sun supply a lookup service called reggie
as part of Jini.
A service uses the ServiceRegistrar
object returned as a proxy
from a locator to register itself with that locator. The service prepares a ServiceItem
that contains a service object and a set of entries. The service object may be a
proxy for the real service. It registers this using the register
method of the ServiceRegistrar
object.
Information about a registration is returned as a ServiceRegistration
object. This may be queried for information such as the lease and its duration.
A client prepares a ServiceTemplate
which is a list of class
objects and a list of entries. For each service locator that is found, the
client can query this using the ServiceRegistrar
object's lookup()
method, to see if the locator has a service matching the template. If the match
is successful, an object is returned that can be cast into the class required.
Service methods can then be invoked on this object.