Menu
Topics Index
...
`


Although Merit Campus provides the online compiler Try Java Online Compiler, it is more important to understand how to compile and run your programs on the local machine Using Command Prompt. Because in real time you will need to run your programs locally and to add to that the online compiler does not support complex programs.

For you to compile and run any Java program on your machine/computer/pc, you should have installed Java on it. Please find the instructions for installation of Java at Installation Of Java on your PC.

Steps for compiling and running a Java program

  • 1. Create a directory: Open the windows explorer(file explorer) and go to the D drive (or any other drive of your choice). Inside that create a directory called programs. This will be the directory you will use for storing all your java programs. This step you have to do only once per computer, it is not necessary to create a new directory for every Java program.
compilation create programs directory
  • 2. Create a new text document: Go to the programs directory in windows explorer and create a new text file by right clicking in the empty area and use the option 'New' -> 'Text Document'.
compilation right click menu for new text document
  • 3. Change the folder options (if required): If the newly created file is 'New Text Document' instead of 'New Text Document.txt', then you need to this additional step of changing the folder options to show the file extension. Please go to the menu option 'Organize' -> 'Folder And Search Options', it launches a dialog with name 'Folder Options'. Go to the tab 'View' and un-select the option 'Hide extensions for known file types'. Click 'OK' on the dialog. Then it should show the file name as 'New Text Document.txt'.
compilation new text document with out extension

compilation choose folder and search options

compilation folder options dialog

compilation choose hide extensions for known file types

compilation new text document with extension
  • 4. Rename the text document to program name: Right click on 'New Text Document.txt' and rename it to 'PrintHelloWorld.java'. You should see the .java extension after you rename it. The type should be 'JAVA file' instead 'Text Document'.
compilation after renaming the text document
  • 5. Why we called it 'PrintHelloWorld.java': Since in this example we will be copying the code from Java Sample Program - Simple Hello World Program In Java and the program name their is PrintHelloWorld. Please note that the file name PrintHelloWorld.java and the program name PrintHelloWorld should be exactly same and they are case sensitive. Having the file names like 'printhelloworld.java' or 'Printhelloworld.java' or 'printworld.java' is not allowed. The spelling should be exactly same and the case of letters (CAPITAL or small) should also be same.
compilation open file with notepad
compilation copy content into file
  • 7. Launch the command prompt: Go to the 'Start' menu and launch the 'Command Prompt' program. Or run the 'cmd' program to launch the command prompt.
compilation launching command prompt
or
compilation launch using cmd

compilation command prompt
  • 8. Go to the required directory: Use the command 'd:' to go the required drive, 'cd programs' to go to the programs directory, 'dir' to see the list of files. The directory listing should show the file 'PrintHelloWorld.java'
compilation change and list directory
  • 9. Compiling the Java program: On the command prompt use the command 'javac PrintHelloWorld.java' to compile the program. It should compile with out any errors. If it says 'not a recognized program', then it means the java is not installed or it is not proper. Go to Installation Of Java on your PC for installing the same. If there are no errors, then run the command 'dir' and see that a new file 'PrintHelloWorld.class' is created. This .class file is created when we compiled the program and it contains the bytecode. You should also see this file in the file explorer.
compilation compilation and list directory

compilation dot class file in explorer
  • 10. Run the Java program: On the command prompt use the command 'java PrintHelloWorld' to run the program. It should print the output 'Hello World' on the screen. This means we are able to successfully compile and run a simple java program.
compilation run java program

Making further changes to the PrintHelloWorld program:

To make further changes to the program:
  • Open 'PrintHelloWorld.java' in notepad, make the necessary changes, save it using 'File'->'Save'
  • Compile in command prompt using 'javac PrintHelloWorld.java'
  • Run using the command 'java PrintHelloWorld'

Creating a new program called SayHelloHi:

  • Create a new text document using 'New' -> 'Text Document' in file explorer, rename it to 'SayHelloHi.java'
  • Write code in the file 'SayHelloHi.java'. The class name given in the file should be 'SayHelloHi' which is same as the program name.
  • Save the file using 'File'->'Save'.
  • Compile in command prompt using 'javac SayHelloHi.java'
  • Run using 'java SayHelloHi'.

Setting up the environment for future programming sessions:

  • Open the directory D:\programs in windows(or file) explorer. This is where all your programs are present.
  • Open the command prompt using 'Start'->'Command Prompt' or by running 'cmd'. Go to your programs directory using the commands 'd:', 'cd programs'. Use 'dir' to see the list of all programs and the .class files created.
  • Run the command 'javac PrintHelloWorld.java' and 'java PrintHelloWorld' to confirm that your java installation is still proper.

Dependent Topics : Installation Of Java on your PC 

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.

Learn Java Programming from our Experts in 30 Days