Compilation and Running Java in command Line

 


Date:31/12/2017
Author: MD JUBAYER
Bachelor of Science (8th semester)
Department of Computer Science and Engineering
BRAC University,
66-Mohakhali, Dhaka, Bangladesh.
You can edit this piece of work and republish as your own but you need proper citation to avoid plagiarism.
Get in touch: [email protected]
Facebook/jubayer247
Compilation and Running JAVA program from Command Line
Before proceeding further make sure you are sure about source file which ends with .java extension and executable file ends with .class extension.
To execute a java program:
java -<jvm commands> <ProgramName> <Argument>
Do not include .class extension to the program name.
ProgramName here is the class Name and arguments are
any string that will be passed into main method space will separate them in the array.
 

public class SimpleProgram{
public static void main(String[] args){
for(String arg:args){
		System.out.println(arg);
		}
	}
}


if you write in command line
>java SimpleProgram He is a genius.
the program will produce the following output line by line
He
is
a
genius.
[Most command might not have any jvm commands and some might have multiple jvm commands]
Note: you need to be in the same directory where the class file exists or need to specify the directory
JVM commands are –s,-d,-cp,-sourcepath,-classpath and many more, we will see only the use of classpath for our context of use.
To compile a program from command line:
>javac -<jvm commands> <Program1.java> <Program2.java>
1.Multiple program can be compiled at once with single command but java launch one program per line.
2.Programs can be compiled staying in completely different directory using the absolute file location.
>javac c:\users\user1\Desktop\Program1.java
Using PATH
Set you path variable as shown in the picture, the location for JAVA_HOME where you installed the JDK.

Path is specified where the java compiler and launcher are stored and operating system use them to compile and run java program.
Using classpath:
The concept of using classpath is to compile or run a java program without being in the right place what it actually means is accessing the file without being in the same directory.
There are two strategies to use classpath:
1.Using as Environment Variable
For our purpose we will not be focus using classpath as an environment variable.
Once one will learn about using classpath as a jvm command and should be able to use as an environment variable. Both of them are pretty much same. But there is a disadvantage of using classpath as environment variable because it is very static for every program it remains same. Changing will affect all while using as jvm command it will be different for each program.
2. Using as JVM commands
Key Concept:
1. It will locate only .class file not java source file.
2. One must use classpath when the program imports classes other than java library
3. This is very important when you are specifying classpath jvm tools forgets where the command line is pointing rather it only sees where the classpath is set and you have to include all the location/directories where it should look for classes even if current directory (using . in the classpath) if classes are also in current directory , you need to explicitly set it to the classpath.
4. –cp or –classpath should work fine.
5. ; ans : are separator to window and linux/mac
Sample Commands:
For compilation:
>javac –cp location1;location2/useful.jar;morelocation SimpleJavaProgram.java
For execution:
> java –cp location1; location2/useful.jar;morelocation rootPackage.subpackage.SimpleJavaProgram
Important:
The classes which are under any package must be called with full package name starting from the root package. To import the classes in other words to have access all the classes during runtime –cp must be define to the parent folder of the rootPackage when the program is in the same directory and imports other class from different package or even same package.
Suppose, you have the following files:
E:\projetcs\human\men\Boy.java H:\workspace\project1\PrintDetails.java
E:\projetcs\human\men\Boy.class H:\workspace\project1\PrintDetails.class
H:\workspace\project1\Main.java

To compile from different directory:
>javac -cp E:\projects;H:\workspace\project1 H:\workspace\project1\Main.java
From same directory :
>javac -cp .;E:\projects Main.java
To run from different directory:
>java -cp E:\projects;H:\workspace\project1 Main
From same directory:
>java -cp .;E:\projects Main
[Note: To use classes from jar/zip file just use full path with the filename.jar in classpath
e.g. E:\folder\subfolder\useful.jar and include all jars from the same director e.g.
E:\folder\subfolder\* ]
In this file we have written the tutorial based on a windows system but it works more or less in the same way in Linux or Mac. There is some change in directory structure where “:” is used as a separator instead of “;” and “/” for directory in the following chapter I will demonstrate the process using Linux as well.