Monday, February 20, 2017

How many ways to run a Java Program

In this session we will learn and understand in how many ways we can run a java program. This is must read topic for all java developers either fresher or experienced. We should organize our computer for command prompt operation. Before we start using java,we have to organize our computer(directory structure)for it. We to first install java in our computer.we have to install it in a directory.In my computer i have directory name javaprog for this purpose.   

There are different ways to run a java program they are:

1) From command prompt we can run .class file with java command

Explanation:  The first step to run any java program is to write a code. To write a java program we have to use a text editor. If we are using windows happily you can use notepad. Using any of these text editors,we must create a file,this file should have extension with .java  let us say we have created a file Demo.java in javaprog directory.

The second step is to compile this file we must execute the command as follows:

c:\javaprog>javac Demo.java

Once compilation is successful, then we should ready for next step.

In the third step, we can run our program by executing the command java as:

c;\javaprog>java Demo



2) From command prompt we can run .jar file with java command

Example: java -jar Demo.jar

3) By double clicking a jar file

4) By double clicking a batch file

I hope all java developers have an idea how to run java program using first 3 points. But using batch file to run a java program less people know about it. we will discuss how to run a java program using batch file now.

Batch file:

Here batch means a group of commands to be executed by a command-line interpreter. A batch file is a kind of script file in Operating System and Microsoft Windows. 

We have to keep all our programs in particular directory. You can give it's name"Javaprog". In windows there is an environment variable that is called "PATH". We can set it by using set command.

Once you enter into DOS Command prompt you should change working directory to javaprog and start working.

Now in our computer,we have created a batch file named xyz.bat as:

set PATH=%PATH%;C:\JDK6\bin\;C:\Javaprog
set classpath=.;C:\Javaprog
Cd c:\javaprog
command

To run xyz.bat file we have to write a command as follows:

java -cp C:\Javaprog  Demo(here i am taking only one command)

When we double click on xyz.bat file automatically above command will execute and our java program will execute.

Note: -cp is used to set class path.


1 comment:

  1. Thanks for writing this in-depth post. You covered every angle. The great thing is you can reference different parts.
    Java Online Training Hyderabad

    ReplyDelete

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...