user1077980
user1077980

Reputation: 155

Executing .exe file from java application

I wrote a C program that simply delete the folder called myFolder.txt
I want to execute the .exe file from a java application.
So, I used the following code:

 try
 {
    Runtime rt = Runtime.getRuntime() ;
    Process p = rt.exec("program2.exe") ;
     p.destroy() ;
 }catch(Exception exc){/*handle exception*/
System.out.println("ERROR");
    }

When I run my java application no error appears but the file is not deleted.

Why?

Upvotes: 2

Views: 1132

Answers (2)

sethupathi.t
sethupathi.t

Reputation: 502

I think that program2.exe might not be in the class path of the Java project.

try  {
    Runtime rt = Runtime.getRuntime() ;
    Process p = rt.exec("program2.exe") ; // @1
    //p.destroy() ; // @2
} catch (Exception exc) {
    /*handle exception*/
    System.out.println("ERROR");
}

@1 - check path of the exe file.

@2 - no need to destroy the process manually, it will end automatically after completing its process.

You can check whether the the process is started or not, run the Java project - immediately go to task manager - process - if there is a process running called program2.exe, your process is started otherwise it is not started. If not started, there is no exception - then the exe file path is a problem, try with giving full path of the exe file.

Upvotes: 2

kennytm
kennytm

Reputation: 523724

You have created a process, and then immediately destroyed it. Of course the executable won't run. Try calling .waitFor() instead (or just let it run).

Upvotes: 6

Related Questions