How To Set Path in Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

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.

Why Do You Set A Java Path?


The path is the most important environment variable of the Java environment which is used
to locate the JDK packages that are used to convert the java source code into the machine-
readable binary format.

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.

How To Set Java Path?


There are two methods to set the path.

1. Temporary path
2. Permanent path

Temporary path of JDK


It is very easy to set the temporary path of JDK. Follow the following steps to set the path.

1. Open the terminal or command prompt


2. Go to the path where the JDK/bin directory has been saved
3. Copy the path
4. Enter the following command in the command prompt
This will set the temporary path of JDK.

Permanent Path of JDK


To set the permanent path of the JDK on your system, follow the steps below.

• Open System settings and go to advanced system settings.

• Click on the environment variables in the tab.

• Go to user variables and click on new.


• Add the variable name as ‘path’.
• Copy the path of the JDK bin directory.

• Click ok.

Congratulations, you have successfully added a Java path.

HOW TO WRITE HELLO WORLD PROGRAM IN JAVA

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.

Let us get started.

Hello World Program in Java


Before we get into the details, lets first start with the coding and see how a basic Hello World
program in Java is coded.
1 public class HelloWorldDemo {
2 public static void main(String[] args) {
3 System.out.println( "Hello World!" );
4 System.exit( 0 ); //success
5 }
6 }
Now that you are done with the coding, lets now analyse the program’s syntax in depth.

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.

Line 2: public static void main( String[] args ) {

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:

• public static void main(String[] args)


• public static void main(String []args)

• public static void main(String args[])


• public static void main(String… args)
• static public void main(String[] args)
• public static final void main(String[] args)
• final public static void main(String[] args)

Line 3: System.out.println( “Hello World!” );

System: It is a pre-defined class in java.lang package which holds various useful methods and
variables.

out: It is a static member field of type PrintStream.

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.

• exit(0): It is used to indicate successful termination.


• exit(1) or exit(-1) or any non-zero value: It is used to indicate unsuccessful
termination.

So that was all about the program syntax. Let us now see how to compile Hello World in Java
program.

COMPILING THE PROGRAM


Now what you need to is type in this program in your text editor save it with the class name
that you have used in your program. In my case, I will be saving it as
HelloWorldDemo.java.

Next step is to go to your console window and navigate to the directory where you have
saved your program.

Now to compile the program type in the below command:

1 javac HelloWorldDemo.java
Note: Java is case-sensitive, thus make sure that you type in the file name in the correct
format.

If successfully executed, this command will generate a HelloWorldDemo.class file which


will be machine independent and portable in nature.

Now that you have successfully compiled the program, let us try to execute our Hello World
Program in Java and get the output.

EXECUTING THE PROGRAM


To execute your HelloWorld in Java program on the command line, all you need to do is type
in the below code:

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.

Let us create a simple java program.

Create a java file as HelloWorld.java

1 public class HelloWorld {


2 public static void main(String args[]) {
3 System.out.println("Hello World");
4 }
5 }
To compile this program type the command shown below on your command prompt and
press enter.

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.

Learn How To Use Java Command Line Arguments With Examples

Java programming language is versatile in every aspect, being platform-independent it


makes Java a clear cut winner for any developer. The execution of any Java program is
hustle-free and precise. We can even pass arguments during execution of a program using the
command-line arguments.

What Are Command Line Arguments?


The command-line arguments are passed to the program at run-time. Passing command-line
arguments in a Java program is quite easy. They are stored as strings in the String
array passed to the args parameter of main() method in Java.
1 class Example0{
2 public static void main(String[] args){
3 System.out.println("edureka" + args[0]);
4 }
5 }

Output:

To compile and run a java program in command prompt follow the steps written below.

• Save your program in a file with a .java extension


• open the command prompt and go to the directory where your file is saved.
• Run the command – javac filename.java
• After the compilation run the command – java filename
• Make sure the Java path is set correctly.

Java Command Line Arguments Example


Here are a few examples to show how we can use the command-line arguments in a Java
program.

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.

Factorial of a number using command-line arguments


1 class Example1{
2 public static void main(String[] args){
3 int a , b = 1;
4 int n = Integer.parseInt(args[0]);
5 for(a = 1; a<= n ; a++)
6 {
7 b = b*a;
8 }
9 System.out.println("factorial is" +b);
10 }
11 }

Output:

Sum of two numbers using command-line arguments


1 class Example2{
2 public static void main(String[] args){
3 int a = Integer.parseInt(args[0]);
4 int b = Integer.parseInt(args[1]);
5 int sum = a + b;
6 System.out.println("The sum is" +sum);
7 }
8 }

Output:

Fibonacci Series program using command-line arguments


1 class Example3{
2 public static void main(String[] args){
3 int n = Integer.parseInt(args[0]);
4 int t1 = 0;
5 int t2 = 1;
6
7 for(int i = 1; i <=n; i++){
8 System.out.println(t1);
9 int sum = t1+t2;
10 t1 = t2;
11 t2 = sum;
12 }
13 }
14 }

Output:

Important Points To Remember


• While launching your application, you can use the command-line arguments to
specify the configuration information.
• When you are using command-line arguments, there is no limitation on the number of
arguments. You can use as many as per your requirement.
• Information in the command-line arguments is passed as Strings.
• The command-line arguments are stored in the String args of the main() method of the
program.

You might also like