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 hello.java 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 Hello.java 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
Hello.java
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.