Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DataInputStream with Runtime.getRuntime

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have this code:

import java.io.*;

public class TestExecProc
{
public static void main(String[] args) {
try{
Process myProcess;
myProcess = Runtime.getRuntime ().exec (new String [] {"cmd", "/c", "start",
"mysql","-p1234","-u","root"});

String s = null;
DataInputStream in = new DataInputStream(new BufferedInputStream(myProcess.getInputStream()));

while ((s = in.readLine()) != null)
{
System.out.println(s);
}


}
catch (Exception ex) {
System.out.println(ex);
}
}
}


it has 2 problems:

1. I can't see the output in System.out.println(s);
and

2. (more important) I'm trying to create an sql statment and I can't. say I want to do this
Runtime.getRuntime ().exec (new String [] {"cmd", "/c", "start",
"mysql","-p1234","-u","root","use", "myDB;"});

the result are unexpected (it will show errors and exis).

anyone?
Thanks
ppr
 
Joe Ess
Bartender
Posts: 9320
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be nice if you would show us the errors you are getting.
I see one problem: You are using a DataInputStream. If you look at the javadoc for DataInputStream it says:
A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.

I would think it extremely unlikely that the process you are spawning is writing primitive Java data types to the console. Not to mention that the method you are using, readLine, is depreciated:
This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mmm, there were no try and catch error - it simply return null.

I got the problem: i had to do this: -uroot
"mysql","-p1234","-uroot","use", "myDB;"});

anyway, I'm trying to backup my system from the application:

public class TestExecProc
{
public static void main(String[] args)
{
try{
Runtime.getRuntime ().exec (new String [] {"cmd", "/c", "start",
"mysqldump","-p55443","-uroot", "mydb>backup.txt"});
}
catch (Exception ex)
{
System.out.println(ex);
}
}

but the arguments taken excludes the > sign. any idea how to solve it?
 
Joe Ess
Bartender
Posts: 9320
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the VM redirects output to the Process class, you probably can't redirect the output anywhere else. Not reading the output from the Process instance can be problematic:

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

java.lang.Process
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic