1. Install JavaIf you dont have Java installed on your system click here to download Java from the Oracle website. Without Java, command Promt will not execute our program. So please proceed to step 2 after installing and configuring Java to your system. 2. Write your Java command in NotepadIn this example I will be creating a Java Class called knowledge along with a simple program that when executed in "Command Prompt" will display the phrase "Knowledge is Power". After writing the code above, the notepad file must be saved on the primary hard drive (e.g. C drive) and within another folder. I have named this folder "project". Note1] In order for command prompt to recognize this file you must save it as a Java file. to do so the name of the file must end with ".java". As such I will be naming this file "knowledge.java". Note2] You will need to keep the notepad document open while executing commands in Command Prompt. 3. Execute the JAVA program from Command PromptFirst we must go to the project directory using in the commands in the screenshot below: Next step is to compile our program. The purpose of compiling is to turn our program into a class file because the computer cannot read any ".java" files. In order to compile a java file we must use a command called "javac"which stands for Java Compiler. After executing the Javac command as depicted in the screenshot above, we must use the DIR command in order to verify that the correct execution of this command. As you could observe in my screenshot the "knowledge.class" line which did not exist before the compilation has now been added and this is indicative that the compilation has been successful. Moreover, if we go to the our C drive and look under the project folder an additional file called "knowledge.class" has been created there. We are know at the final step of successfully executing our java program. This step is as simple as typing "java" followed by name of our class. I.e. "java knowledge". Command promt has successfuly executed our program. "Knowledge is Power!"
0 Comments
Leave a Reply. |
CategoriesArchives
June 2020
|