tags:

views:

340

answers:

3

Hi, I am having the sample code like this.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author padmaja
 */

import java.io.*;
class Test{
  public static void main(String arg[]){
    try{
      String command = "cmd /C start C:/excel.bat";
      Runtime rt = Runtime.getRuntime();
      Process pr = rt.exec(command);  
    }catch (IOException e) {
      e.printStackTrace();
    }
  }
}

I am opening excel sheet by using the above code.

The problem is if use a thread to open excel sheet every 15 minutes then multiple excel sheet will be dispalyed. I want to close the running of a batch file from the above code itself.

Thanks in advance

A: 

One option is to use taskkill command (http://technet.microsoft.com/en-us/library/bb491009.aspx) to kill the process. Of course this will simply terminate the process so you won't be able to do any cleanup/save etc.

Rahul
+2  A: 

Once the Excel sheet is opened, the end-user will interact with it, right? How will you know it is ok to close it? If you are only starting Excel for some macros that run on load, you might be able to exit in that macro itself.

Hemal Pandya
That was my first thought!WHEN do you know, it's the right time to close a sheet? Does a VBA macro works on it? Then this macro should close the file.Never ever speculate about time a process will take.
furtelwart
Very correct. But unless pallavi clarifies we will never know :-)
Hemal Pandya
A: 

In my project we have the same issue.

You can write an DLL which can open and close started processes as you want it. In C++ you can handle the processes like you described. YOu get a process id where you can identify running processes. As far as i know you can only kill a process, not terminate gracefully by asking the user to close the window.

Markus Lausberg