How to Compile and Run Java Program in Command Prompt ( With Packages ) on Windows 10

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi this is a little from learning lad and welcome to another tutorial on Java programming language in this video we will see how we can compile and run a Java program from command prompt in order to continue with this video you need to have Java installed in your computer and you need to set the path environment variable if you don't know about this then I have already made a video and I'm going to put the link of that video in the description box you guys can check that out so here the first thing that I'm gonna do is in my desktop I'm gonna create a folder and I'm gonna name it as example inside this folder we will store the Java source file that we're gonna be creating in a moment ok after that I'm gonna open up a text editor I have Visual Studio code installed in my computer so I'm going to open it up you can use any text editor here notepad or notepad plus plus any text editor you guys can use and once the text editor is open I'm going to go to file and I'm going to create a new file and here the first thing that we do is we will save this file so I'll go to file and I'm going to click on this save and I will save it in my desktop inside the folder that I have created just a moment before and I'm gonna name this file as hello and the extension will be dot Java okay after that I'm going to click on the Save button and as we can see hello dot Java is saved okay now here in this Java source file let's create a simple Java program to display some text to the screen so I'm gonna create a class and I'm gonna call it as hello here the name is same as the file name hello and after that we're gonna have the public static void main method in this class and this will be the entry point of our program and this method will take some arguments of type string and here we will have the system dot out dot print line method and we can a displays text to the screen let's say hello YouTube okay and after that I'm gonna save this okay now we have a very simple program written in this hello dot Java and this file is saved in the desktop inside a folder called as example now I'm going to close this text editor I'm going to come back to my desktop and I'm going to click on this example folder I'm going to open it up and here you guys can see we have hello dot Java the file that we have created in just a moment before so what we want to do is we want to run this Java file that we have created so in order to run a Java program first we have to compile the Java source file and we have to create the class files containing the bytecode of the source code and then we have to call the Java interpreter and we have to pass that class files containing the bytecode which the interpreter can run so here to compile and run the Java program we will be using the command prompt so what I want to do is I want to open up the command prompt with the path of the current folder where I have saved this hello dot Java so to do that what I'm going to do is I'm going to come to this URL bar I'm gonna click on it once and it will highlight the path and I just want to type in CMD and after that I'm gonna hit the enter button and it will open up the command prompt in this folder or the command prompt with the path of this folder now you can run the Java program from any other folder but you have to provide the exact path and just to eliminate that typing here I have opened up the command prompt with this path ok so we are in the desktop and inside the example folder so the first thing that we have to do is we have to compile this Java source file and we have to generate the class file so to do that we will call the Java compiler and that is Java C this Java compiler is installed when you install the Java in your computer - this java compiler we need to provide the source file that we want to compile so here we want to compile hello dot java containing the java source code so we will write java c space hello dot java and after that I'm gonna hit the enter button in here and it will bring the prompt back but if we look at the folder in here we have another file created with the name hello but the extension is dot class for every class present in your source file you will get a class file created for that if you remember then in our hello dot java we have the class hello we have one the one class and that's why for that class hello dot class file has been generated now we have the bytecode in the class file the next thing that we do is we have to call the Java interpreter and we have to pass this class file so that the Java interpreter can run the Java program so to call the Java interpreter we have to type in Java and then we have to provide the name of the class containing the static void main method which is the entry point of our program if we look at this source code that we have created then the name of the class containing this static void main method is hello so here we have to type in Java that is for calling the Java interpreter and we have to type in hello okay don't get confused with the hello dot class here you have to provide the name of the class containing the static void main method for this Java interpreter because the execution will start from the static void main method after that hit the enter button and you guys can see we get hello YouTube as the output now this is for a simple program without containing any package and all this so what if our program is going to contain the packages so what we're going to do is we can add the package information to our program and let's see what happens so what I'm going to do is I'm gonna delete this hello dot class file generated we have hello dot Java right now I'm going to open it up using the visual studio code you can open it in any text editor and here to this source code let's add some package information for example let's say this class is present in the package called as one ok now we have the package here now how we can run this program so I'm going to close it here if your source file is containing the packages then you have to follow just a bit different approach so here the first thing that we do is we will open up the command prompt with the path of this folder so I'm going to go to this URL bar I'm going to type in CMD and I'm going to hit the enter button and you guys can see we have the command prompt opened with this path now the next thing that we do is we will compile this Java source code since our source code is containing the package information what we want to do is we want to compile it and we want to store the generated class files inside the folders with the package names if you remember then the packet name that we have given in our source code is one so what we want to do is we want to store the generated class files after calling the Java compiler inside the folders with the package names where they are present so here to specify that we will use a flag minus D and then we need to specify where we want that folders and that class files to be present we want those files to be created here in this example folder so we will say dot this dot here represents the current folder ok and then we need to provide the name of the source file so which is hello dot Java if your source code is containing the package information then while compiling the Java code you have to call in Java C Java compiler minus D here we'll specify that we want the class files to be created inside the folders with the package names and those files and folders to be created in the following directory and that following directory here in this case is the present directory from where we are running this program and we are specifying that by using the dot here and then we will specify the name of the file containing the Java code okay now if I hit the enter button here here as usual nothing happens but if you look at the folder then we have a folder created one and if I open up that then you guys can see hello dot class file now I'm going to come back to my example further here now in order to run this program we have to call this our interpreter and the class file containing the static void main method is present in this one folder or inside the package called as one so we will specify one dot and then the name of the class which is hello okay if I hit the enter button here you guys can see we get hello YouTube as the output now what if our program contains sub-packages just to demonstrate that i'm going to delete this folder one and then i'm going to open up this source file again and here let's add a sub package and let's say to 1.2 okay I'm gonna save this file and I'm going to close this now you I'm in the example folder where I have the source file I'm going to open up the command prompt in here since our program is containing the packages what we want to do is while compiling the Java program we will say that the class files generated should be stored in the folders so we will use the minus key flag and then we need to specify where the folders and that files to be created I want them to be created in the present directory which is this example folder and that's why I'm gonna use the dot here and then we will specify the name of the source file which is hello dot Java if I hit the enter button nothing happens here the prompt will come back but in this example folder if you see then we get a folder called as one if I open it up then this folder will contain another for chorus - if I open up that one then you guys can see we have hello dot class here now I'm going to come back in here so so what it has done is it has created separate folder for this sub package okay now in order to run this Java program we will call the Java interpreter and then where exactly is the class file containing the static void main method is present and with reference to this example folder where we are at the moment the file is present inside the folder one and then inside another folder called as two and then name of the class is hello the class containing the static void main method which is hello is present in the package - which is present inside the package one okay after that if I hit the enter button in here we get hello YouTube as the output so this is it guys this is it for this video this is how you guys can compile and run a Java program from command prompt if you like this video then hit the like button if you disliked it then hit that like button if you want to say something if you want to share some opinion then write that in the comment box for more tutorials like this do subscribe to the channel thank you for watching I'll see you later in the next video
Info
Channel: LearningLad
Views: 73,194
Rating: undefined out of 5
Keywords: java, Run Java Program from Command Prompt, javac, java compiler, java interpreter, java package, cmd, command prompt, How to Compile and Run Java Program from Command Prompt, java programming, java video tutorial, java videos
Id: QWUFPehCj1I
Channel Id: undefined
Length: 12min 25sec (745 seconds)
Published: Thu Aug 22 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.