|
|
|
|
A very simple Java application looks like this:
Program StructureEvery Java program contains a number of classesA class has:
This means that it:When you run an application, you specify the name of a main class. If that class doesn't have a main method with the proper signature -- Error! Objects, ObjectsEverything in a Java program is an object (except for the primitive data types).In the above HelloWorld example, the main method's body calls the println method of the System.out object. Command Line ArgumentsThe single argument to main contains all the command line argumentsUnlike C and C++, the command line arguments do not include the program name
If the above program is run as follows: java [options] EchoArguments fee "fi fo" fum 12 Englishmen at 0.5 apiece will output: 8 arguments passed: 'fee' 'fi fo' 'fum' '12' 'Englishmen' 'at' '0.5' 'apiece' Note that, normally, command line arguments are separated by whitespace. However, you can enclose a single argument within quotes to override this behavior. Program Exit ValueIf you wish to return a numeric value to the system, as a normal C or C++ program would, you must call the System.exit method:
Environment:Java does not support operating system environment variables -- that's non-portableHowever, Java does support a platform-independent system properties list:
When I ran the above program on my system (from within NetBeans), it gave me the following output: HOME = C:\Documents and Settings\Bryan Higgs CLASSPATH = C:\Documents and Settings\Bryan Higgs\My Documents\Java Projects\Test\build\classes -- listing properties -- java.runtime.name=Java(TM) 2 Runtime Environment, Stand... sun.boot.library.path=C:\Program Files\Java\jdk1.5.0\jre\bin java.vm.version=1.5.0-b64 java.vm.vendor=Sun Microsystems Inc. java.vendor.url=http://java.sun.com/ path.separator=; java.vm.name=Java HotSpot(TM) Client VM file.encoding.pkg=sun.io user.country=US sun.os.patch.level=Service Pack 2 java.vm.specification.name=Java Virtual Machine Specification user.dir=C:\Documents and Settings\Bryan Higgs... java.runtime.version=1.5.0-b64 java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment java.endorsed.dirs=C:\Program Files\Java\jdk1.5.0\jre\li... os.arch=x86 java.io.tmpdir=C:\DOCUME~1\BRYANH~1\LOCALS~1\Temp\ line.separator= java.vm.specification.vendor=Sun Microsystems Inc. user.variant= os.name=Windows XP sun.jnu.encoding=Cp1252 java.library.path=C:\Program Files\Java\jdk1.5.0\bin;.;... java.specification.name=Java Platform API Specification java.class.version=49.0 sun.management.compiler=HotSpot Client Compiler os.version=5.1 user.home=C:\Documents and Settings\Bryan Higgs user.timezone= java.security.policy=applet.policy java.awt.printerjob=sun.awt.windows.WPrinterJob file.encoding=Cp1252 java.specification.version=1.5 user.name=Bryan Higgs java.class.path=C:\Documents and Settings\Bryan Higgs... java.vm.specification.version=1.0 sun.arch.data.model=32 java.home=C:\Program Files\Java\jdk1.5.0\jre java.specification.vendor=Sun Microsystems Inc. user.language=en awt.toolkit=sun.awt.windows.WToolkit java.vm.info=mixed mode, sharing java.version=1.5.0 java.ext.dirs=C:\Program Files\Java\jdk1.5.0\jre\li... sun.boot.class.path=C:\Program Files\Java\jdk1.5.0\jre\li... java.vendor=Sun Microsystems Inc. file.separator=\ java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport... sun.cpu.endian=little sun.io.unicode.encoding=UnicodeLittle sun.desktop=windows sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+m... As you can see, even the default system properties provide quite a bit of information. You can also pass name/value pairs into the Java program by using the -D option on the command line. |
|
This page was last modified on 02 October, 2007 |