How to create the first Java program


Basic requirements for writing a simple Java program
  1. Java Development Kit (JDK): You need to have a JDK installed on your system to run a Java program. JDK is a collection of programming tools and software needed to develop and run Java applications. You can download and install the JDK from the official website of Oracle.                                                                                                                                                                                          click here to learn how to install the JDK toolkit                                                                                                                                        

  2. Text Editor (windows Notepad) or IDE: You need a text editor or an Integrated Development Environment (IDE) to write your Java code. There are many free and paid options available, such as Eclipse, IntelliJ IDEA, NetBeans and Notepad++        


  3. Setting up environment variables: You need to set up the environment variables for Java on your system. This step is necessary to ensure that your computer can find the Java compiler and runtime environment. The environment variable "PATH" needs to be set to include the path to the bin directory of the JDK installation.      

  4. Compiling the Java program: Once you have written your Java code, you need to compile it using the Java compiler. The Java compiler will generate bytecode from your Java source code. The bytecode can then be executed on any system that has the Java Virtual Machine (JVM) installed.                                                                                             

  5. Running the Java program: After compiling your Java code, you can run it using the Java Virtual Machine (JVM). You can use the command "java" followed by the name of your Java class file to run your program.


Let us start to write our first Java Program

step 1: open the command prompt (windows) / terminal (Linux, Ubuntu)  

step 2: first to need to create a working directory in the hard disc.      
  1. type  D: and press enter                                                                                                                                                                                                                                                                        

                                                                                                                             
  2. type md javac and press enter. javac is here working directory's name                                                                                                                                                                                                 
                                                                                                                                                                 

step 3: should open a text editor for writing source codes of the program. if the operating system is Windows type notepad Example.java on the cmd and press enter on the keyboard                  
         
                                                                                                          

An empty Notepad file then will open. Save it

 open your working directory and you can see the text file that you named Example.java


step 4: let's try to print that "Hello Java" on the console. follow the below steps.

  1. //type on the source file "Example.java" below codes.                  
  2. class Example{
  3. public static void main(String[] args){
  4. System.out.println("Hello Java");
  5. }
  6. }
  7. //go to file and click save and close the file 
step 4: let's type the following commands on the command line.
  1. Example.java file to be compiled before the execution. compiling code is javac Example.java. type the code on cmd and press enter then you could see in your working directory file as an Example.class                                                                         
                    
  2.  our program is ready. let's try to execute it. type on the cmd as java Example. In the end, you could see that "Hello Java" on the console.                                                                                                                                                                                                                                                                                                  

Post a Comment

0 Comments