Open In App

How to run java class file which is in different directory?

In this article, we will learn about how to use other project’s utilities, classes, and members. Before proceeding let’s learn about some keywords.

classpath



Classpath is the location from where JVM starts execution of a program. Similar to the classic dynamic loading behavior, when executing Java programs, the Java Virtual Machine finds and loads classes lazily (it loads the bytecode of a class only when the class is first used). The classpath tells Java where to look in the filesystem for files defining these classes. Variables and methods which are accessible and available at classpath are known as classpath variables. By default JVM always access the classpath classes while executing a program. JVM always go into the deep of classpath to search for a class or resource.

The JVM searches for and loads classes in this order:



  1. bootstrap classes: the classes that are fundamental to the Java Platform (comprising the public classes of the Java Class Library, and the private classes that are necessary for this library to be functional).
  2. extension classes: packages that are in the extension directory of the JRE or JDK, jre/lib/ext/ user-defined packages and libraries

Using import keyword

import keyword is used in Java to import classes from current project’s classpath. You can import classes from different packages but from same classpath. It is to be remembered that packaging of a class starts from classpath. Suppose you have directory structure as follows:

a > b > c > d > class A

and your classpath starts from c, then your class should be in package d not in a.b.c.d.

Using classpath -cp option

import keyword can import classes from the current classpath, outside the classpath import can’t be used. Now suppose you already have a project in which you have used some utility classes, which you need in your second project also. Then in this situation import keyword doesn’t work because your first project is at another classpath. In that case, you can use -cp command while compiling and executing your program.

Let’s proceed with the following example. Create a directory structure as shown in the figure below.

Here you have 2 projects proj1 and proj2. proj1 contains src and classes. In the src directory, we will keep .java files that are source files and in classes directory, we will keep .classes files that are files generated after compiling the project.
Following are the steps to run java class file which is in different directory:

How to run a java class with a jar in the classpath?

You can also use jar file instead of class files from different classpath. The process will be same, you just have to replace classes folder with jar folder and class name with jar name.
Suppose you have jar file in the lib directory, then to compile you can use

cp_tutorial/proj2/src>javac -d ../classes -cp ../../proj1/lib MainClass.java

and to execute

cp_tutorial/proj2/classes>java -cp ../../proj1/lib; MainClass

Related Article: Setting up Java Environment


Article Tags :