how to redirect stdin to java Runtime.exec?

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

I want to execute some sql scripts using Java's Runtime.exec method. I intend to invoke mysql.exe / mysql.sh and redirect the script file to this process. From the command prompt I can run the command

 -p 

I can invoke mysql.exe using Runtime.exec but how do I redirect data from sql file to mysql.exe ?


I read the article in http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4 and used the StreamGobbler mechanism to get the error and output streams. No problem there. The problem comes in reading the file "scripts\create_tables.sql" using BufferedReader and passing the contents to prcess's outputstream. I was expecting the Process to pass the data to the mysql.exe. But I see that only the first line is read from this sql file.

OutputStream outputstream = proc.getOutputStream(); OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream); BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);   while ( (line = br.readLine()) != null)   { bufferedwriter.write(line); bufferedwriter.flush(); System.out.println(line);   }   bufferedwriter.flush();   bufferedwriter.close();   proc.waitFor()  

When I do this I see that only the first line in create_tables.sql is executed. The exit code for the process is 0 and there is other no error or output.

回答1:

Exec returns a Process object to you.

Process has getInputStream and getOutputStream methods.

Just use those to grab the input stream and start shoving bytes into it. Don't forget to read the output stream or the process may block.



回答2:

Redirection is a functionality of the OS shell/cmd enviroments. To invoke them correctly we should use Runtime.exec(String[]) instead of Runtime.exec(String). Here is the code.

public Result executeCmd(String[] cmds, boolean waitForResult) {     Result result = new Result();     result.output = "";     try     {         for(int i=0;i 1)             process=Runtime.getRuntime().exec(cmds);         else             process=Runtime.getRuntime().exec(cmds[0]);         if (waitForResult)         {             StreamGobbler errordataReader = new StreamGobbler(process                     .getErrorStream(), "ERROR");              StreamGobbler outputdataReader = new StreamGobbler(process                     .getInputStream(), "OUTPUT");              errordataReader.start();             outputdataReader.start();              int exitVal = process.waitFor();             errordataReader.join();             outputdataReader.join();             result.returnCode = exitVal;             result.output = outputdataReader.output;             result.error = errordataReader.output;         }     }     catch (Exception exp)     {         result.exp = exp;         result.returnCode = -1;     }     return result; } 

And call this method using

Result result = executeCmd(cmds, true); 

where

CMD[0]::cmd CMD[1]::/c CMD[2]::.\mysql\bin\mysql --host= --port= -u 


添加新评论

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!