Google+

Like Us On Facebook


Total Pageviews

183. Java and the Windows Command Prompt






Pre-requisites -

1. Install Java (Refer Post#30)
2. Configure the Java (Refer Post#31)

Now lets try few commands one by one by following the below tasks -

Task 1 ->  Checking the version of the Java that is installed on your machine 

1. Launch Command Prompt on your windows machine as shown below -


2. Assuming that you have installed and configured Java as specified in the Pre-requisites section above, type the command 'java -version' in the command line  as shown below and press 'Enter' key on your keyboard -


3. Observe that the version of Java that we have installed on your system is displayed in the command prompt as shown below -


Task 2 -> Running a Java Program from Command Prompt 

1. Create a new folder say 'mywork' in your C Drive as shown below -



2. Open the folder, create & open a text file and write the following simple Java Program into the text file as shown below -



3. Select 'File' -> 'Save As' as shown below -


4. In 'Save As' dialog, select 'All Files' in the 'Save As Type' field, enter 'HelloWorld.java' into the 'File Name' field and click on 'Save' button as shown below -


5. Close the text document and observe that 'HelloWorld.java' got saved in the 'mywork' folder as shown below -



6. Launch Command Prompt window as shown below -


7. Enter the command 'cd \' to change the current directory to the root directory and press 'Enter' key as shown below -


8. Observe that the current directory got changed to the root directory as shown below -


9. Now change the current directory to C:\mywork by enter the command 'cd \mywork' as shown below and press 'Enter' key -


10. Observe that the current directory got changed to C:\mywork as shown below -


11. Now find the files available in the C:\mywork location by entering the command 'dir' as shown below and press 'Enter' key -


12. Observe that the 'HelloWorld.java' file is displayed as shown below -


13. Compile the Java File 'HelloWorld.java' by entering 'javac HelloWorld.java' as shown below and press 'Enter' key -


14. Observe that nothing is displayed except the new Prompt as shown below -


15. Now find the files in the C:\mywork location by entering the command 'dir' as shown below and press 'Enter' key -


16. Observe that the two files 'HelloWorld.java' and 'HelloWorld.class' are displayed (javac command has created the 'HelloWorld.class' file in step13)  as shown below -


17. Run the Java Program by entering 'java HelloWorld' as shown below and press 'Enter' key -


18. Observe the Java Program has printed 'Hello, World!' as shown below -



Task 3 -> Print all the command line arguments 

Pass the command line arguments to String args[] in  main( ) method and print them by writing the code in main( ) method by following the below steps -

1. Create a text document at C:\mywork with the following code as shown below -


2. Save the text document as  'CommandLine.java' and close the Text document

3. Compile the Java File 'CommandLine.java' in command prompt

4. Run the Java Program by passing the arguments one, two, three, four and five by entering 'java CommandLine one two three four five' as shown below and press 'Enter' key -


5. Observe that the command line arguments got printed as shown below -







Please comment below to feedback or ask questions.

For-each Loop will be explained in the next post.







No comments:

Blog Index