JAVA is pretty famous software platform used to develop variety of applications; web-based, stand alone, mobile, etc.,. JAVA is used in wide variety of platforms from embedded devices to enterprise servers.
Here are the steps to write your first JAVA program.
Step (1). You need to download JDK and install it on your system. For eg: Download and install JDK under C:\JDK7 folder.
Step (2). Create a separate folder to store your JAVA program. For eg: create a folder workspace in C: drive.
Step (3). Before writing the JAVA program, keep in mind the following things.
- You should write a public class in your java file.
- The file name should be same as your public class name and the extension must be .java. For ex: your class name is FirstJava, your file name must be FirstJava.java. Keep that in mind it is case-sensitive, ie., your program will not compile if your class name is FirstJava and file name is firstjava.java. So you should create your file as FurstJava.java.
- You should add public static main function in your public class. This is the entry point to run your program.
Step (4). Now open any text editor and type the following JAVA code and save the file as FirstJava.java under c:\workspace folder.
public class FirstJava { public static void main(String[] args) { System.out.println("Hello! This is my First JAVA Program!"); } }
Step (5). Now it is the time to compile and run your program.
- javac.exe is used to compile the JAVA programs and generate the machine independent byte code ie., .class files.
- java.exe program to load the byte codes and run the program.
These two executable programs are located under bin folder of JDK installation. For eg: if you install JDK under c:\jdk7 folder, these two files are located under c:\jdk7\bin folder.
Step (6). Open command prompt window and go to c:\workspace folder, where you saved your FirstJava.java program.
Step (7). Type the following command at command prompt to compile your FirstJava.java program.
javac FirstJava.java
You will see the error “‘javac’ is not recognized as an internal or external command, operable program or batch file.”
This is because, your javac file is located under c:\jdk7\bin folder; but your are accessing this program from c:\workspace folder where javac.exe doesn’t exist.
To resolve this issue:
- either you have to use complete path of the javac.exe executable (eg: c:\jdk7\bin\javac.exe FirstJava.java) or
- you have to add the folder where javac.exe exist to PATH environment variable. The following is the command to set PATH environment variable.
set PATH=%PATH%;c:\jdk7\bin\
If you set the PATH environment variable to the folder where javac.exe exist, you can use javac at command prompt from any location.
Step (8). Once you compile your FirstJava.java program, it will generate FirstJava.class file. This file contains the byte code.
Step (9). Now you have to run your first JAVA program. Type the following command at command prompt:
java FirstJava
It will throw the error “Error: Could not find or load main class FirstJava”. Because java.exe will look for the .class files through CLASSPATH environment variable. Even though your .class file is located in the current directory from where you are running the program, it will not recognize your .class file until you mention the path through CLASSPATH environment variable or through command line arguments.
Two ways we can resolve this issue. One is by passing the .class file location through the command line arguments.
java -cp . FirstJava
Another way is by setting the CLASSPATH environment variable.
set CLASSPATH=%CLASSPATH%;. java FirstJava
Find that ‘.’ is added to the CLASSPATH, which means java.exe will look for .class files into current working directory. And you should pass only class name to java.exe, NOT the FirstJava.class file name.
After running the above command your FirstJava program will run and display the following result:
Hello! This is my First JAVA Program!
—