简体   繁体   English

无法从另一个Java程序运行Java程序

[英]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 - 多个问题-

  1. You are trying to run command "editor.java" from command line. 您正在尝试从命令行运行命令“ editor.java”。 Your command array remains unused. 您的命令数组保持未使用状态。
  2. 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);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM