Introduction to Java IEEM241 Routing and Fleet Management Feb 3, 2005
Agenda Installing and set PATH environment variables First Cup of Java –HelloWorldApp.java Java basics File I/O Reference
Installing Java In IS Lab –WinXP: all are installed with the J2SE Software Development Kit (JDK) –Win98: Check C:\ drive, whether there is a folder named something like j2sdk?????, i.e. c:\j2sdk1.4.2_06 If not, double click the installation file on D:\ drive. The filename is something like j2sdk-1_4_2_06- windows-i586-p.exe.
Installing Java (cont’d) For your own PC –Download Download J2SE SDK(JDK) In UST, 1_4_2_04-windows-i586-p.exehttp:// /ieem241/j2sdk- 1_4_2_04-windows-i586-p.exe
Update the PATH environment variable For Windows 2000/XP: –Start Settings Control Panel double-click System ->Advanced tab Environment Variables –Look for "Path" in the User Variables and System Variables. –Add c:\j2sdk1.4.2_ \bin; at the end of PATH –Click "Set", "OK" or "Apply –Run new Command Prompt window to take effect
Update the PATH environment variable (cont’d) For Windows 98: –In IS Lab Open a command window Type set PATH=%PATH%; c:\j2sdk1.4.2_ \bin; Notice: DO NOT close this window –For your own PC Edit autoexec.bat in C: Add c:\j2sdk1.4.2_ \bin after PATH = Save the file and reboot the system (In IS Lab we cannot do this. All files on C:\ will be recovered after rebooting.)
First Cup of Java: create source file –Use notepad or other java developer software, type in the following code Save as HelloWorldApp.java in a directory Notice 1: File name should be the same as the class name Notice 2: Case Sensitive, HelloWorldApp helloworldapp
First Cup of Java: Compile the source File Start a command window –For WinXP/2000, start a new command prompt window –For Win98, use the same window that you set the PATH environment variable, or open a new one and reset the PATH Change to the directory which you save the.java file (e.g cd c:\java) Type javac HelloWorldApp.java Type dir, you can see a file called “ HelloWorldApp.class ” was created
First Cup of Java: Running it! –In the same directory, enter at the prompt: java HelloWorldApp –Now you should see:
First Cup of Java: Errors –Bad command or file name(in Win98) –'javac' is not recognized as an internal or external command, operable program or batch file. (in WinXP) –Exception in thread “main” java.lang.NoClassDefFoundError –Check if you have typed a wrong file name –Check the PATH environment variable –Check whether the file is in the current directory
Java Basics: comments –Comment Type Document comments /** */ /* text */ and //
Java Basics: Class Each.java file should contain a class with the same name as the file name.
Java Basics: main The main method –Meaningful when public class if there are more than 1 class in this file –Should be verbatim in this form
Read & Write File Notice 1: “import” is like “#include” in C++ Notice 2: DO NOT forget the try-catch clause Read & write a file line by line package
Read & Write File Can only read/write files character by character
In Class Exercises Bubble Sorting Generating a simple report Generating a calendar All source files can be downloaded from If your PC does not have UltraEdit-32 installed, please download from
Reference –The Java Tutorial ation/download.html –Bruce Eckel, Thinking in Java, 3 rd Edition –Java course website