[英]Unable to run a java program from another java program
Iam trying to run a java file(editor.java) from another java program(exec.java). 我试图从另一个Java程序(exec.java)运行Java文件(editor.java)。 It takes input and displays file not found message.please give me suggestion through which I run a progam succesfully.
它需要输入并显示未找到文件的消息。请给我建议,以帮助我成功运行程序。
import java.io.*;
public class exec {
public static void main(String argv[]) {
try {
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("Enter the java class name");
String s=br.readLine();
String[] cmd = {"java", "-cp", "E:\netbeans\Project\src", s};
Process pro=Runtime.getRuntime().exec(s);
try (BufferedReader in = new BufferedReader(new InputStreamReader(pro.getInputStream()))) {
String line=null;
while((line=in.readLine())!=null) {
System.out.println(line);
}
}
} catch(Exception err) {
err.printStackTrace();
}
}
java.io.IOException: Cannot run program "editor.java": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at project.exec.main(exec.java:18)
You need to pass your .class file
in your command line argument. 您需要在命令行参数中传递
.class file
。 You don't run a .java
file with a java
command. 您不使用
java
命令运行.java
文件。
Just pass editor
as argument, if the class containing your main method
is editor.class
. 如果包含
main method
的类是editor.class
,则只需传递editor
作为参数。
Also, do follow the @Azodious's
answer below. 另外,请遵循以下
@Azodious's
回答。
Also, you might need to change the path in your array
to the path containing the class file
. 另外,您可能需要将
array
中的路径更改为包含class file
的路径。 src
folder might not be having your class file
src
文件夹可能没有您的class file
So, run your program using: - java exec editor
. 因此,使用以下命令运行程序:
java exec editor
。 I think that should work. 我认为应该可以。
You are not passing commands array to exec
method 您没有将命令数组传递给
exec
方法
Change it to following: 将其更改为以下内容:
Process pro=Runtime.getRuntime().exec(cmd);
and, your error shows that you are trying to run src file: 并且,您的错误表明您正在尝试运行src文件:
Cannot run program "editor.java"
You should pass the .class file name
to run it. 您应该传递
.class file name
来运行它。
inside E:\\netbeans\\Project\\src you found only source file, source file u could not run,,, 在E:\\ netbeans \\ Project \\ src中,您仅找到源文件,您无法运行源文件,
try to do in class file found inside *E:\\netbeans\\project\\build\\classes* 尝试在* E:\\ netbeans \\ project \\ build \\ classes *中找到的类文件中进行操作
String[] cmd = {"class", "-cp", "E:\netbeans\project\build\classes\", s};
NOTE: Check your class path 注意:检查您的课程路径
Thank you 谢谢
Multiple issues - 多个问题-
Are your compiled classes in the same directory as source? 您的编译类与源文件位于同一目录中吗? Usually, with eclipse the classes are generated in
bin
folder. 通常,使用eclipse,这些类在
bin
文件夹中生成。 You should be doing - 你应该做的-
String[] cmd = {"java", "-cp", "E:\\netbeans\\Project\\bin", s};
Process pro=Runtime.getRuntime().exec(s);
Is your editor.java
in the default package? 您的
editor.java
是否在默认软件包中? If not, you need to enter fully qualified name when running the command. 如果不是,则在运行命令时需要输入标准名称。
I would suggest try running the class from command line, and then form the same command from Java code. 我建议尝试从命令行运行该类,然后从Java代码形成相同的命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.