0

I am using the following code for running an application:

private void RunApp2() throws IOException
{
    StringBuilder sb = new StringBuilder();
    String filePath  = System.getProperty("user.dir");
    String jarfile = filePath + "\\MyAppV2.jar";

    File f = new File(jarfile);
    if(f.exists() && !f.isDirectory()) {
        // do something
    }
    else
    {
        AreThereProblem = true;
    }

    try { // jarname arguments has to be saperated by spaces
        Process process = Runtime.getRuntime().exec("cmd.exe start /C java -jar \""+jarfile + "\"");

        //.exec("cmd.exe /C start dir java -jar "+jarfile+" "+name+" "+id+" dir");
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream ()));
        String line = null;
        while ((line = br.readLine()) != null){
            sb.append(line).append("\n");
        }
        System.out.println("Console OUTPUT : \n"+sb.toString());
        //process.destroy();
    }catch (Exception e){
        lblInformation.setText(e.getMessage());
    }
}

But how can I close MyAppV2.jar application if it is already running before I'm running it again?

Mofi
  • 46,139
  • 17
  • 80
  • 143
esalem
  • 19
  • 1
  • 4
    Have you tried anything yet? – Gurwinder Singh Dec 25 '16 at 04:51
  • 2
    That depends on what mechanisms are available in your program to signal it to terminate, or if you need to *kill* it using OS-depending command. – Andreas Dec 25 '16 at 04:54
  • I added new information – esalem Dec 25 '16 at 14:53
  • Possible duplicate of [How to create a cross-process Singleton class in Java](http://stackoverflow.com/questions/2810378/how-to-create-a-cross-process-singleton-class-in-java) – Elist Dec 25 '16 at 20:53
  • You have to make some additional effort to enable such functionality. Depending on your requirements and possibilities you may use network (use certain port), memory mapped file, another OS-wide synchronization object. You have to deal with OS specific approaches because each java process starts as *separate* OS process. – lospejos Dec 25 '16 at 21:16

0 Answers0