DOC PREVIEW
CORNELL CS 211 - Applications and Applets

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Recitation 09. Applications and AppletsJava applicationsThose who took CS100J and CS100M in fall 2003 maynot have had much experience with Java applications.A Java application is simply a collection of classes oneof which contains one method with this header:public static void main(String[] pars)In a terminal or DOS window, one executes an appli-cation whose method main is in class C (file C.java) bynavigating to the directory containing the program filesand executingjava CYou can type this in the DrJava Interactions pane, too.The argument to main can be an array of Strings, andmethod main can use these as input to it. We don’thandle that here.jar files (Java Archive files)A program may consist of many classes, generally inthe same directory. If you want to give someone yourprogram, you have to give them all the .class files —zipthem up into a .zip file— and the someone must thenunzip them. That's messy. To make things easier, youcan make a jar file of the classes. Jar stands for JavaARchive; after tar files (TapeARchives) on Unixsystems.To make a jar file, get into a DOS or command-linewindow and navigate to the directory where the .javaand .class files are. Then, type in this command:jar -cf file-name.jar *.classThe c is for create. The f is for file and indicates thatthe name of the file to create follows: file-name.jar. The*.class is expanded to name all the .class files in thedirectory. So, this command makes up a jar file namedfile-name.jar that contains all the .class files in thedirectory.You still have to insert into the jar file something thattells it which class has method main. Suppose it is classMainClass. Then do the following:(a)Make up a file x.mf that contains one line of text init:Main-class: MainClassThe suffix on x.mf stands for manifest. You actuallydon't have to use the suffix. Make sure you hit the enterkey after typing the text; there must be a carriage-returnor line-feed in it. You can create this file in wordpad ornotepad or DrJava or any editor you want. Make surethe file is in the same directory as file file-name.jar.(b) Type this command in the window:jar -umf x.mf file-name.jarThe u stands for update, the m for manifest, and the ffor file. Since the m comes before the f, the manifestfile name, x.mf, comes before the file name. Thiscommand inserts into jar file file-name.jar the fact thatmethod main appears in class MainClass.You can do both steps together —insert the classes andthe main-class indication— by using the commandjar -cmf x.mf file-name.jar *.classYou can now email file file-name.jar to anyone, andthey can run it on their computer, whether it has a Unix,Macintosh, or Windows system, as long as their systemhas Java in it. To execute the program in the jar file,type this line (yes, include the extension .jar):java!-jar file-name.jarIn some systems, you will also be able to run theprogram just by double-clicking on the jar file.If you want to see what is in jar file file-name.jar, thentype this:jar tvf file-name.jarYou can find out more about the jar command bytyping simply jar and hitting the return/enter key.2Recitation 09. Applications and AppletsApplets. An applet is a Java program that is started bya browser (e.g. netscape or internet explorer) when anhtml file has a suitable applet tag. Here, we explore thedifference between applications (the kind of programswe have been writing) and applets.Classes Applet and JApplet. Any Java program that isto be executed by a browser has to be a subclass ofeither Applet (in package java.awt) or JApplet (in thenewer Swing package, javax.swing).(Class JApplet is asubclass of Applet).Here is a tutorial on writing Japplets:http://java.sun.com/j2se/1.3/docs/api/index.htmlAn application is started by calling static method main,right?An applet, instead, has five methods, all of which arenonstatic, that are called by the browser at varioustimes. Som of these should be overridden for the appletto do its job. The inherited versions of these method donothing (but very fast).• Method init() is called to tell the Java program that ithas been loaded into the system. It is always called be-fore the first time that method start is called. Overridethis to perform initialization (e.g. start a thread)..• Method start() is called by the browser to inform thisapplet that it should start its execution. It is called aftermethod init and each time the applet is revisited in aWeb page. (E.g. if the applet window is hidden, methodstop is called, which could stop anything that the appletis doing, like providing an animation. When thewindow becomes visible again, method start is called,and it can continue execution).• Method stop() is called when the applet should stopwhatever it is doing because the applet is no longervisible --see the discussion of method start.• Method destroy is called just before the applet isterminated by the browser. In method destroy, theapplet can kill its resources --e.g. threads that werestarted in method init.• Method paint(Graphics g). An applet occupies a spacein the window for the html page. It can be painted, justlike any Panel.Our applets will not use start, stop, and destroy.An html file resides on one computer and is sent to yourcomputer, where the browser loads it and shows it in awindow. The applet typically is on the same computer(in the same folder) as the html page. It, too, is sent toyour computer, where your browser executes it.You know about tags like <p> and </b> on html pages.Consider these two tags<applet codebase="Java Classes"code="TrivialApplet.class"width=200 height=200></applet>These tell the browser to start a Java applet, which is inTrivialApplet.class. The applet is a 200x200 pixelsquare.The argument codebase=“Java Classes” tells thebrowser that the .class files for this program are infolder “Java Classes”, which must be in the same folderas the html page that contains the tag.If there are a lot of class files, the above can beinefficient, because each file is individually retrievedfrom the place where the html file resides and broughtto your compueter. You can have CodeWarrior store allthe .class files in compressed form in a single file calleda “jar” file, so that only one file has to be retrieved. Todo this, in Codewarrior, click on the target setting icon,select “Java output”, and then set the “Output type” to“Jar File”. Then, Codewarrior will produce a jaf filesinstead of a folder of .class files.Then, use these


View Full Document

CORNELL CS 211 - Applications and Applets

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download Applications and Applets
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Applications and Applets and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Applications and Applets 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?