We know that there are many IDE available for Java programming. They have facility to write the programs as well as to compile them. Such IDE' are Eclipse, Netbeans and so on.
But here I will show how you can write and compile a Java program on Windows 10 system, without using any IDE.
Here we are going to write a Java program using notepad, the famous text editor of Windows for many years, and compile the Java program on Windows 10 command prompt.
Let's get started
Writing Java program using Notepad
1. Locating notepad on Windows 10
Start notepad by searching notepad in applications list.
2. Writing Java program in notepad
Start writing your Java program in notepad once you opened it.
3. Saving Java program as .java file
After writing out your Java program in notepad, you need to save this Java program file with .java extension. If you just saved the file as regular notepad file, then it will created as .txt file.
For doing this go to file > saves as, as shown in below screenshot.
4. Must match Java program filename with java classs used
In Java programming, the Java program filename must match with the class name used in Java program. e.g. if your class name is Main, then your Java program filename must be Main.java
In notepad, first change the Save as type field to All Files, and then give a filename to Java program with matching Java class name that is used in the Java program.
After writing and saving your Java program, exit out of notepad.
Compiling Java program
In the above section, we have just written our Java program in notepad text editor and saved the file with .java extension.
Now it's time to compile the Java program. In the following steps, we are doing this on Windows 10 command prompt (also called Windows command line).
1. Starting Windows 10 command prompt
If you don't know what is command prompt or how to start it, then just type CMD on search bar and open Windows command prompt.
2. Compile and run the Java program on Windows 10 commad prompt
To compile our Java program, we have to give command -
javac 'java program filename', e.g. javac Main.java.
To run the Java program, we have to give command -
java 'java program filename', e.g. java Main.java.
This will be clear by the below screenshot.
All right, I know some of you may not know, how to use commands to navigate between your directories and showing directory contents etc.
So here, I am not going to teach you all of the Windows commands, but I demonstrate some required commands only.
Suppose you have saved your Java program file in Documents folder, then you need to follow below steps in order to compile and run the Java program, including navigating to your program file location etc.
Little explation -
- Suppose your user name is admin, then command promt open's at C:\Users\admin,
give command cd Documents to go inside Documents folder. - To see list of directory contents, give command dir, you can see your Java program file here.
- Now give command to compile Java program, java HelloWorld.java
- After that, give command dir again, you can see a new file here, HelloWorld.class, it's java byte code file (executable) file.
It is created only if your program is error free and compiled successfully - Now run Java program and check program output. For that, give command, java HelloWorld
- Give exit command to exit out of command prompt.
Note - Your Java installation (jdk) must be included in your system path (system environments variables) to compile and run your Java programs.
Conclusion
In this way, we have learnt how to write and compile a Java program on Windows 10 without using any IDE.
This method is very simple and fast, as notepad is a very light weight and simple text editor as compared to Java IDE's.
And most important is that while you start learning Java as beginner, most modern Java IDE's gives you most of the ready made program stucture. And you might not grasp all the details of Java programming stucture clearly.
It's more clear if you type every line of the program yourself.
When you are well experienced with the Java programming stucture, syntax, then you can use any Java IDE of your choice. I recommand to use this method in your starting days of learning Java programming.
Let me know if you liked this method? Happy coding 😊.
Comments
Post a Comment