How to Use the Command Prompt to Work With Java

Posted by

This tutorial will show you how to run Java on your computer using Command Line

1. To use command prompt for java, you will have to install the JDK.

2. Download the JDK SE (Standard edition) from that website and install it.

3. Once you've installed it, open up its installation directory (without you change it, the default is 'C: Program Files Java jdkx.x_xx', where xx.xx is the version number.)
– In the jdk folder, open the folder 'bin', and copy the complete path from the address bar, or from the properties of any of the files in the folder bin.
– If you can not find the installation directory or the bin folder, that means you did not install it right. Reinstall it and retry step 3.

4. Open command prompt.
– To open command prompt, click on Start, then click on Run, type 'cmd' in it and press enter.

5. After you open the command prompt, write 'path =' and paste the path you have copied earlier from the bin folder (Note: ctrl + v does not work in command prompt for pasting, use right click).
– When you do this step right, you will get no reply but your directory printed again (something like 'C: Users YourUserName').
– But if you do it wrong, you will get an error. Retry from step 3.
– Now your command prompt is ready to compile a Java program.

6. Write your program.
– To write your program, you will need some text editor like Notepad or Notepad ++.
– Write your program in it and save it as YourClassName.java (eg. MyClass.java).
– You can write any program and if you do not have one, you can use our Java: Simple program to display a message.

7. Now Change the working directory to where you have saved your program.
– Changing the working directory:
* To do that, open the folder where you have saved your program and copy the complete path from the address bar or from the properties of your program.
* Then in command prompt, type 'cd' Paste your path here.
– When you do this step right, your working directory should now change to the folder where your program is saved.

8. Compile your program.
– To Compile, write 'javac' filename.java
* Again, when you do it right, you will get no confirmation but your working directory printed again and have a class file named same as your program file, ie, filename.class in your working directory.
* If you do it wrong, you will get an error.

9. Run your program.
– To run your program, run the class file just made from the command prompt.
– To do that write 'java' filename.class.

10. Thats it! Your program should run now.

Leave a Reply

Your email address will not be published. Required fields are marked *