0

I have a class file look like below:

import java.util.*;
import java.io.*;


public class GnuPG {

    public GnuPG() {
    }


    public static void doDecrypt(String strInput, String strOutput, String pass) throws Exception {
        String[] cmd = { "cmd", "/c", "cd C:\\GnuPG\\ & gpg.exe --passphrase "+pass+" --output "+strOutput+" --decrypt "+strInput+" & echo exit! & exit(1)"};
        Process p = Runtime.getRuntime().exec(cmd);
        inheritIO(p.getInputStream(), System.out);
        inheritIO(p.getErrorStream(), System.err);
    }

    private static void inheritIO(final InputStream src, final PrintStream dest) {
        new Thread(new Runnable() {
            public void run() {
                Scanner sc = new Scanner(src);
                while (sc.hasNextLine()) {
                    dest.println(sc.nextLine());
                }
            }
        }).start();
    }    

}

which run the GnuPG to do decryption of gpg file, im not sure what is the correct way to stop the process when the gpg.exe is done its job. Currently im using :

echo exit! & exit(1)

at the cmd command at the end. Is that is a proper way to kill the process?

Teddybugs
  • 1,232
  • 1
  • 12
  • 37
  • You should put a `try-catch` in your `run()` and look for `InterruptedException`. When you want to stop the thread you can just send an interrupt. – user2004685 Mar 24 '16 at 07:18
  • how is that duplicate? code is not same – Teddybugs Mar 24 '16 at 07:21
  • 2
    @David but the topic is the same – SomeJavaGuy Mar 24 '16 at 07:22
  • nice, changed topic, get devoted and mark duplicate, how is that helpful? the possible answer you suggest is totally different from what i need. – Teddybugs Mar 24 '16 at 07:30
  • got my question solved from http://stackoverflow.com/questions/9029795/new-runnable-but-no-new-thread by defining a thread like: Thread theprocess = new Thread(new Runnable() { and later start it like this theprocess.start(); it will auto exit the process once the gpg.exe done its job.. hope this answer will help other.. still no idea why they mark duplicate this.. nothing related to suggested duplicate post. – Teddybugs Mar 24 '16 at 08:00

0 Answers0