如何使 class 可用于包外的运行时 java 命令
How to make a class available for Runtime java command outside of package
我正在尝试从另一个 Java 程序中 运行 一个 Java 程序,其中两个程序不在同一个包中,实际上什至不在同一个项目中我在 SO 上找到了答案,这里是代码:
try {
Process processCompile = Runtime.getRuntime().exec("javac Main.java");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Process processRun = null;
try {
processRun = Runtime.getRuntime().exec("java Main");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
printLines(" stdout:", processRun.getInputStream());
printLines(" stderr:", processRun.getErrorStream());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
执行以下代码会出现以下错误:
Error: Could not find or load main class Main
现在我知道我必须以某种方式使 Main.java 可以从另一个程序普遍用于 运行 它。我不知道该怎么做。
我真的很感激在这方面的帮助。
I am trying to run a Java program from another Java program where both
programs are not in the same package in fact not even in the same
project and to begin with I had found an answer on SO, here is the
code:
如果 class 是在包中定义的,您应该在 运行 java 命令时指定它:
try {
processRun = Runtime.getRuntime().exec("java Main");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
此外,如果class定义在包中,最好定义在与包对称的目录结构中。我说因为你编译class的时候没有指定Main.java
文件的包路径:
try {
Process processCompile = Runtime.getRuntime().exec("javac Main.java");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我正在尝试从另一个 Java 程序中 运行 一个 Java 程序,其中两个程序不在同一个包中,实际上什至不在同一个项目中我在 SO 上找到了答案,这里是代码:
try {
Process processCompile = Runtime.getRuntime().exec("javac Main.java");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Process processRun = null;
try {
processRun = Runtime.getRuntime().exec("java Main");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
printLines(" stdout:", processRun.getInputStream());
printLines(" stderr:", processRun.getErrorStream());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
执行以下代码会出现以下错误:
Error: Could not find or load main class Main
现在我知道我必须以某种方式使 Main.java 可以从另一个程序普遍用于 运行 它。我不知道该怎么做。
我真的很感激在这方面的帮助。
I am trying to run a Java program from another Java program where both programs are not in the same package in fact not even in the same project and to begin with I had found an answer on SO, here is the code:
如果 class 是在包中定义的,您应该在 运行 java 命令时指定它:
try {
processRun = Runtime.getRuntime().exec("java Main");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
此外,如果class定义在包中,最好定义在与包对称的目录结构中。我说因为你编译class的时候没有指定Main.java
文件的包路径:
try {
Process processCompile = Runtime.getRuntime().exec("javac Main.java");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}