为什么我输入批处理文件输入的代码不起作用

为什么我输入批处理文件输入的代码不起作用

本文介绍了为什么我输入批处理文件输入的代码不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试写入已加载的批处理文件进程,但无法弄清楚如何做与返回相同的操作.

I'm trying to write to a loaded batch file process, but I cannot figure out how to do the equivalent of a return.

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以及仅编写和刷新,但是它不起作用.想法?

When run, it should output to a text file on my desktop... but it doesn't seem to take the input? I've tried using \n and \n\n, as well as just writing and flushing, but it doesn't work. Ideas?

推荐答案

首先,对不起,我不是批处理开发人员,而且自从完成任何(严重的)批处理编码以来已经很长时间了,所以我没有无法识别set /p命令...

Firstly, appologise, I'm not a batch developer and it's being a long time since I've done any (serious) batch coding, so I didn't recognize the set /p command...

可能由于多种原因,为什么您的代码无法正常工作,但显而易见的事情是批处理文件中的此命令...

There might be a number of reason WHY you code isn't working, but the obvious thing that stands out is this command in your batch file...

echo test >> test.txt

将"test"与"test.txt"呼应".它没有回显您键入的内容.

Which is "echoing" test to test.txt. It's not echoing what you have typed.

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

To do that, you need to echo the environment variable delBuild, which your input will be assigned to.

echo %delBuild% >> test.txt

还请注意,一旦发送\n,它很可能会将文本提交到环境变量中,并且批处理文件将继续运行.

Also note, that once you send \n, its likely that the text will be committed to the environment variable and the batch file will continue to run.

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

This is the batch file I used in my testing...

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

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

This is the code I used to test it with...

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-恕我直言

Note- I've used ProcessBuilder as it generally easier and more forgiving the trying to use Runtime#exec on it's own - IMHO

这篇关于为什么我输入批处理文件输入的代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:28