java - Why is my code to enter input to a batch file not working? -
i'm trying write loaded batch file process, cannot figure out how equivalent of 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, should output text file on desktop... doesn't seem take input? i've tried using \n , \n\n, writing , flushing, doesn't work. ideas?
firstly, appologise, i'm not batch developer , it's being long time since i've done (serious) batch coding, didn't recognize set /p
command...
there might number of reason why code isn't working, obvious thing stands out command in batch file...
echo test >> test.txt
which "echoing" test
test.txt
. it's not echoing have typed.
to that, need echo environment variable delbuild
, input assigned to.
echo %delbuild% >> test.txt
also note, once send \n
, text committed environment variable , batch file continue run.
this batch file used in testing...
@echo off set /p delbuild=lozors?: echo %delbuild% >> test.txt
this code used test 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 invincible".getbytes()); } { try { os.close(); } catch (exception e) { } } inputstream = null; try { = p.getinputstream(); int in = -1; while ((in = is.read()) != -1) { system.out.print((char)in); } } { try { is.close(); } catch (exception e) { } } int exit = p.waitfor(); system.out.println("exited " + exit); } catch (exception exp) { exp.printstacktrace(); } } }
note- i've used processbuilder
easier , more forgiving trying use runtime#exec
on it's own - imho
Comments
Post a Comment