To get started first of all create new folder where you will save your exercise files and which is easily accessible. In my system I am going to create folder JavaTutes in E: drive. Keep in mind that there is no space in folder name. Path of my folder is E:/JavaTutes.
Open Notepad and save this as First.java (to save on top menu bar click on File -> Save as…, In Save as type choose all files and in name type “First.java”). And click on save button.
Now start typing in notepad (First.java file).
First of all, create class, as in previous tutorial I said that file should match with class name. And the class name will be First.
Inside class we need to define main method, because main method executive very first and without main method your program will not execute.
Inside main method you can do anything what you want.
So for now we will only print ”Welcome to learnpainless.com“.
To print anything on screen we use System.out(). Our message will be inside brackets of out(). As shown below.
Now save your file, and open Command Prompt. To open Command Prompt open start in Windows and search cmd.
Command Prompt will start with c:/windows/system32 etc. folder. So we need to first go inside our project files folder from command prompt.
Use cd command to change directory. And Drive letter to change drive. For example, my files are inside E drive so first I will change drive by typing E: and hit enter.
Now type cd JavaTutes and hit enter.
Now I am inside my project files folder. So I will compile my java class. Type this command o compile java file. javac Filename for example ”javac First.java” and hit enter
If there is no error in file, then command prompt will show your project directory without any message.
If you go inside your project folder from My Computer, then you will see 2 files there First.java and First.class file. And if you open that First.class file with notepad then you will see encrypted codes. Because its compiles file of our file. In Bytecode.
To execute this bytecode file in command prompt type java Filename for example java First and hit enter. And you will see your output in command prompt. As my output is ”Welcome to learnpainless.com“.
here is example code of First.java file
System.out.println("Welcome to learnpainless.com");
This is how we write java programs and how we execute java programs. And you also seen that what Bytecode file we are talking about in introduction posts. And if we run that First.class file on another OS them it will execute without any exception, that’s why java is platform independent and secure.
Subscribe to our newsletter!
We'll send you the best of our blog just once a month. We promise.