How to Compile and Run Java Program from Command Prompt

Hi this is Anil and welcome to the video tutorial
on Java programming. And in this tutorial we are gonna learn how
to run a Java Program from the command prompt. so in the previous tutorial I have explained
you know how to write first program in Java and also you know how to run that program
and in the previous tutorial we were using the eclipse IDE and because of that we were
running our program very easily. You know just by clicking the run button. So in this tutorial, i’m gonna show you
how to run your Java Program from the command prompt by typing the commands. so the first thing is you know you guys need
to download and install JDK in your system and also you guys need to set the path environment
variable to execute the Java program very easily. so if you guys don’t know about it then
please watch my video tutorial on downloading and installing the JDK in your system. so here you are gonna start writing the java
code. So first of all I’m gonna go and open up the
editor, you know any text editor. You know i’m gonna open up the notepad++
and you know we are gonna write our Java code. So the first thing that we are gonna do is
we are gonna create a class. So its gonna be public, the access specifier
and then the class keyword and then the class name. Let’s say Hello and then a pair of curly
braces and between these curly braces we are gonna define this class. So the next thing that we are gonna do is
we are gonna build our main method. You know the Main method is the starting point
of a Java Program. so its gonna be public static void main and
between these parenthesis we are gonna write string then Square brackets then Args and
then we are gonna have a pair of curly braces here and inside is main method we are gonna
write the statements that we want to execute and here we just gonna write system.out.println
and i just gonna say hey YouTube and here this line is gonna print hey YouTube to the
screen. so I just gonna save this program. So i’m gonna go to file and then i’m gonna
go to save and I just gonna save it in my desktop and I just create a new folder. And let’s give a name Java and inside that
java folder i’m gonna save this and its gonna be hello dot Java. So hello dot Java.
make sure that you have this dot java Extension and also the filename same as the class name
that you have given and then click on save and you guys can see hello dot Java All right. now i’m gonna close this and in my Desktop
you guys can see a folder called Java and inside that we have hello Java file. All right. I’m gonna close it and i just gonna open
up run command and I’m open up the command prompt. Click ok and here we are in my user directory
and since our Java program that we want to execute is in our desktop, we are gonna move
to the desktop. so I just gonna use the dir command here and
you guys can see Desktop. So i’m gonna use CD then Desktop. Now we are in the desktop and then I’m gonna
use the dir again and you guys can see we have a java folder. So its gonna be CD and then Java and this
CD stands for change directory and now we are in the JAVA directory. So if i use the dir here, you guys can see,
we have hello dot Java. So just gonna open it. All right you guys can see the
here. So now the first thing that you guys need
to do when running a java program is you guys need to compile your Java code and you guys
need to convert your Java source code to something called bytecode. So to compile the Java code, we are gonna
use the Java compiler and to call that Java compiler, we need to use the command Javac
and then we just need to give the name of the file. So its gonna be Hello.hava. So i’m gonna write
And then i’m gonna hit enter. Ok. its gonna return the prompt again and
if you guys get any error here, you know which means that you might have not set the path
environment variable or you know you guys may have made any mistakes while writing the
code. so make sure that you haven’t done any error. All right. Now i’m gonna open up the folder you know
which is the folder where we have saved our And inside that you guys can see we have another
file created, which is called hello.class This Hello.class is a file which is gonna
contain the bytecode, which is created from this Hello.Java So the next thing that we are gonna do is
we are gonna execute this Hello.class file you know which contains the bytecode. So to execute the java program, you know we
need to call the java interpreter. So its gonna be java and then just need to
write Hello. So here this Hello in this second line is
from this Hello.class file. so whatever the name of the class file that
you get, you guys need to pass it here. So its gonna be only hello. you should not use dot class So i just gonna hit enter, and you guys can
see we get “ hey YouTube “ That’s the output of our program. So one last time, to run the Java program,
we need to convert our source code to something called bytecode and for that we are gonna
use the javac command you know which calls the Java compiler and we need to pass the
filename with the .java Extension and here we have passed hello.Java and to run the Java
code we need to call the Java interpreter and to call the Java interpreter, you are
gonna use the Java command and you need to pass the filename you know which contains
the bytecode. so which is hello in this case. so this is it guys. this is how you guys can run a Java Program
from the command prompt. Thank you for watching and don’t forget to
subscribe to my channel. if you guys have any doubt any suggestion
please tell us and i’ll see you in the next tutorial.

100 thoughts on “How to Compile and Run Java Program from Command Prompt

  1. Hi sir,, the problem is that at the last step when I typed java Hello
    it didn't work,, it says "the system cannot find the file C:programDataOraclejavaJavapathjava.exe
    kindly guide me as soon as possible..
    and thanks for the tutorial

  2. Hello Mr. Anil, im very grateful for your explicit videos but i would love if you could help me on this. im a highschool student so i dont know very much about this, i have followed your instructions and i tried a simple programme with and the javac command works, but then the java command comes with an error: could not find or load main class main

  3. i can understand u earn by the advertise but if possible try to keep it 5 sec and then skip option it would be great help when we are learning in flow.

  4. Hello, I'm working on java docs and I've included link to this video, hope this is ok for you
    you can see the content here,

  5. error : package system doesn't exist
    system.out.println("Hello Youtube");

    Mr.Anil can you please debug this error… Thank You so much in advance…

  6. Just found your tutorials today, and so far I'm liking them. I am impressed you are still active in the comments even though the video is 3 years old. Every other tutorial I have found the creator is no longer active.

  7. I'm getting an error -could not open 'c:program filesjavajre1.8.0_131libamd64jvm.cfg'
    wat should i do?

  8. If we have 2 classes, class A and class B(having the main method)…what should b the file name to compile and run it in cmd??

  9. Thank you sir for uploading video…..when I compile and run in cmd…. everytime he will say set path..
    one time set path then again an again ..why it is asking
    javac: file not found
    usage:javac options sources files
    use help for a list of possible options …..
    please reply

  10. Hi anil. Your videos are very much helpful. I am having a doubt can you plz clear that. By following your videos and practising the topics what you discussed in your videos can I crack a job as a Java developer. I am asking you this bcoz their is a gap of 2 years since I completed my studies. Right now I am jobless but interested to be as a software developer. Please reply

  11. thanks alot sir… since 1 week i am unable to run a program i tried so many ways but when i watched ur video its amazing ……
    thanks alot sir…..

  12. awesome through your video I learned to run the programme thank you so much βž•βž•πŸ…°πŸ…°πŸ…°βž•βž•

  13. BRO I REALLY NEED YOUR HELP!!!!!!!!!!!!! Actually how to edit and make our java program coding in command prompt USING " edit " ?????????? example:edit

  14. Can you help ? Where i come at min 5:37 to me is says this " Error: could not open `C:Program FilesJavajre1.8.0_191libamd64jvm.cfg' " my output not working cant find the problem

  15. Generally when our people demonstrate something, they repeatedly talking, touching the point already they said, which is bad. I have watched many videos, not find short and sweet and to the point.

  16. Everytime when we will save the program we use same these long process…there is not any shortcut because plhe mene java padi thi to sedhe run and compile krte the PR ab kr rhi hu program to run ho ja RHA PR compile ni ho RHA…plz tell me

  17. For me Its Showing Like Javac is not recognized as an internal or external command,operable program or batch file What to do now

  18. Hye, Ive tried the "javac …." then error occurs, and the error said the "javac" is not recognized as a command, i have triple checked the file name and class name and there was no problem… how do i fix it?

  19. When I wrote javac , it is showing "javac is not recognised as internal or external command, Oracle program or batch file" WHY????

  20. thanks man.. tutorial for running a program which has user input?
    my program is unable to take input
    import java.util.*;
    class first1
    public static void main(String[] args)throws InputMismatchException
    Scanner x=new Scanner(;
    System.out.println("Enter I");
    byte i=x.nextByte();
    System.out.println("Hello World");

  21. Thank you so much for a clear and simple tutorial! I was having a hard time figuring out how to compile and run a java program and you simplified it for me!

  22. 5:31 I got this error:
    Error: A JNI error has occurred, please check your installation and try again

    Exception in thread "main" java.lang.UnsupportedClassVersionError: Test has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 52.0

    at java.lang.ClassLoader.defineClass1(Native Method)

    at java.lang.ClassLoader.defineClass(Unknown Source)

    at Source)

    at Source)

    at$100(Unknown Source)

    at$ Source)

    at$ Source)

    at Method)

    at Source)

    at java.lang.ClassLoader.loadClass(Unknown Source)

    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

    at java.lang.ClassLoader.loadClass(Unknown Source)

    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Leave a Reply

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