This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes DataInputStream with Runtime.getRuntime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "DataInputStream with Runtime.getRuntime " Watch "DataInputStream with Runtime.getRuntime " New topic
Author

DataInputStream with Runtime.getRuntime

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
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

Joined: Oct 29, 2001
Posts: 8836
    
    7

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
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

Joined: Oct 29, 2001
Posts: 8836
    
    7

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: DataInputStream with Runtime.getRuntime
 
Similar Threads
Runtime.getRuntime().exec("sh export LC_ALL=C") fails!!!!!!!!!!
how to execute dos commands using RunTime.getRuntime.exec()
Get the UNIX process ID of the java program
controlling Command Prompt window's size?
How to capture the PID when instantiating for a Java program