Applications are java programs that run from the command line. All operating systems provide the facility to run such programs.
Applications are run using the java command - notice, with no extension
On Unix machines you are "at the command line" once you log in (typically the $ prompt). On Windows machines you need to get into DOS. See this page for DOS assistance. Particularly note difference in slashes and command differences (ls and dir, cat/more and type)
Typically, you cannot start an application on a remote machine from your PC.
When
java myProgram
is called, Java looks in the myProgram file for its main
method.
Applications do not instantiate from any superclass
or browser, so the programmer must explicitly instantiate the
class ... usually in the main(String[] args) method
For applications, you can provide arguments to the program from the command line:
java myprog arg1 arg2 arg3
(note: no extension of .class or .java)
arg1 arg2 arg3 will be the values of the parameters you want to send. For example: java Main 5
Note that the main method in the class called receives these arguments.
Hence you would need to access the args array to get these values. Specifically,
args[0] (in the main method) would be whatever is typed above for arg1 in the myprog call. In the second example, arg[0] will equal the String 5.
One often sees
a first line of code like
if (args[0] != null)
This allows the program to do one thing if there are no parameters, and something else if there are parameters.
Hmmm, consider if (args.length > 0) ... discuss the difference and array out of bounds
Also, note the parameters of main are String[] args . To make an String into an int, consider using Integer.parseInt(String s) (Similar methods in other wrapper classes)
Other ideas: Integer.valueOf(args[0]).intValue();
Since you are not inheriting from Applets now,
applications use more conventional backgrounds...
Frame allows the user to put Menus on them, both via MenuBar and
PopUpMenu
In PlayBalloon.java we see
an old familiar Applet made into an application. This is really lazy of
the text...it is using Java 1.0. Discuss how I know and what it
means...
This application is then enhanced to have a menu bar (done correctly)
Check out this old familiar code
When we wanted to print things on Applets, we used the Graphics area and the command: For applications the output will typically go to the "default" output.
See the java.lang.System class and its Class Variables. (static)
Particularly for our purposes You will see the output on your console (the same place that you started your application with the java command.)
It is a good programming practice to not rely on an
individual Operating System for Windowing options. Always
provide your user with an exit menu option.
WordProcessor.java shows
code for menus - note that it does not process any words...and that it
is java 1.0 code and thus will cause deprecation warnings. Again, how do I know this?
The GUI and Frames
Remember the Balloon Applet class? Here we will convert it to
an application.
Discuss clean OO code and Main.java.
Specifically, the class Frame.
Here was the Applet version using interfaces for controllers.
Output
g.drawString(String str, int x, int y)
What do you suppose this would be? You can use System.out.println() commands in an Applet too. Where would you see the output?
See for more on applications
Closing: System.exit(0)