如何在同时接收输出的同时将 stdin 重定向到多个 java 进程

如何解决如何在同时接收输出的同时将 stdin 重定向到多个 java 进程

我正在尝试编写一个 java 程序来启动多个进程并将 stdin 重定向到主程序到这两个进程,同时将这些进程的输出读取到 stdout。

目前我正在尝试启动 vlc 进程,稍后计划能够使用不同的键盘键分别控制它们,但我想了解如何使其适用于任何进程,例如像命令行或自定义 telnet 客户端,因此您可以生成两个命令行并向它们发送相同的命令输入,或者以编程方式替换进入每个进程的部分命令。

我遇到的问题是无法预测哪一端将在何时发送,因此我基本上需要一个双向管道,或者两个管道同时运行。我一直无法弄清楚如何做到这一点,我尝试过的一切都让它挂起,等待任何一种方式的输入。

我可以启动两个 vlc 进程,甚至读取它们的输出,但我无法在进程挂起等待输入的情况下将 stdin 发送给它们。

这是我已经拥有的:

父类:

    package multiVLC;
    
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    public class multiVLC {
        public static void main(String[] args) {
            Path video1 = Paths.get("C:\\path\\to\\video1.mkv");
            Path video2 = Paths.get("C:\\path\\to\\video2.mkv");
            new VLCProcess(video1).start();
            new VLCProcess(video2).start();
        }
    }

过程:

package multiVLC;

import java.io.*;
import java.nio.file.Path;
import java.util.Scanner;

public class VLCProcess extends Thread {

    private Path videofile;

    public VLCProcess(Path videofile) {
        this.videofile = videofile;
    }

    public void run() {
        try {
            String[] cmd = {"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe",videofile.toRealPath().toString()};
            ProcessBuilder ps = new ProcessBuilder(cmd);
            ps.redirectErrorStream(true); // combine stdErr and stdOut
            Process pr = ps.start();

            BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));
            BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));

            String line;

            Scanner scanin = new Scanner(in);
            Scanner scansysin = new Scanner(sysin);

            while (true) {  // THIS HANGS
                if (scanin.hasNextLine()) {
                    System.out.println(this.getName() + ":" + scanin.nextLine());
                }
                if (scansysin.hasNextLine()) {
                    out.write(scansysin.nextLine());
                }
                System.out.println("still in loop"); // NEVER PRINTS
            }

//            while ((line = in.readLine()) != null) { // THIS WORKED TO RUN TWO VLC PROCESSES,BUT DOESN'T REDIRECT STDIN
//                System.out.println(this.getName() + ":" + line);
//            }
//            pr.waitFor(); // not sure if this is needed

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我如何才能正确处理管道?

解决方法

要将程序的输入重定向到多个进程的输出,您可以尝试以下代码,这有望满足您的要求:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;
import java.util.Scanner;

public class Main {
    
    private static final Charset CHARSET = StandardCharsets.UTF_8; //Make sure you use the proper charset.
    
    public static class StartedProcess implements Runnable {
        
        private final BufferedWriter out;
        private final BufferedReader in;
        private final Process pro;

        public StartedProcess(final Process pro) {
            this.pro = Objects.requireNonNull(pro);
            out = new BufferedWriter(new OutputStreamWriter(pro.getOutputStream(),CHARSET));
            in = new BufferedReader(new InputStreamReader(pro.getInputStream(),CHARSET));
            //err = new BufferedReader(new InputStreamReader(pro.getErrorStream(),CHARSET));
        }
        
        @Override
        public void run() {
            final String n = Thread.currentThread().getName();
            try {
                for (String line = in.readLine(); line != null; line = in.readLine())
                    System.out.println(n + ": " + line);
                System.out.println(n + " stream closed.");
            }
            catch (final IOException iox) {
                System.out.println(n + " stream error: " + iox);
            }
        }
        
        public Process getProcess() {
            return pro;
        }
        
        public BufferedWriter getOutput() {
            return out;
        }
        
        //public BufferedReader getError() {
        //    return err;
        //}
    }
    
    private static StartedProcess startProcess(final String... command) throws IOException {
        final ProcessBuilder pb = new ProcessBuilder(command);
        pb.redirectErrorStream(true);
        final StartedProcess sp = new StartedProcess(pb.start());
        new Thread(sp).start(); //Probably you could better utilize an ExecutorService here.
        return sp;
    }
    
    private static String pathToString(final String pathFirst,final String... pathMore) {
        return Paths.get(pathFirst,pathMore).toString();
    }
    
    public static void main(final String[] args) throws IOException { //You should handle this exception appropriately.
        final String vlc = pathToString("path1","to","vlc");
        final String mv1 = pathToString("path2","movie1.mkv");
        final String mv2 = pathToString("path3","movie2.mkv");
        
        System.out.println("Starting processes...");
        
        final ArrayList<StartedProcess> processes = new ArrayList<>(Arrays.asList(
                startProcess(vlc,mv1),startProcess(vlc,mv2)
        ));
        
        final Scanner scan = new Scanner(System.in,CHARSET.name());
        
        while (!processes.isEmpty()) {
            System.out.println("Waiting for your input...");
            final String line = scan.nextLine();
            final Iterator<StartedProcess> procit = processes.iterator();
            while (procit.hasNext()) {
                try {
                    final BufferedWriter out = procit.next().getOutput();
                    out.write(line);
                    out.newLine();
                    out.flush();
                }
                catch (final IOException iox) {
                    procit.remove();
                }
            }
        }
        
        System.out.println("No processes left.");
    }
}

基本上,程序的主线程负责获取用户输入,然后一个简单的循环将用户输入转发到所有存储进程的输出。

每个衍生进程的输入都在其自己的线程中处理,因此您应该确保同步它们,以防在它们之间共享资源。

System.out 确实在进程之间共享,因为在此代码中我只是将所有进程的输入打印到它,但不要担心,因为 PrintStream 的方法已经同步,所以你赢了不会让字节交错,但你会得到交错的行。

您不需要导出每个 InputStreamStartedProcess,我建议您不要这样做,因为它是在 Runnable 本身内部处理的,这就是它的目的.

您可以对此进行编码,以将 ExecutorService 用于 Runnable,并适当地处理 Exception

编辑 1:

我忘了提到,您可能会让您的输入与进程的输出交错,这是因为当您在 CLI 中键入时,进程将同时写入它。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-