This document provides instructions on how to compile and run a Java file from the command line. It explains how to check that the Java compiler (javac) is in the system path, and provides the commands to compile a Java file, run it, take input from a file, output to a file, and make a batch file to automate the process. It also addresses some frequently asked questions.
1. ACMACMACMACMACMACMACMA
CMACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
ACM Training Session
MACMACMACMACMACMACMAC BRAC UNIVERSITY
MACMACMACMACMACMACMAC
6/15/2012
Mahbub
MACMACMACMACMACMACMAC
This work is licensed under a Creative Commons Attribution-
MACMACMACMACMACMACMAC
NonCommercial-ShareAlike 3.0 Unported License.
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
2. Page 1 of 2
Compile and Run Java file from command line
1. First check if the path of javac is added in the Path (System Variables) or not.
To check that
Go to start -> run -> write cmd
Now type javac if you see the following message, then javac is not added in
the Path
'javac' is not recognized as an internal or external command,
operable program or batch file.
For add the path of javac.
Go to C: -> Program Files -> Java -> jdk -> bin
Copy the path
Now Write click on My Computer -> properties -> Advanced -> Environment
Variables
From System variables find path and double click
A window will pop up
Now in the Variable value filed paste the path at the end, which you copied
previously.
The paths are in the following forms of pattern
xxxx;zzzz;zzzz;path_of_javac
So make sure you have put a semicolon (;) to separate two paths
2. Command for Compile java file
javac yourClassName.java
3. Command for Run java file
java yourClassName
S. Mahbub – Uz – Zaman (09301004)
3. Page 2 of 2
4. Command for taking input from a file
java yourClassName <inputFileName.txt
5. Command for giving output to a file
java yourClassName >outputFileName.txt
6. All codes
javac yourClassName.java
java yourClassName <inputFileName.txt>outputFileName.txt
Frequently Asked Question
1. How to make batch file
Create a text file and rename it to textFileName.bat
Make sure your Hide Extension option is not checked
in, because you might end up with following
textFileName.bat.txt
So your .bat file will not work
2. How can I edit batch file
Write click the batch file and click edit
3. How can I hold the black screen of cmd for debug
javac yourClassName.java
java yourClassName <inputFileName.txt>outputFileName.txt
pause
4. How can I automatically open the output file
javac yourClassName.java
java yourClassName <inputFileName.txt>outputFileName.txt
start notepad outputFileName.txt
S. Mahbub – Uz – Zaman (09301004)