Java Applications and Applets

 

Introduction.  For our first semester we will focus on developing Java applets and/or applications.  Next semester we will get involved more in JSP and Servlets.  There are even more differences between applets and applications that will become more apparent as we go through the semester.  But remember, 

 

applets have been designed to be invoked by a client's browser on the web

 

applications are going to be run as stand-alone programs on someone's computer

 

So Java isn't just for developing applets.

It is also important to remember that getting applets to work from the web through the client's can be much more difficult than advertised in the Java hype.

Compilation and Interpretation.  In order for Java programs to obtain platform independence Java code is not compiled into an executable.  Java compilers actually create what is called bytecode.  This bytecode is found in what are called class files.

  • Java code is found in .java files

  • Java bytecode is found in .class files

Java bytecode is then interpreted at run time by something on a particular computer.  How this is done depends on the program.  For example, it may be done with a JVM - Java Virtual Machine within a browser.  Though we have all probably heard that different browsers and different corporations versions of Java Virtual Machine can cause some difficulties!

Application or Applet?  Now for the big, not entirely well-defined, question.  According to Deitel & Deitel, an application is a program that executes in the command window.  In Windows, depending on your version, this is the MS-DOS Prompt or Command Prompt environment window.  

  1. You must first edit the text file to develop the code and save it with some name FileName.java.

    1. This FileName must be the same as the name of the overall class within the file

  2. Then the command  javac  is giving to compile the code FileName.java and create a new file called FileName.class.

    1. In order for this to work your operating system must be able to find the Java compiler which is in another directory

    2. Depending on your version of Windows, this is the reason for either putting a PATH in your autoexec.bat or changing some variables in your user environment.

  3. Then the programs are loaded into memory and executed using the interpreter using the  java FileName command.

  4. The results show up in the command window for the users type of interface.

It is certainly possible to improve on this interface both from the point of view of you as a developer and a user.  The Java Swing class has a number of built-in objects we will quickly use to improve interactivity with the user.

On the other hand, for an applet, the steps are slightly different.

  1. You must first edit the file to develop the code and save it with some name FileName.java.

    1. This FileName must be the same as the name of the overall class within the file

  2. Then the command  javac  is giving to compile the code FileName.java and create a new file called FileName.class.

    1. In order for this to work your operating system must be able to find the Java compiler which is in another directory

    2. Depending on your version of Windows, this is the reason for either putting a PATH in your autoexec.bat or changing some variables in your user environment.

  3. You need to create an HTML file called FileName.html that refers to FileName.class.

  4. The you execute the HTML file using the appletviewer or some browser.

  5. The results show up in the either the browser or the minimal browser called the appletviewer.

We will now work examples of each type in class and then develop them further throughout the semester.

Working in the Command Window.  Your are likely to be unfamiliar with moving around in a command line environment.  First you need to access either the MS-DOS Prompt or the Command Prompt.  You should get a window that looks something like the following.

 

 

At this point you need to move to the directory where you have the program files.  One way to move back towards the root directory on the C: drive is to type

cd ..

This will move you one directory back towards the root  \  as illustrated in the following image.

 

 

You are likely to realize that you would like to move more quickly to the directory you desire.  The following command will move me to my JavaP directory off the root, into its subdirectory called MessageBoxes.

cd  \JavaP\MessageBoxes

This is illustrated in the following image.

 

While there are many other commands you could give at the prompt, including those to compile and or execute Java programs, you may want to list the files in this directory.  This is done with the following command.

dir

This is illustrated in the following image.

 

We won't spend any more time on command line commands in Windows.  These few will suffice, though there are others and there are modifications of these.