P
Paul Smith
I have 3 questions in this post that I would be most grateful for some
help with.
I want to start a process that has its input sourced from a file. I
tried:
Process process = Runtime.getRuntime().exec("c:\mysql\bin\mysql.exe
--database=bob < c:\file.sql"
but I get a usage error returned, even though running the same command
from a command prompt is fine.
The problem appears to surround the redirection of standard input
(i've tried putting a backslash in front of it).
QUESTION 1 -- Is this not possible using Runtime.exec()?
A workaround I have thought of is to pass the name of the file
containing the sql to my Java program, read it in and pass it to the
input stream of the external process. However, my application hangs
when I do this.
QUESTION 2 -- Are there any other alternatives to the workaround
below?
QUESTION 3 -- Why does the code below cause my Java program to hang?
I've seen it mentioned that the handling of the external process'
input should be on a different thread. Why is that?
The workaround code is:
try
{
OutputStream outputstream = process.getOutputStream();
OutputStreamWriter outputstreamwriter = new
OutputStreamWriter(outputstream);
BufferedWriter bufferedwriter = new
BufferedWriter(outputstreamwriter);
BufferedReader br = new BufferedReader(new FileReader(fileName));
String s = br.readLine();
while (s != null)
{
bufferedwriter.write(s);
s = br.readLine();
}
bufferedwriter.flush();
}
catch (FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
help with.
I want to start a process that has its input sourced from a file. I
tried:
Process process = Runtime.getRuntime().exec("c:\mysql\bin\mysql.exe
--database=bob < c:\file.sql"
but I get a usage error returned, even though running the same command
from a command prompt is fine.
The problem appears to surround the redirection of standard input
(i've tried putting a backslash in front of it).
QUESTION 1 -- Is this not possible using Runtime.exec()?
A workaround I have thought of is to pass the name of the file
containing the sql to my Java program, read it in and pass it to the
input stream of the external process. However, my application hangs
when I do this.
QUESTION 2 -- Are there any other alternatives to the workaround
below?
QUESTION 3 -- Why does the code below cause my Java program to hang?
I've seen it mentioned that the handling of the external process'
input should be on a different thread. Why is that?
The workaround code is:
try
{
OutputStream outputstream = process.getOutputStream();
OutputStreamWriter outputstreamwriter = new
OutputStreamWriter(outputstream);
BufferedWriter bufferedwriter = new
BufferedWriter(outputstreamwriter);
BufferedReader br = new BufferedReader(new FileReader(fileName));
String s = br.readLine();
while (s != null)
{
bufferedwriter.write(s);
s = br.readLine();
}
bufferedwriter.flush();
}
catch (FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}