1 / 46

Object-Oriented Programming (Java) Unit 21

Object-Oriented Programming (Java) Unit 21. Kirk Scott. Applets and Self-Executable .jar Files. 21.1 Turning an Application into an Applet and Putting It into a Web Page 21.2 Turning an Application into a Self-Executable .jar File. Night Monkey, South America.

adora
Télécharger la présentation

Object-Oriented Programming (Java) Unit 21

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming (Java) Unit 21 Kirk Scott

  2. Applets and Self-Executable .jar Files • 21.1 Turning an Application into an Applet and Putting It into a Web Page • 21.2 Turning an Application into a Self-Executable .jar File

  3. Night Monkey, South America

  4. 21.1 Turning an Application into an Applet and Putting It into a Web Page • Simple applets were introduced in Unit 5. • It is possible to turn a graphical application into an applet. • This is illustrated here with the ClickHand application. • To turn an application into an applet, take the following steps:

  5. 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.

  6. The init() method should contain the code from the frame class constructor that creates the contents of the frame. • It cannot contain any code that refers to the frame, such as giving it a title or a size. • 4. Remove the frame class and all references to it from the code.

  7. An illustration follows • Here are the main() method and the frame class of the ClickHand application before being changed to an applet • It is not necessary to show the rest of the code, because it is not affected by the conversion

  8. import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • import java.awt.Graphics2D; • import java.awt.Rectangle; • import java.awt.geom.Ellipse2D; • import java.awt.geom.Point2D; • import java.lang.*; • import java.util.*;

  9. public class ClickHandBefore • { • public static void main(String[] args) • { • ClickHandFrame myframe = new ClickHandFrame(); • myframe.setVisible(true); • } • }

  10. class ClickHandFrame extends JFrame • { • private ClickHandPanel myPanel; • private final int FRAMEW = 500; • private final int FRAMEH = 500; • public ClickHandFrame() • { • setTitle("ClickHand Frame"); • setSize(FRAMEW, FRAMEH); • myPanel = new ClickHandPanel(); • Container contentPane = getContentPane(); • contentPane.add(myPanel, "Center"); • addWindowListener(new WindowCloser()); • }

  11. private class WindowCloser extends WindowAdapter • { • public void windowClosing(WindowEvent event) • { • System.exit(0); • } • } • }

  12. This is how the code looks after the conversion • Things are shown as commented rather than being completed removed • A complete, runnable version of this code is available separately.

  13. import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • import java.awt.Graphics2D; • import java.awt.Rectangle; • import java.awt.geom.Ellipse2D; • import java.awt.geom.Point2D; • import java.lang.*; • import java.util.*;

  14. public class ClickHandAfter extends JApplet • { • /* • public static void main(String[] args) • { • ClickHandFrame myframe = new ClickHandFrame(); • myframe.setVisible(true); • } • } • class ClickHandFrame extends JFrame • { • private ClickHandPanel myPanel; • private final int FRAMEW = 500; • private final int FRAMEH = 500; • public ClickHandFrame() • { • setTitle("ClickHand Frame"); • setSize(FRAMEW, FRAMEH); • */

  15. public void init() • { • ClickHandPanel myPanel = new ClickHandPanel(); • Container contentPane = getContentPane(); • contentPane.add(myPanel, "Center"); • } • } • /* • addWindowListener(new WindowCloser()); • } • private class WindowCloser extends WindowAdapter • { • public void windowClosing(WindowEvent event) • { • System.exit(0); • } • } • } • */

  16. 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 for ClickHand • The html requires the existence of a compiled class file for the applet • The html shown below is written under the assumption that the class file is in the same folder as the html file • Notice that sizing the applet in the Web page is done in the html since the applet does not have a frame.

  17. <html> • <title>ClickHand as an Applet</title> • <body> • Here is ClickHandApplet. • <applet code="ClickHandApplet.class" width="500" height="500"> • </applet> • </body> • </html>

  18. 21.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 • Such a file is known as a self-executable • jar files, which have the extension .jar, can be used for this purpose in Java

  19. The abbreviation jar stands for Java Archive. • In general, jar files are compressed, although the compression option can be turned off. • It is possible to create a jar file simply for the sake of compression as well as creating one for the sake of self-execution

  20. When making a self-executable jar file it is easy to make a small mistake and end up with something that doesn’t work • Remember to check your results to make sure you got what you wanted • To turn an application into a self-executable jar file, take the following steps:

  21. 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

  22. 2. Make sure that you are doing this with an application, not an applet. • Applets have init() methods instead of main() methods • A self-executable has to have a class containing a main() method in order to work

  23. 3. Write a manifest file • A manifest file should have the extension .mf • The file can be given any name

  24. In the manifest file you specify which class in the application is the main class • The manifest file for this example has the name mainclass.mf, and it has these contents: • Main-Class: ClickHand

  25. It is important to note three things about the contents of this file • 1) The file name ClickHand does not take an extension • 2) The file name ClickHand is followed by a new line. • The manifest file won’t work correctly if its contents are not followed by a new line. • 3) There is only one space between the colon and the file name ClickHand

  26. There is also one other word of warning: • Depending on the editor you use to make the file and the operating system environment, it is possible to create a file which has a .mf extension, but which has been saved as a .txt file. • If later on the jar utility claims that it can’t find the manifest file, double check whether or not the file was saved with the right extension and type.

  27. Assume that the path on your system is set up so that you can run executables from the Java bin folder at any location • Also assume that all of the class files resulting from compilation are sitting together in a single folder along with the manifest file

  28. Then you create the self-executable jar file by entering the following command at a command prompt for that folder: • jar cvfm ClickHand.jar mainclass.mf *.class

  29. 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 folder.

  30. In case you’re curious, the options cvfm stand respectively for the following: • c: Create. This says that you are using the jar command to create a jar file. • v: Verbose. This says that an informational message will be printed at the command prompt as each class file is added to the jar file.

  31. f: File. This says that the name of the jar file, ClickHand.jar, is specified after the options. • m: Manifest. This says that the name of the needed manifest file is specified after the name of the jar file.

  32. It is also possible to add a 0 (zero) to the option list. • This will cause the jar file to be created without compression. • There are other options, but they are not needed at this time.

  33. If the jar file is successfully created and the operating system is cooperative, it should be possible to run the self-executable by double clicking on its icon • Even if the operating system isn’t cooperative, if the jar file has been successfully created, it should be possible to run it from the command prompt by entering the command shown on the next overhead

  34. This will work at a location where it is possible to run commands in the Java bin folder • If you have to spend much time at the command prompt, it can be convenient to use this command to check your work from the keyboard without having to switch back to the mouse. • java –jar ClickHand.jar

  35. You may have problems running the jar command for creating a jar file and the java command for running a jar file depending on how Java was installed on your system • It is possible that they won’t run from the folder where you have compiled your application • The general solution to this problem is to make sure that the Java bin folder, the one containing commands like jar and java, is part of the path on your system • Depending on what version of Windows you're using, these are the steps for accomplishing this:

  36. These are the steps for accomplishing this: • 1. Go to: Start/Settings/Control Panel • 2. Click this icon/link: Performance and Maintenance • 3. Click this icon/link: System • 4. Click on this tab: Advanced

  37. 5. Click this button at the bottom: Environment Variables • 6. Highlight the PATH entry and click on the edit button • 7. Add this PATH entry: C:\Program Files\Java\jdk1.6.0\bin

  38. Even if you're using a different/more recent version of Windows, the idea is the same. • The only catch is finding the Environment Variables. • If you don't find an icon for Performance and Maintenance, you might simply use the "Search Control Panel" option if it is present in the upper right hand corner. • Type in "Environment Variables" and you might be given a link directly to the location where they can be changed.

  39. Notice that the different entries in the PATH are separated by semicolons • You don't want to mess up any entry that's already there • Also, point 7 above assumes that you have a standard installation of Java on your system • You should double check the path to the bin folder • You may also have a different version of Java installed, so you would want to look that up and use that version rather than jdk1.6.0 in the PATH

  40. If you don't want to mess with the PATH environment variable, there is a quick and dirty solution to the problem • The jar and java commands are located in the Java bin folder and they will run from a command prompt there • You can copy all of the files needed for making the jar file into the bin folder of the Java installation on your computer

  41. This includes both the class files from a successful compilation and the manifest file • Alternatively, you might decide to copy all of the source files to that folder and compile the application there • Then you can run the commands to create the jar file

  42. The shortcoming of this solution is that you clutter up the Java program folder with your files and you have to be careful when cleaning up so that you don’t delete any of the Java system files • There are two last words to the wise concerning this approach • You will probably find the Java folder in the Program Files folder • In the Java folder you will probably find both a jdk folder and a jre folder

  43. Each will probably have a bin folder • You want the jdk folder, not the jre folder • By default this folder may be set to “read only” • If you will be producing class or jar files there, you will need to remove this restriction.

  44. The End

More Related