How To Set Path in Java
How To Set Path in Java
How To Set Path in Java
Java is one of the most prominent programming languages that has conquered the IT world.
To keep up with the growing industry, it is important to learn Java and the first step for
obvious reasons is installing Java. After the installation, if you want to work with java
without any hiccups one would need to set the java path.
Tools like javac and java can be used by setting the path. When you save your file in the java
directory i.e where java is installed, it is not necessary to set a path. But if you are saving
your file outside the directory, it is necessary to set the path beforehand.
1. Temporary path
2. Permanent path
• Click ok.
The very first program that any Java programmer learns to code is Hello World Program in
Java. But many a time we miss out on the nitty-gritty of the basic syntax.
SYNTAX ANALYSIS
Line 1: public class HelloWorldDemo {
This line makes use of the keyword class for declaring a new class called HelloWorldDemo.
Since Java is an Object-Oriented Programming (OOP) Language, the entire class definition,
including all of its members must be contained in between the opening curly brace { and the
closing curly brace}. Also, it is using the public keyword to specify the accessibility of the
class from outside the package.
This line declares a method called main(String[]). It is called the main method and acts as the
entry point for the Java compiler to begin the execution of the program. In other words,
whenever any program is executed in Java, the main method is the first function to be
invoked. Other functions in the application are then invoked from the main method. In a
standard Java application, one main method is mandatory to trigger the execution.
Let us now break down this entire line and analyze each word:
public: it is an access modifier specifies the visibility. It allows JVM to execute the method
from anywhere.
static: It is a keyword which helps in making any class member static. The main method is
made static as there is no need for creating an object to invoke the static methods in Java.
Thus, JVM can invoke it without having to create an object which helps in saving the
memory.
void: It represents the return type of the method. Since the Java main method does not return
any value its return type is declared as void.
main(): It is the name of the method that has been configured in the JVM.
String[]: It represents that the Java main method can accept a single line argument of the
type String array. This is also known as java command line arguments. Below I have listed
down a number of valid java main method signatures:
System: It is a pre-defined class in java.lang package which holds various useful methods and
variables.
println: It is a method of PrintStream class and is used for printing the argument that has
been passed to the standard console and a newline. You can also use print() method instead of
println().
Line 4: System.exit( 0 );
The java.lang.System.exit() method is used to exit the current program by terminating the
currently executing Java Virtual Machine. This method takes a status code as input which is
generally a non-zero value. It indicates in case any abnormal termination occurs.
So that was all about the program syntax. Let us now see how to compile Hello World in Java
program.
Next step is to go to your console window and navigate to the directory where you have
saved your program.
1 javac HelloWorldDemo.java
Note: Java is case-sensitive, thus make sure that you type in the file name in the correct
format.
Now that you have successfully compiled the program, let us try to execute our Hello World
Program in Java and get the output.
1 java HelloWorldDemo
Voila! You have successfully executed your first program in Java.
In case you are using an IDE, you can skip all this hassle and just press the execute button in
your IDE to compile and execute your Hello World in Program Java.
HOW TO COMPILE AND RUN YOUR FIRST JAVA PROGRAM
High-level languages like Java, C, C++, etc. compile a program to its equivalent low-level
code which can be understood and executed by the machine.
The first step is to create a folder, create a Java Class and write a Java Program. When we
write a Java program, javac (Java Compiler) translates the java source code to the bytecode
i.e. .class file. Bytecode is machine language of the Java Virtual Machine (JVM). Bytecode is
also referred to as the magic code of Java which is the platform-independent.
An important step after installing Java into the system is to set a path.
1 javac HelloWorld.java
This runs javac.exe, the compiler. The generalized command to compile any Java program.
javac <Java_file_name>.java
Once you hit enter, the HelloWorld .class file will be generated. You will
find both HelloTesters.java and HelloTesters.class among the files in your working
directory.
When we compile java program using javac tool, generally java compiler performs below
steps:
• Syntax checking
• Adding extra code
• Converting source code to byte code i.e. from .java file to .class file
So, when I say compiler adds extra code at the time of compilation, for instance, if you have
not written any constructor into your program then the compiler will add one default
constructor to your program.
So, the main purpose of the java compilation is to produce .class file of a program which the
machine understands.
Note: Java requires each class placed in its own source file must be same as class name
with extension Java.
When we start compiling the source code, each class is placed in its own .class file that
contains bytecode. Suppose if you want to compile multiple Java files at a time, then you can
use below command:
1 javac *.java
This command will convert all java files to .class file.
With this, we come to an end of this article on the Java Compilation process. I hope you
understood how to compile a java program and are clear about each and every aspect that I
have discussed above.
Output:
To compile and run a java program in command prompt follow the steps written below.
The beauty relies on the parseInt method in the Integer class. Every Number classes such as
Integer, Float, Double and so on have parseXXX methods that convert String into the
respective object of their type.
As we all know that array starts its index with zero. Therefore args[0] is the first index in this
String[] array which is taken from the console. Similarly, args[1] is second, args[2] is the
third element and so on.
When an application is launched, the run-time system passes the command-line arguments to
the application’s main method via an array of Strings.
Output:
Output:
Output: