Example: confidence

Supplement I.C Creating, Compiling and Running Java ...

Supplement Creating, Compiling and Running java programs from the Command Window For introduction to java Programming By Y. Daniel Liang This Supplement covers the following topics: Opening a Command Window ( ). Using Simple DOS Commands (create, change directories, display files, delete files and directories) ( ). Creating and Editing programs Using Notepad and WordPad ( ). Compiling and Running programs ( ). Common Errors ( ). introduction Sun releases each version of java with a java Development Toolkit (JDK). This is a primitive command-line tool set that includes software libraries, a compiler for Compiling java source code, an interpreter for Running java bytecode, and an applet viewer for testing java applets, as well as other useful utilities.

© Copyright Y. Daniel Liang, 2012 1 Supplement I.C Creating, Compiling and Running Java Programs from the Command Window For Introduction to Java Programming

Tags:

  Programs, Introduction, Running, Java, Compiling, Introduction to java, Compiling and running java programs

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Supplement I.C Creating, Compiling and Running Java ...

1 Supplement Creating, Compiling and Running java programs from the Command Window For introduction to java Programming By Y. Daniel Liang This Supplement covers the following topics: Opening a Command Window ( ). Using Simple DOS Commands (create, change directories, display files, delete files and directories) ( ). Creating and Editing programs Using Notepad and WordPad ( ). Compiling and Running programs ( ). Common Errors ( ). introduction Sun releases each version of java with a java Development Toolkit (JDK). This is a primitive command-line tool set that includes software libraries, a compiler for Compiling java source code, an interpreter for Running java bytecode, and an applet viewer for testing java applets, as well as other useful utilities.

2 JDK 7 can be downloaded from See Supplement , Installing and Configuring JDK, on how to install JDK 7. Opening a Command Window Using JDK from Windows, you have to type the commands from the command window. Assume you have successfully installed JDK. Start a Command window by clicking the Windows Start button and choosing Run to open the Run dialog box, as shown in Figure Enter cmd in the Open field and click OK to display a command window, as shown in Figure 1. Copyright Y. Daniel Liang, 2012. The Run dialog Figure You can start the command window from the Windows Start button.

3 You will see the command prompt window displayed as shown in Figure Figure You can type the JDK commands in the command window. Now type javac in the DOS prompt. If your JDK is configured properly, the command displays the usage of the command, as in Figure Otherwise, an error message is displayed to indicate that the command is not found, as shown in Figure Please refer to Supplement , Installing and Configuring JDK, to configure JDK 7. 2. Copyright Y. Daniel Liang, 2012. Figure The javac command displays the usage of the command.

4 Figure The javac command is not found if JDK is not properly configured. Simple DOS Commands To work with JDK on Windows, you need to know some simple DOS commands. Here are several frequently needed commands: dir -- Lists all files in the directory. mkdir dirName -- Creates a new directory named dirName. cd dirName -- Changes to the specified directory. For example, cd c:\ changes to the directory c:\. cd .. -- Changes to the parent directory. del filename Deletes a file. For example, del deletes the file named in the current directory.

5 Del *.* Deletes all files in the directory. [Caution: files deleted from the command window cannot be recovered.]. rmdir dirName Deletes the specified directory dirName. 3. Copyright Y. Daniel Liang, 2012. type Displays the contents of the specified file. Creating and Editing programs Using Notepad and WordPad You can create programs using Windows Notepad or WordPad. To do so, first open a command window; change the directory to where your programs should be stored, and type notepad or write to create a file for java source code.

6 For example, the following command starts Notepad to create or edit : notepad If does not exist, a dialog box is displayed to alert you that it is a new file, as shown in Figure Figure The Notepad alerts you that the specified file is new. Click Yes to create a new file. You can type the code for the program in the Notepad, as shown in Figure Choose File, Save to save the file. Figure You can use Notepad to create and edit java source files. Compiling and Running java programs To compile the source code, use the javac command.

7 For example, the following command compiles : 4. Copyright Y. Daniel Liang, 2012. javac To run the class, use the java command. For example, the following command runs Welcome: java Welcome Figure shows a sample run. Figure Use the javac command to compile and java to programs . Common Errors Question 1. I tried to run the program from the DOS prompt, but got exception. What is wrong? Answer: The class file is not found. There are several reasons: 1. the class has been compiled; 2. the classpath was not set properly. You need to set classpath to.

8 ;%classpath%. 3. If the class has a package statement, you didn't invoke it with the full class name including the complete package name. For example, if the class path is c:\liang and the package for the class is chapter1, you have to type java from the c:\liang directory. Question 2. I tried to run the program from the DOS prompt, but got exception. What is wrong? Answer: The class does not have a main method or the main method signature is incorrect. 5. Copyright Y. Daniel Liang, 2012.


Related search queries