You now have everything that you need to run your first program on the command line. But before you do that, you will learn what the command line is and how to use it. The next video will help you with this. Specifically, you will learn the following in this video:
What is the command line?
How do you open it?
Why is the command line required to run a code written on a text editor?
Some basic commands used on the command line to navigate between directories and to open a file inside a directory.
Please note that you will be writing your codes on Notepad, which is the text editor on Windows. For Mac, Linux, etc. you can use the respective text editors. Please keep in mind that you cannot use MS Word as a text editor for writing codes.
Well, Java programs can be written on any text editor such as Notepad. But do you really think that Notepad itself will understand Java commands and execute them? The answer is no. To run and execute Java programs written on a text editor, you need an application called the Command line. But why do you need this command line? Well, you need it because Notepad doesn't know where the Java compiler is on your computer. The Java compiler is not an icon that you click and it compiles a code to machine language. To be able to access the Java compiler, you need the Command line. It gives a command to your computer and your Notepad file can then access the compiler. The compiler then compiles it and you're sorted. However, before you start writing the code, you need to know what the command line is and where is it on your PC. You also need to know how to open it and how you can work with it. Let's learn all this. Remember, you just install JDK, which contains a few tools that are mandatory for a Java code to run the compiler and the Java runtime environment. Now, one of the ways in which the code written in text editor can access these tools is through the command line. The Command line is an application through which you can give commands or instructions to your computer for windows. To open the command line, you just need to search Command Prompts. In the Start menu you will see the Command prompt application here. When you click this, you will see a window with black background like this. This window can also tell you which version of JDK you installed. For this, type Java space and hit enter. You will see something like this. This shows that I have version eight on my PC. I have GRE and VM stands for virtual machine. In case you don't see anything like this, it means there is something wrong with your installation of JDK. Now you want to compile and run the code. In order to compile and run the code, you need to first go to the folder where the code is. So you need to tell your computer to go to the folder where the code resides. Once you're in that folder, you can tell the computer to run and compile the code. Let's see how you can do that. Let's say you're currently at C Users Videos and you want to navigate to Desktop. What you have to do is type CD Space Desktop and this will bring you to Desktop.
Now you can do anything you want. When you are in Desktop, you can create directories, list the contents and much more. Let's say you want to move two steps ahead. Let's say you want to go to Videos and inside Videos, you want to go to movies. You can do this in one single step. You type CD Space videos, backward slash movies. This brings you to movies inside Videos if you want to open a file in a directory, first navigate to that directory. For instance, if you have a JPEG file sun. JPEG inside User Images to open this file, you need to navigate to User Images folder and then type the file name like this it will open the file in the default program. Now to open a file, you need to navigate to the directory that contains it. For example, what if you are in user program files and you want to open a file sun. JPEG that is there on your desktop while you write sun. JPEG while in user program files, the computer will look for sun. JPEG in program files which is the current directory you are in. If it doesn't find it there, it won't open it. To open it without navigating to the desktop. What you can do is you can add desktop to your path. To know what a path is, type Path and this will appear on your screen. So this is a list that tells your computer to open a file that is not present on your current directory. It needs to look for it in these directories. So if you add desktop there after looking for sundown. JPEG in program files, it will look for it in these directories and open it. But how do you add desktop to your path? Go to Start, right click on my computer, go to Properties, go to Advanced System Settings, click on Environment Variables and there you will see the variable called Path. You can edit it like this, this and add your path to it. Now you can add desktop here like this. Now when you go back to the command line, you can open sun. JPEG without navigating to the desktop itself.
Java programs can be written on any text editor like Notepad, but it doesn't understand Java commands and cannot execute them.
To run and execute Java programs, you need an application called the Command line because Notepad doesn't know where the Java compiler is on your computer.
The Java compiler is not an icon that you click and it compiles code to machine language; you need the Command line to access it.
Before writing code, you need to know what the Command line is and where to find it on your PC, as well as how to open it and work with it.
To access the Java compiler and Java runtime environment, you need to install JDK, which contains mandatory tools for Java code to run.
The Command line is an application through which you can give commands or instructions to your computer for windows.
To open the command line, search Command Prompts in the Start menu, and you will see a window with a black background that can tell you which version of JDK you installed.
To compile and run code, you need to navigate to the folder where the code is located and tell the computer to compile and run the code.
To navigate to a folder, type CD Space, followed by the folder name in the Command line, and to move two steps ahead, type CD Space, followed by the folder name with a backward slash.
To open a file, navigate to the directory that contains it and type the file name, and to open a file that's not present in the current directory, you can add the directory to your path variable in the Environment Variables settings.
So now you know how to use the command line. Here, a very important takeaway was the path variable. If you add a directory to the path variable, you don’t need to navigate to that directory to open any file inside it. Also, you learnt about the ‘cd’ command, which allows you to navigate between directories. Remember, all the commands we discussed in the video were for Windows.
For commands on Mac, you can refer to this link.
BE VERY CAREFUL WITH THIS. ONCE YOU ADD A DIRECTORY TO THE PATH VARIABLE, YOU NEED TO CLOSE AND RESTART COMMAND PROMPT TO SEE IT IN ACTION.
Let’s now move to writing a code on a text editor. You will need to use all your knowledge of the command line to execute this code.
You just ran your first code on the command line. One very important step here was adding the JDK to the path variable. This helps in using the Java compiler, which is contained by the JDK, without moving to the directory that contains the JDK.
All this needs to be done for Windows only and not for Mac.
These are the steps you need to follow to add the JDK to your path:
Go to Advanced Settings in System Properties
2) Click on Environment Variables
3) Click on Edit after selecting the Path, or double click on Path.
4) In the dialog box that appears, click on New.
5) Finally, browse to C/Programs(x86)/Java/JDK/bin.
6) Click OK on the current window and the previous window as well.
7) Restart your command line, and you are done!
Now that you know the advantages of using an Integrated Development Environment (IDE), let’s download it. In the next segment, you will download an IDE called IntelliJ. You will also learn how to use it and how to write a code on it.