简体   繁体   English

通过另一个Java程序运行具有输入语句的Java程序

[英]Run a java program having input statements via another java program

I am trying to develop an online Java IDE. 我正在尝试开发在线Java IDE。 I am not able to get input from another java program. 我无法从另一个Java程序获取输入。

The second program: HelloWorld.java 第二个程序: HelloWorld.java

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello world..................");
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Name");
        String name = sc.nextLine();
        System.out.println("you entered " + name);
        sc.close();
    }
}

public class Demo {
    public static void main(String[] args) {
        Process pro = Runtime.getRuntime().exec(javac HelloWorld.java);
        Process pro1 = Runtime.getRuntime().exec(java HelloWorld);
    }
}

The problem is that when I compile and run the above program I am getting "Enter Name" of System.out.println("Enter Name"); 问题是当我编译并运行上述程序时,我得到了System.out.println("Enter Name"); in HelloWorld.java . HelloWorld.java However, after that nothing happens. 但是,此后没有任何反应。

It is not the same "Runtime". 它不是相同的“运行系统”。 Calling exec starts a new process. 调用exec将启动一个新过程。 Interaction with a running process should use the input and output stream from executing the process. 与正在运行的进程的交互应使用执行该进程的输入和输出流。 eg use the following: 例如,使用以下内容:

Process p = Runtime.getRuntime().exec(*******);
InputStream is = p.getInputStream();
InputStream es = p.getErrorStream();
OutputStream os = p.getOutputStream(); 

start with a simple example of passing param to the process manually and afterwards see how you can connect streams from different processes 从一个简单的手动将参数传递给流程的示例开始,然后查看如何连接来自不同流程的流

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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