How To Run a .Java file in cmd (command prompt)
If you are able to make your .java file on your own. you have to compile it and then you will be able to run it.
I know there is a huge number of people facing problems after installing eclipse.
I will firstly describe how to run a java file using command prompt and then will describe in details how to remove the errors in eclipse like error code 2 or error code 1 while starting the eclipse.
run a java file using cmd
step 1: Install the jdk (java development kit) for your pc. but keep in mind that you must have to install the proper jdk. that means if you are using 64 bit pc you have to download the jdk for 64 bit. and for the case of 32 you need 32 bit jdk
Step 2: after installing it you need to open cmd to check whether everything is fine or not.
press windows + r …. type cmd and press ENTER. in cmd you will be able to check your java version …
just type ” java -version” press a space after java . otherwise it will be an invalid command.
it will show your java version . if it shows error. just download java from oracle site.
step 3: now you have to add an environment variable to add java commands in cmd. otherwise the commands to compile and run a java file will be invalid command
how to add environment variables to run java file
open your my computer or this pc then find the path location where you have installed your jdk (java development kit) the default location will be like this
you need to copy your path location
here is a screenshot so that you may understand
Step 4: you are almost done just you need to add this path to environment variables.
click windows and search for environment variable and go to edit environment variables for your system.
now in system variables you will find a variable name path . just edit the variable and paste the previously copied path link at the starting . and then type ; to separate the variables which was present there from the beginning . then save it.
Step 5: now start your cmd. if it was previously opened . just close the window and open again otherwise changes will not be reflected.
now go to the location through cmd where your java file is located you are supposed to run.
cd/ is the command to go back to the root directory and cd space folder name is the command to go into the folder
and if you are willing to go to other drive .. just go back to the root directory and type d: or e:
when you reach at your destinated directory, you may compile the java file … the command is javac space file name… example:
and after finishing compile process you may run it by using this command java space classname.
eg—- java helloworld (in case of class name – helloworld)
some more tips : which editor you should use to create java file, I use sublime as it looks good and many good features it has. but choice is always yours. Editor will not affect your programing . you may use notepad ++ too or other editors
and if you are willing to reduce your headache of errors while making a program you may use eclipse. if you face any kind of errors after installing it and unable to open it don’t get nervous. just get the exact version of eclipse based on your system. if you are using 32 bit pc you need to install the 32 bit eclipse. after installing it install java development kit (jdk) and JRE . then run the eclipse.
if still problem persist just leave a comment in the below comment section I will solve it.
happy coding 🙂