Java Runtime.exec 可以是另一个使用 stdin 的 java 程序吗?

发布于 2024-11-25 14:53:22 字数 2163 浏览 0 评论 0原文

我遇到了一个问题,当使用 Java 运行时运行另一个 java 程序时,该程序会冻结,因为另一个程序需要 stdin 。使用 Runtime exec() 执行另一个 java 程序后处理 stdin 时出现问题。

这是我无法开始工作的示例代码。这可能吗?

import java.util.*;
import java.io.*;

public class ExecNoGobble
{
    public static void main(String args[])
    {
        if (args.length < 1)
        {
            System.out.println("USAGE: java ExecNoGobble <cmd>");
            System.exit(1);
        }

        try
        {            
            String[] cmd = new String[3];
                cmd[0] = "cmd.exe" ;
                cmd[1] = "/C" ;
                cmd[2] = args[0];
            Runtime rt = Runtime.getRuntime();
            System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]);
            Process proc = rt.exec(cmd);
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);        
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}

还有 ReadInput.java 文件:

import java.io.*;

public class ReadInput {

   public static void main (String[] args) {

      //  prompt the user to enter their name
      System.out.print("Enter your name: ");

      //  open up standard input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String userName = null;

      //  read the username from the command-line; need to use try/catch with the
      //  readLine() method
      try {
         userName = br.readLine();
      } catch (IOException ioe) {
         System.out.println("IO error trying to read your name!");
         System.exit(1);
      }

      System.out.println("Thanks for the name, " + userName);

   }

}  // end of ReadInput class

最后,启动它的批处理文件:

@echo off

echo.
echo Try to run a program  (click a key to continue)
echo.
pause>nul
java ExecNoGobble "java -cp . ReadInput"
echo.
echo (click a key to end)
pause>nul

我也在这里发布了问题: http://forums.oracle.com/forums/message.jspa?messageID=9747449

I have run into an issue where , when using Java Runtime to run another java program, the program freezes because the other program requires stdin . There is a problem with handling the stdin after executing another java program with Runtime exec() .

Here is sample code that I can't get to work. Is this even possible?

import java.util.*;
import java.io.*;

public class ExecNoGobble
{
    public static void main(String args[])
    {
        if (args.length < 1)
        {
            System.out.println("USAGE: java ExecNoGobble <cmd>");
            System.exit(1);
        }

        try
        {            
            String[] cmd = new String[3];
                cmd[0] = "cmd.exe" ;
                cmd[1] = "/C" ;
                cmd[2] = args[0];
            Runtime rt = Runtime.getRuntime();
            System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]);
            Process proc = rt.exec(cmd);
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);        
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}

And the ReadInput.java file:

import java.io.*;

public class ReadInput {

   public static void main (String[] args) {

      //  prompt the user to enter their name
      System.out.print("Enter your name: ");

      //  open up standard input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String userName = null;

      //  read the username from the command-line; need to use try/catch with the
      //  readLine() method
      try {
         userName = br.readLine();
      } catch (IOException ioe) {
         System.out.println("IO error trying to read your name!");
         System.exit(1);
      }

      System.out.println("Thanks for the name, " + userName);

   }

}  // end of ReadInput class

And, finally , the batch file that launches it:

@echo off

echo.
echo Try to run a program  (click a key to continue)
echo.
pause>nul
java ExecNoGobble "java -cp . ReadInput"
echo.
echo (click a key to end)
pause>nul

I also posted the question here:
http://forums.oracle.com/forums/message.jspa?messageID=9747449

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靖瑶 2024-12-02 14:53:22

调用 Process.getOutputStream 方法并将输入提供给返回的输出流。

API 文档

公共抽象OutputStream getOutputStream()

获取子进程的输出流。输出到流是
通过管道传输到由以下表示的进程的标准输入流中
这个进程对象。

实现说明:输出流最好是
缓冲。

<前><代码>返回:

连接到正常输入的输出流
子流程。

Call the method Process.getOutputStream and feed your input to the returned output stream.

api docs:

public abstract OutputStream getOutputStream()

Gets the output stream of the subprocess. Output to the stream is
piped into the standard input stream of the process represented by
this Process object.

Implementation note: It is a good idea for the output stream to be
buffered.

Returns:

the output stream connected to the normal input of the
subprocess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文