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.

Ok so let’s start.

  1. 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.
  2. Now start typing in notepad (First.java file).
  3. 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.
  4. Inside class we need to define main method, because main method executive very first and without main method your program will not execute.
  5. Inside main method you can do anything what you want.
  6. So for now we will only print “Welcome to learnpainless.com”.
  7. To print anything on screen we use System.out(). Our message will be inside brackets of out(). As shown below.
  8. Now save your file, and open Command Prompt. To open Command Prompt open start in Windows and search cmd.
  9. Command Prompt will start with c:/windows/system32 etc. folder. So we need to first go inside our project files folder from command prompt.
  10. 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.
  11. Now type cd JavaTutes and hit enter.
  12. 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

java-first-program-1

  1. If there is no error in file, then command prompt will show your project directory without any message.
  2. 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.
  3. 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”.

java-first-program-2

here is example code of First.java file  

class First{
	public static void main(String[] args){
		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.