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

Ruining java class file from different directory: Here, we are going to learn how to run java class file, which is I different directory? By Preeti Jain Last updated : January 02, 2024

Problem statement

  • We have two java file named [Java.java and C.java] and two directories named [E:\Javaprograms and E:\Cprograms].
  • The first java file Java.java is inside E:\Javaprograms directory and The second java file C.java is inside E:\Cprograms directory.

Given problem is to execute Java.class file of E:\Javaprograms directory inside E:\Cprograms directory.

Running java class file which is in different directory

There are various steps to follow to run java class file which is in other directory,

Step 1: Create a Java file

In the first step, we are creating a java file named Java.java in E:\Javaprograms directory.

Java.java

class Java {
    public void display() {
        System.out.println("Java.java file is in E:\\Javaprograms directory");
    }
}

Step 2: Compile

In the second step, we will compile Java.java file in E:\Javaprograms directory so we will perform a few steps.

  1. Open command prompt or terminal from the start menu.
  2. After open terminal, we have to reach the path where our Java.java file has been stored.
  3. C:\Users> cd\ and press enter 
        [To move to the base directory]
    C:\>  e: and press enter and then cd Javaprograms and again press enter.
        [To move to the directory where our Java.java file is stored.]
    E:\Javaprograms> javac Java.java and press enter 
        [If file is successfully compiled then class file will 
        generate in the same directory E:\Javaprograms.] 
    

Step 3: Analysis

In the third step, we will see what will happen if we run java class file named Java.class of [E:\Javaprograms] in another directory named [E:\Cprograms].

Here, we are creating another java file named C.java in E:\Cprograms directory.

C.java:

class C {
    public static void main(String[] args) {
        System.out.println("C.java file is in E:\Cprograms directory");
        // Here we are creating an object of Java.java class 
        // of E:\Javaprograms
        Java ja = new Java();
        Ja.display();
    }
}

Note: If we compile the above program then we will get compile-time error class Java not found because this class is located in E:\Javaprograms directory so we try to execute Java.class inside E:\Cprograms then we will get an error so to overcome this problem when we include Java.class file of E:\Javaprograms directory in this E:\Cprograms directory.

Step 4: Include java.class file to specific directory

In the fourth step, we will see how to include Java.class file of E:\Javaprograms in this E:\Cprograms directory.

With the help of -cp option we can include Java.class of E:\Javaprograms in this E:\Cprograms directory.

Syntax for Compiling:

E:\Cprograms> javac -cp E:\Javaprograms C.java

-cp E:\Javaprograms: -cp with pathname (we will provide the path of the included file and here included file is Java.class is in E:\Javaprograms directory).

C.java: This is the name of the compiled class.

Syntax for Executing:

E:\Cprograms> java -cp E:\Javaprograms; C

Step 5: Let's practice with an example

In the fifth or final step, we will understand with the help of Example,

Example

Java.java inside E:\Javaprograms

class Java {
    public void display() {
        System.out.println("Java.java file is executing in different directory");
    }
}

C.java inside E:\Cprograms

class C {
    System.out.println("C.java file is executing in same directory");
    public static void main(String[] args) {
        // Here we are creating an object of Java.java class 
        // of E:\Javaprograms
        Java ja = new Java();
        ja.display();
    }
}

We will compile and execute C class of E:\Cprograms directory and in that we are executing another java class named Java.class of E:\Javaprograms inside E:\Cprograms with the help -cp option.

E:\Cprograms> javac -cp E:\Javaprograms C.java 
E:\Cprograms> java -cp E:\Javaprograms; C 

Output

E:\Programs>javac -cp E:\Javaprograms C.java
E:\Programs>java -cp E:\Javaprograms; C
C.java file is executing in same directory
Java.java file is executing in different directory 

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.