Download CS 303, Assignment 8, Background

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 10, Background
The explanations here are given with reference to the ClickKey application and
applet. There are other links on the Web page illustrating 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, which is what is used on the Web page. Notice
that sizing the applet is done in the html since it no longer has a frame to size.
<html>
<title>ClickKey as an Applet</title>
<body>
Here is ClickKeyApplet.
<applet code="ClickKeyApplet.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 the source code and
compile it first. .jar files can be used to make these kinds of self-executable files in Java.
Follow the directions below to do this. It is also possible to create .jar files 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 see if you
have obtained the result that you want. To turn an application into an applet, 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. If it is to be a manifest file, it has to have the extension .mf.
The file can have 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:
ClickKey
It is important to note two things about this: The file name ClickKey does not have 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 results of compilation are sitting together in a single location
and that it’s possible to run executables from the Java bin directory in that location, you
create the .jar file by entering the following command at a command prompt:
jar cfm ClickKey.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, ClickKey.jar,
in the same directory.
It is important to note that you may have problems running the jar command
depending on how Java was installed on your system and what the path is like. 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 to
this 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 a 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.