Question

everybody

I have following problem. I want to use Java process builder to start external application (win32 executable based on Qt if it's of any importance). Starting external application works well. Application is started as it's expected. That part works fine. Besides starting external applications I also want to keep track of instances in order to be able to "destroy" any instance still running when exiting Java app.

When I start applications I do the following:

ExternalTaskTracking externalProcessList = new ExternalTaskTracking();

...

ProcessBuilder pb = new ProcessBuilder(arguments);
Process newProcess=pb.start();
externalProcessList.addProcess(newProcess);

...

ExternalTaskTracking is simple Java class I use (that is I intend to use) to track processes.

public class ExternalTaskTracking {
protected ArrayList<Process> processList;
protected Integer  processCount;

public ExternalTaskTracking(){
    super();
    processCount=0;
}

public void addProcess(Process uProcess){
    processCount++;
    processList.add(uProcess);
    //processList.set(processCount, uProcess);

}//end addProcess(Integer uProcess)

public Boolean killProcess(Process uProcess){
    Integer index=processList.indexOf(uProcess);
    if (index==-1){
        return false;
    }//end if
    try{
        uProcess.destroy();
        processList.remove(index);
        processCount--;
        return true;
    }catch (Exception ex){
         logger.error(ex.toString());
         return false;
    }//end try-catch
}//end killProcess(Process uProcess)

public void killAll(){
    for (Integer i=0;i<processCount;i++){
        try{
            Process p=processList.get(i);
            if (p!=null){
                p.destroy();
            }else{
                logger.error("killAll()->Null process?!?");
            }//end if
        }catch (Exception ex){                
           logger.error(ex.toString());
        }//end try-cath
    }//end for
}//end killAll()
}//end class

Problem is that rezult of pb.start() is always null, while external app is successfully started, so I can not maintain process list. Any ideas what I am doing wrong?

Was it helpful?

Solution

You have not initialized processList.

public ExternalTaskTracking(){
    super();
    processList = new ArrayList<Process>();
    processCount=0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top