【问题标题】:Why is my code to enter input to a batch file not working?为什么我的输入批处理文件的代码不起作用?
【发布时间】:2013-08-13 00:23:13
【问题描述】:

我正在尝试写入已加载的批处理文件进程,但我不知道如何执行相当于返回的操作。

Java 代码:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Scanner;

public class Start {
    public static void main(String[] args) {
        try {
            Process p = Runtime.getRuntime().exec("C:\\Users\\Max\\Desktop\\test.bat");// Runtime.getRuntime().exec("keytool -genkey -alias " + name.replace(" ", "").trim() + "  -keystore key");
            DataInputStream in = new DataInputStream(p.getInputStream());
            Scanner scanner = new Scanner(in);
            // System.out.println(scanner.nextLine());
            DataOutputStream out = new DataOutputStream(p.getOutputStream());
            out.write("test\n\n".getBytes());
            out.flush();
            out.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

批号:

@echo off
set /p delBuild=lozors?: 
echo test >> test.txt

运行时,它应该输出到我桌面上的文本文件...但它似乎没有接受输入?我尝试过使用 \n 和 \n\n,以及仅写入和刷新,但它不起作用。想法?

【问题讨论】:

  • 为什么在编写文本时使用 DataOutputStream?为什么不使用 PrintStream 并简单地使用其println(...) 方法来编写新行?
  • 您期望发生什么? test.bat 不读取任何输入。
  • "但它似乎不接受输入?" 但为什么要接受呢?首先,批处理文件不需要任何输入
  • 批处理文件正在将字符串test 写入test.txt 的末尾。无论您在命令行上键入什么内容,它都会执行此操作。
  • 我还“怀疑”当您从进程中读取输入时,它可能已经停止执行......

标签: java batch-file stream return output


【解决方案1】:

首先,抱歉,我不是批处理开发人员,而且我已经很久没有进行任何(严肃的)批处理编码了,所以我不认识set /p 命令...

您的代码无法正常工作可能有多种原因,但最突出的一点是批处理文件中的这个命令...

echo test >> test.txt

这是“呼应”testtest.txt。它不会与您输入的内容相呼应。

为此,您需要回显环境变量delBuild,您的输入将被分配给该变量。

echo %delBuild% >> test.txt

另外请注意,一旦您发送\n,文本很可能会被提交到环境变量并且批处理文件将继续运行。

这是我在测试中使用的批处理文件...

@echo off
set /p delBuild=lozors?: 
echo %delBuild% >> test.txt

这是我用来测试它的代码...

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TestProcessBuilder02 {

    public static void main(String[] args) {
        try {
            ProcessBuilder pb = new ProcessBuilder("test.bat");
            pb.redirectError();
            Process p = pb.start();

            OutputStream os = null;
            try {
                os = p.getOutputStream();
                os.write("I am invincible".getBytes());
            } finally {
                try {
                    os.close();
                } catch (Exception e) {
                }
            }
            InputStream is = null;
            try {
                is = p.getInputStream();
                int in = -1;
                while ((in = is.read()) != -1) {
                    System.out.print((char)in);
                }
            } finally {
                try {
                    is.close();
                } catch (Exception e) {
                }
            }
            int exit = p.waitFor();
            System.out.println("Exited with " + exit);
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }

}

注意 - 我使用了 ProcessBuilder,因为它通常更容易和更宽容地尝试单独使用 Runtime#exec - 恕我直言

【讨论】:

    【解决方案2】:

    我实际上只是使用了答案:Communicate with a windows batch file (or external program) from java,但我没有使用缓冲写入器,因为它似乎阻止了它的工作。

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 2017-11-02
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 2013-08-06
      相关资源
      最近更新 更多