main : Java Glossary
home M words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (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 be sure:
public static void main ( String[] args )
   {
   }

Most of the interesting stuff happens in a GUI app only after main has exited. The AWT 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.

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.


CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.18] Spread the Net
You are visitor number 18,119.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/main.html J:\mindprod\jgloss\main.html