Download cs303assignment9background

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
CS 303, Assignment 9, Background
The explanations here are given with reference to the ClickHand application and
applet. There are links on the course Web page to several files which illustrate the
outcome of the processes described.
Turning an application into an applet and putting it into a Web page
To turn an application into an applet, take the following steps:
1. import java.awt.* and javax.swing.*.
2. Change the class to an applet by having it extend JApplet.
3. Replace the main() method with an init() method. The contents of the init() method
will not be the same as the contents of the main() method.
4. Save the contents of the Frame class constructor for future reference, but otherwise
remove this class and all references to it, or machinery associated with it, from the code.
5. Put the code for creating the contents of the frame (panels, etc.), taken from the Frame
class constructor, into the init() method.
To get the applet into a Web page, you need to have an html file with the correct
codes in it. Here is a simple example. This is the html source code you would find in the
course Web page for the link to the ClickHandApplet. Notice that sizing the applet is
done in the html since the applet does not have a frame to size.
<html>
<title>ClickHand as an Applet</title>
<body>
Here is ClickHandApplet.
<applet code="ClickHandApplet.class" width="500" height="500">
</applet>
</body>
</html>
2
Turning an application into a self-executable .jar file
It is convenient to be able to group together the components of an application in a
single file which can be run without having to open it as source code and compile it first.
.jar files can be used to make these kinds of self-executable files in Java. The directions
below explain how to do this. It is also possible to create files of the .jar type which
serve other purposes. When trying to make a self-executable .jar file, you may
accidentally create one of these other kinds of .jar file. Remember to check your results
to make sure you got what you wanted. To turn an application into self-executable .jar
file, take the following steps:
1. Compile the source code of the application so that .class files are produced.
Compilation of a given set of source files might result in more than that number of .class
files, depending on how many classes were defined in the source files. All of the
resulting class files are needed.
2. Write a manifest file. A manifest file has to have the extension .mf. The file can be
given any name. For this example, I used the name mainclass.mf. In the manifest file
you specify which class in the application is the main class. The .mf file for this example
contains:
Main-Class:
ClickHand
It is important to note two things about this: The file name ClickHand does not take an
extension. However, it is followed by a new line. The .mf file won’t work correctly if its
contents are not followed by a new line.
3. Assuming that all of the class files resulting from compilation are sitting together in a
single directory and that it’s possible to run executables from the Java bin directory from
that location, you create the self-executable .jar file by entering the following command
at a command prompt for that directory:
jar cfm ClickHand.jar mainclass.mf *.class
If desired, it would be possible to list all of the class files rather than using the wildcard
expression *.class. The successful outcome of this command is a new file,
ClickHand.jar, in the same directory.
You may have problems running the jar command depending on how Java was
installed on your system. It is possible that it won’t run from the directory where you
have compiled your application. One solution to this problem is to try and fix the path.
The quick and dirty solution is to copy all of the needed files into the bin directory of the
Java installation on your computer. This is where the jar utility is located and it will run
from a command prompt in the bin directory. The shortcoming of this solution is that
you clutter up the Java program directory with your files and you have to be careful when
cleaning up so that you don’t delete any of the Java system files.