Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 117,527 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,979 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



problem in executing Batch file by using Runtime.getRuntime().exec

 
Reply to this topicStart new topic

problem in executing Batch file by using Runtime.getRuntime().exec, How to apply Wait() and notify() method

imranbaseer
post 2 Jul, 2008 - 03:44 AM
Post #1


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 2

Hi,
* This is my code ...........
* In the below code for loop is executed before the .bat file, so that i gave this.wait() and notify() method , it waits for long time and it doesn't execute the for loop ,it will execute only .bat and it wait for notify the thread.......
* this.notify() is not execute for the below code.....
* Tell me suggestion for the below code and how solve it.........


CODE
public class Waitfor
{
    public synchronized void work(){
        try
        {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("cmd /c start D:\\test.bat");
            this.wait();
            this.notify();
            for(int i=0; i<6; i++ ){
                System.out.println(i);
            }
        }
        catch (Exception e)
        {
           e.printStackTrace();
        }
        }
    public static void main(String args[])
    {
        Waitfor w=new Waitfor();
        w.work();
    }
}




Thanks
J.Imran
User is offlineProfile CardPM

Go to the top of the page


gl3thr0
post 2 Jul, 2008 - 05:02 AM
Post #2


D.I.C Head

**
Joined: 27 Oct, 2007
Posts: 177


My Contributions


if u want it to wait use sleep() from the Thread class..

so u could do this..

CODE

System.out.println("to red");
Thread.sleep(1000);
System.out.println("to blue");
Thread.sleep(1000);
System.out.println("to green");
Thread.sleep(1000);
System.out.println("Done");


the methods wait() and notify() dont work the way u think
User is offlineProfile CardPM

Go to the top of the page

pbl
post 2 Jul, 2008 - 02:58 PM
Post #3


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,278



Thanked 132 times

Dream Kudos: 75
My Contributions



CODE
public class Waitfor
{
    public synchronized void work(){
        try
        {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("cmd /c start D:\\test.bat");
            this.wait();
            this.notify();
            for(int i=0; i<6; i++ ){
                System.out.println(i);
            }
        }
        catch (Exception e)
        {
           e.printStackTrace();
        }
        }
    public static void main(String args[])
    {
        Waitfor w=new Waitfor();
        w.work();
    }
}


wait() makes you wait until another Thread apply a notify() to you
As you sleep you can't execute your notify() command that would wake you up

You can do wait(10000L);

that will put you on hold unitl somebody notify() you or 10000 ms passed
but then remove your notify() statement
User is online!Profile CardPM

Go to the top of the page

imranbaseer
post 2 Jul, 2008 - 10:05 PM
Post #4


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 2

* Tanx for quick reply for my forum.....
* I have already used sleep(1000) and wait(1000)......
* I have to solve this problem without specify the millisecond in the parameter.....
* How to do it by wait() and notify() method.........

Thanks in Advance,
ImranJ
User is offlineProfile CardPM

Go to the top of the page

pbl
post 3 Jul, 2008 - 02:59 PM
Post #5


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,278



Thanked 132 times

Dream Kudos: 75
My Contributions


QUOTE(imranbaseer @ 2 Jul, 2008 - 10:05 PM) *

* How to do it by wait() and notify() method.........

Thanks in Advance,
ImranJ

You'll have to have another Thread that check regularly if the job is done and then notify() your main Thread that job is done
If you know nothing about Thread you should read a tutorial about it
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 03:47PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month