Given:
鉴于:
We have two java file named [Java.java and C.java] and two directories named [E:\Javaprograms and E:\Cprograms].
我们有两个名为[ Java.java和C.java ]的Java文件,以及两个名为[ E:\ Javaprograms和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.
第一个Java文件Java.java在E:\ Javaprograms目录中,第二个Java文件C.java在E:\ Cprograms目录中。
Given problem:
给定的问题:
Given problem is to execute Java.class file of E:\Javaprograms directory inside E:\Cprograms directory.
给定的问题是执行E:\ Cprograms目录中的E:\ Javaprograms目录的Java.class文件。
Solution:
解:
There are various steps to follow to run java class file which is in other directory,
要执行其他目录中的java类文件,需要执行各种步骤,
1) In the first step, we are creating a java file named Java.java in E:\Javaprograms directory.
1)第一步,我们在E:\ Javaprograms目录中创建一个名为Java.java的Java文件。
Java.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.
2)在第二步中,我们将在E:\ Javaprograms目录中编译Java.java文件,因此我们将执行一些步骤。
Open command prompt or terminal from the start menu.
从开始菜单打开命令提示符或终端。
After open terminal, we have to reach the path where our Java.java file has been stored.
打开终端后,我们必须到达存储Java.java文件的路径。
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].
3)在第三步中,我们将看到如果在名为[ E:\ Cprograms ]的另一个目录中运行名为[ E:\ Javaprograms ]的Java.class的Java类文件将会发生什么。
Here, we are creating another java file named C.java in E:\Cprograms directory.
在这里,我们在E:\ Cprograms目录中创建另一个名为C.java的 Java文件。
C.java:
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.
注意:如果我们编译上述程序,则将得到找不到编译时错误类的Java,因为该类位于E:\ Javaprograms目录中,因此我们尝试在E:\ Cprograms中执行Java.class,那么将得到一个错误因此,当我们在此E:\ Cprograms目录中包含E:\ Javaprograms目录的Java.class文件时,可以克服此问题。
4) In the fourth step, we will see how to include Java.class file of E:\Javaprograms in this E:\Cprograms directory.
4)在第四步中,我们将看到如何在该E:\ Cprograms目录中包含E:\ Javaprograms的 Java.class文件。
With the help of –cp option we can include Java.class of E:\Javaprograms in this E:\Cprograms directory.
随着-cp选项的帮助下,我们可以包括电子商务 的java.class:\ Javaprograms在此E:\ Cprograms目录。
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).
–cp E:\ Javaprograms :带有路径名的-cp (我们将提供包含文件的路径,此处包含的文件是Java。class在E:\ Javaprograms目录中)。
C.java: This is the name of the compiled class.
C.java :这是已编译类的名称。
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,
5)在第五步或最后一步中,我们将借助示例来理解,
Example:
例:
Java.java inside E:\Javaprograms
E:\ Javaprograms中的Java.java
class Java {
public void display() {
System.out.println("Java.java file is executing in different directory");
}
}
C.java inside E:\Cprograms
E:\ Cprograms中的C.java
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目录的C类 ,并使用help –cp选项在E:\ Cprograms中执行另一个名为Java.class of E:\ Javaprograms的 Java类。
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
翻译自: https://www.includehelp.com/java/how-to-run-java-class-file-which-is-in-different-directory.aspx