Home » Java programming language

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?
Submitted by Preeti Jain, on July 19, 2019

Given:

  • 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:

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

Solution:

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

1) 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");
    }
}

2) 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.] 
    

3) 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.

4) 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

5) 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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.