The Artima Developer Community
Sponsored Link

Java Answers Forum
Problem during executing Command Prompt through java

2 replies on 1 page. Most recent reply: Jun 9, 2008 5:10 AM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
keshari lal

Posts: 3
Nickname: keshari
Registered: May, 2008

Problem during executing Command Prompt through java Posted: Jun 4, 2008 5:41 AM
Reply to this message Reply
Advertisement
Hi All

My current code is executing system commands through java programme,like “dir”,”date” itc.Whenever I run the code the desired output comes.But when I am running the code continuselly then lots of command prompt open regularlly.
Is there any way to stop the code from execution untill unless the command prompt get closed manually?Here is my code.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

interface IPerlService
{
public String BZMCreateZone(String s);
public int BZMRenew();
}
public class Sample
{
public void BZMCreateZone()
{
Runtime runtime;
Process process;
String s=null;
String str=null;

try
{
runtime = Runtime.getRuntime();

process =runtime.exec("cmd /c start dir");

str=process.toString();
try
{
process.waitFor();
}
catch (InterruptedException e)
{
e.printStackTrace();
}

BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
s = reader.readLine();

}
catch (IOException ex)
{
ex.printStackTrace();

}


}
public static void main(String[] args)
{
Sample obj_Sample= new Sample();
obj_Sample.BZMCreateZone();
}
}


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Problem during executing Command Prompt through java Posted: Jun 9, 2008 5:01 AM
Reply to this message Reply
Just looked at the Runtime API, would addShutdownHook() not work? You make the Shutdown hook Thread perform ^C.

Process child = Runtime.getRuntime().exec("yourCommand");
Thread shutDownThread = new Thread() {
        public void run() {
            OutputStream ctrlC = child.getOutputStream();
            out.write("^C".getBytes());
            out.close();
        }
    });


Keep in mind this is just a suggestion i.e. this is the route I'd pursue. Have never tried it - so I'm not sure it would work.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Problem during executing Command Prompt through java Posted: Jun 9, 2008 5:10 AM
Reply to this message Reply
> Just looked at the Runtime API, would addShutdownHook()
> not work? You make the Shutdown hook Thread perform ^C.
>
>
> 
> Process child = Runtime.getRuntime().exec("yourCommand");
> Thread shutDownThread = new Thread() {
>         public void run() {
>             OutputStream ctrlC = child.getOutputStream();
>             out.write("^C".getBytes());
>             out.close();
>         }
>     });
> 

>
> Keep in mind this is just a suggestion i.e. this is the
> route I'd pursue. Have never tried it - so I'm not sure
> it would work.

My apologies, actually in the Shutdown thread, after executing your Ctrl+C command maybe you should input your second CMD to take in - that would make more sense.

You shutdown is called at the end of your process, then the
next process is executed.
out.close();
//  You then do within shutDownThread
child = Runtime.getRuntime().exec("anotherCommand");

Flat View: This topic has 2 replies on 1 page
Topic: Null pointer exception during executeUpdate(sql) Previous Topic   Next Topic Topic: Corba retry mechanism

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use