main : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

main
Standalone applications don’t necessarily have init and start methods. They must have a public static void main routine to start things off. The main routine typically creates some object of the master class. It can’t use any non-static variables or methods in the class with the implied this, since there is no this object. It typically quickly returns. Then the event loop processing starts. Even the static main method must live inside some public class, or at least in the top class, the first class and the class for whom the source file is named. There are no such thing as standalone static methods in Java. The signature of the main class must look exactly like this. Cut and paste it to
public static void main ( String[] args )
   {
   }

Most of the interesting stuff happens in a GUI (Graphic User Interface) app only after main has exited. The AWT (Advanced Windowing Toolkit) or Swing thread is still alive fielding keypresses and mouse clicks. To stop the application, you need to close all the GUI Frames down or call System.exit().

In Swing you can control this with JFrame.EXIT_ON_CLOSE.

Note that main does not return an int, as do C/C++ programs. In Java, you return an error code with System. exit( n );

Command Line

The arguments that main gets passed are the result of the both the command processor and the Java runtime cooking the command line. I strongly suggest dumping the parameters out surrounded in [] if you are in the least suspicious the parameters you are getting are not what you expect. The command processor does macro expansion (% in Windows) and wildcard expansion so that *.html becomes a list of individual files or directories matching the pattern.

The Java run time splits the command line parameters at spaces, except for parameters enclosed in quotes. It preserves the spaces inside the quotes and strips the quotes. The Java runtime treats quotes literally that are preceded by \. The rule seems to be \ is treated literally, except when you have a string of \ before a " then \ means the next \ is taken literally, but if the next character is a " then that quote is taken literally. I have never seen a stranger quoting scheme.

Command Line Quoting Scheme in Windows
To Get You must type Notes
E:\Program Files E:\Program Files You must surround the parameter in quotes if it contains a space.
E:\Program Files\ E:\Program Files\\ You must double a trailing \, but not embedded ones.
E:\Program Files\\ E:\Program Files\\\\ You must double each trailing backslash, but not embedded ones.
E:\Program Files\\\ E:\Program Files\\\\\\ You must double each trailing backslash, but not embedded ones.
E:\\Program Files E:\\Program Files Don’t need to double multiple embedded \s either.
E:\\\Program Files E:\\\Program Files Don’t need to double multiple embedded \s either.
E:\Program Files" E:\Program Files\ Not that you would ever want this. This is what happens if you carelessly have a single trailing \ on your parameter. This is very common in windows. It means a directory.
a " character a \ character" Parameters with embedded quotes must be preceded by a \.
a \" character a \\\ character" to get an embedded literal \ then you need \\\

One practical consequence of this is you should do your utmost to avoid a \ at the end of any parameter or set parameter, particularly a path or classpath. The penalty for failing to this will be for example that ant can’t understand any arguments you pass to it because its command line parsing is screwed up by the trailing \ when it mindlessly uses macro expansion of the classpath on its command line.

Unfortunately, trailing backslashes occur frequently in Windows, to describe drives, directories, especially in paths and classpaths. Given that \ is such a frequently used character in Windows it was not a good idea to snaffle it as a quoting character. It would have been better to just double up " I would be curious to find out how command line quoting works on other platforms. If it is the same, the use of \ is not so problematic since on other platforms, / is use in filenames.


This page is posted
on the web at:

http://mindprod.com/jgloss/main.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\main.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[18.222.164.7]
You are visitor number