0

I want implement a timed pdf in java. Basically open a pdf and have it open for any number of seconds I desire and then close automatically after the time runs out. I tried running on a thread and putting the thread to sleep that didn't seem to help. I am stuck and I cant seem to find anything online that would help me.

package testing;
import java.awt.Desktop;
import java.io.File;


/**
 *
 * @author xxx
 */
public class PrimeThread implements Runnable {

    String name;
    int time;


    public PrimeThread(String x)
    {
    name = x;
    time = 30000;
    }
    @Override
    public void run() {
     try{
     System.out.printf("%s os sleeping for %d\n", name, time);
     try{    
         File fi = new File("C:\\Users\\xxx\\Downloads\\CBP Form 3078.pdf");
         Desktop.getDesktop().open(fi);



     }
     catch(Exception ex){

     }
     Thread.sleep(time);
     System.out.printf("%s is done\n", name);
     }
     catch(Exception e){

   }
}
Tim B
  • 40,716
  • 16
  • 83
  • 128
  • Possible duplicate of [How to kill a process in Java, given a specific PID](http://stackoverflow.com/questions/4633678/how-to-kill-a-process-in-java-given-a-specific-pid) – Mark Sholund Nov 28 '15 at 23:20
  • 2
    You would have to change your approach, Desktop.open hands off control to the OS so you've lost control of the process as far as I can tell. – Mark Sholund Nov 28 '15 at 23:22

1 Answers1

3

As soon as you did Desktop.getDesktop().open(fi); then you opened the file in your computer's default PDF viewer. At that point you no longer have any control over what is going on, it's a completely separate process.

You would need to find out what process you just created somehow and send it a kill signal or use a java PDF viewer running inside your application so you have control over it.

Tim B
  • 40,716
  • 16
  • 83
  • 128