Why is my code to enter input to a batch file not

2019-08-30 18:59发布

问题:

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 Code:

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();
        }
    }
}

Batch code:

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

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?

回答1:

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

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

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

echo %delBuild% >> test.txt

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();
        }
    }

}

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



回答2:

I actually just used the answer at: Communicate with a windows batch file (or external program) from java except, I didn't use the bufferedwriter as it seemed to stop it from working.