Providing input to php script when launched through java application -
i need run php
script through java
application , give input based on questions.
i found stream_get_line or fgets hangs every time send input.
this not happen other applications (for example if execute xcopy).
i have tried lot of approaches (most of them visible in code below), main suspicion might encoding (or stream_get_line , fgets in php has bug). help?
since actual php script third party, cannot change it.
php code
<?php $tty = os_windows ? @fopen('\con', 'r') : @fopen('/dev/tty', 'r'); $myfile = fopen("c:\\temp\\tty_try.php.txt", "w") or die("unable open file!"); echo "internal encoding:", mb_internal_encoding(), "\r\n"; //<=== gives utf-8 print "enter something: "; fwrite($myfile, "going wait input..."); $tmp = stream_get_line($tty, 10, php_eol); //fgets($tty); //, 1024); //when launched through java application following statements never executed echo "encoding of output:", mb_detect_encoding($tmp), "\r\n";//<=== gives ascii fwrite($myfile, "got input: "); fwrite($myfile, $tmp); echo "and answer is: |", $tmp, "|"; fclose($myfile); ?>
java code
private void testinputtophpcommand() { try { process process = runtime.getruntime().exec("c:\\php\\php.exe c:\\temp\\tty_try.php"); outputstream stdin = process.getoutputstream(); inputstream stdout = process.getinputstream(); bufferedreader reader = new bufferedreader(new inputstreamreader(stdout)); bufferedwriter writer = new bufferedwriter(new outputstreamwriter(stdin)); bufferedwriter out = new bufferedwriter (new outputstreamwriter(stdin, //standardcharsets.utf_16)); //standardcharsets.us_ascii)); standardcharsets.utf_8)); string line = ""; int read = 0; char [] buf = new char[256]; while(-1 != (read = reader.read(buf))) { line = new string(buf, 0, read); if (line.endswith("enter something: ")) { string newline = system.getproperty("line.separator"); //byte[] bytes = newline.getbytes("us-ascii"); byte[] bytes = newline.getbytes("utf-8"); //out.newline(); //stdin.write(bytes); //stdin.close(); //string input = new string(bytes, "us-ascii"); string input = new string(bytes, "utf-8"); out.write(input); out.close(); /* out.write("\\h"); out.newline(); out.flush(); out.write("\\q"); out.newline(); out.flush(); */ } } process.waitfor(); } catch (ioexception | interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } }
Comments
Post a Comment