Usage of javac and java commands, setting up classpath

In the last topic we have seen how to set path environment variable. Now, we will see how to set classpath and how to use javac command with -d option.

Let us consider following java program.

class ProgramOne{
  public static void main(String[] args){
  System.out.println("Setting Classpath Environment Variable");
}
}

Type the above program in notepad and save it as “ProgramOne.java”. I will save it in C:\Sources in my system.

Separating java files and class files

Till now, we have seen, when you type javac command in command prompt, .class file is generated in the same folder as that of .java file. Usually we should keep our java files and class files in seperate folders. How to make this .class file to be generated in another folder say C:\Classes (in my system)? This we can achieved using -d option of javac command.

Open the command prompt and go to the location of .java file using CD command.

C:\> CD Sources                                                             ——-> (In My System)

Now type the javac command with -d option. Syntax of javac command with -d option is,

>javac  -d  (Specify the path where to save generated .class files)  FileName.java

We have already set the path environment variable for the whole system in the last topic, so no need to set it in the command prompt. If you have not set the path environment variable, go through our previous concept to see how to set path environment variable.

Directly run javac command like below.

C:\Sources> javac -d  C:\Classes ProgramOne.java

This will save generated .class file in I:\Classes.

Why we need classpath?

If you run java command from C:\Sources, you will get an error saying could not find or load main class. Because .class file is saved in another folder. To run .class file, you need to go to that location, again using CD command. This will be the time consuming. The easy and best way to run .class files saved in another folder is use  -classpath option of java command. Syntax of java command with -classpath option is,

>java  -classpath  (path of generated .class files)  ClassName

In our example it looks like,

C:\Sources> java -classpath C:\Classes ProgramOne

You can also avoid use of -classpath option each time you run java command by setting the classpath variable by using set classpath command.

C:\Sources> set classpath=C:\Classes

You can also check whether the classpath has been correctly set or not using echo command.

C:\Sources> echo %classpath%       ——-> It will display the value of classpath variable.

Now you can run java command without using -classpath option.

C:\Sources> java ProgramOne

This classpath setting is available only for this instance of command prompt. You can also avoid setting classpath each time you open command prompt by setting classpath in Environment variables section of your system. The procedure to set classpath variable is same as that setting path environment variable as we discussed in previous concept.

As of now, Our java files contain only one class. can we include more than one class in one java file? If we can include, then what should be the name of java file? How to compile and run them? We will see it in out next Topic.

Leave a Reply