File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Runtime.exec to wait for external application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.exec to wait for external application" Watch "Runtime.exec to wait for external application" New topic
Author

Runtime.exec to wait for external application

Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 634
Hi,

I am using runtime with following code



now problem is, when this cmd is executed a db2 console is opened (which is fine) but java losses the control of it and there is no way to find out if the table creation is over and wether it was success or failure, even process.waitFor() doesnot wait and process.exitValue() gives 0 (i,e sucess)
any pointer?

-P


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
Why don't you run your DDL commands via JDBC? I don't remember the details, but in one project I ran the BACKUP DATABASE command via some administrative procedure.
ashu Suri
Greenhorn

Joined: Oct 22, 2008
Posts: 18
Try StreamGobblers.
This should solve your problem.
Sample code :

Runtime rt = Runtime.getRuntime();

Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");

// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");

// kick them off
errorGobbler.start();
outputGobbler.start();

// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);



class StreamGobbler extends Thread
{
InputStream is;
String type;

StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}

public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}

Try this.
Thanks,
Gagan
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
ashu Suri wrote:Try StreamGobblers.
This should solve your problem.


The problem is that db2cmd opens a new window. So if you open a standard Windows command prompt, and type
db2cmd
a new window with the required DB2 environment initialised will pop up. You won't be able to access stdout and stderr used in that window.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 634
Hey Gagan,

this did not work, the exit value '0' is printed even if db2cmd window is still opening..

Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
You don't even need the ADMIN_CMD procedure, you can run the DDL through JDBC. See
http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/JDBC20.html
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 634
okay about running ddl , yeha i can , in this case i am creating database in db2, which i doubt is possible using jdbc.

-P
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
Praful Thakare wrote:okay about running ddl , yeha i can , in this case i am creating database in db2, which i doubt is possible using jdbc.


If you had read the article I had linked, you would have found it is possible with some databases and not with others - don't know about DB2, but it's worth a try, especially when the other way, via exec(), does not work. Hmm?

Creating a database. A database can be created using tools supplied by the database vendor, or via SQL statements fed to the database from a Java program. Since there is normally a database administrator (of course, as a developer, this may be you), and not all JDBC drivers support database creation through Data Definition Language ( DDL), this topic will, in general, be left as DBMS (DataBase Management System) and driver specific. If you are interested in more details, there typically is a CREATE DATABASE statement, but be sure to review your DBMS SQL reference, as it is not part of the SQL standard, but is DBMS-dependent.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 634
Thanks for your comments Istvan, though i have some different requirement of creating entire environment, db, app server,messaging q etc using on program henc ei am using java runtime, any ways fond the solution, incase some one stumbles here in future.



Cheers
-P
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime.exec to wait for external application