# Is there a way to get only one program? \[JAVA Mode Program\]

**URL:** <https://discourse.processing.org/t/is-there-a-way-to-get-only-one-program-java-mode-program/11096>\
**Category:** Processing\
**Created:** [May 10, 2019, 8:19am UTC](https://discourse.processing.org/t/is-there-a-way-to-get-only-one-program-java-mode-program/11096 "2019-05-10T08:19:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [May 10, 2019, 8:19am UTC](https://discourse.processing.org/t/is-there-a-way-to-get-only-one-program-java-mode-program/11096/1 "2019-05-10T08:19:53Z")

</div>

![t1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a83880912e32ea100490f2fd69a60d436ad681f4.jpeg)

Is there a way to get only one program?

When you click an exe file N times, N programs are turned on.  
So, is there a way to turn on just one program with N clicks of an exe file?

> > How can I run only one program with N clicks?

Please help me. ~~

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [May 12, 2019, 2:22am UTC](https://discourse.processing.org/t/is-there-a-way-to-get-only-one-program-java-mode-program/11096/2 "2019-05-12T02:22:14Z")

</div>

From the screenshot, I can see you are using Windows. One idea is that you run the [tasklist](https://superuser.com/questions/18830/is-there-a-command-in-windows-like-ps-aux-in-unix) command from Processing and search for current running sketches. If there is one, then you kill the current sketch.

Another approach is that you check for an environmental variable. If it does not exist, launch the sketch and set it. Otherwise, do not lunch the sketch. In addition, you need to override the `exit()` method so you unset the variable when the current program is terminated by the user.

these are all concepts. You need to work in the fine details of the implementation. There could be better ways to do it. You should explore StackOverflow for alternative generic solutions.

Kf

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [May 12, 2019, 10:57pm UTC](https://discourse.processing.org/t/is-there-a-way-to-get-only-one-program-java-mode-program/11096/3 "2019-05-12T22:57:01Z")

</div>

@kfrajer

thank you for your reply.

Let’s improve by the above method.  
thank you.

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [May 12, 2019, 11:22pm UTC](https://discourse.processing.org/t/is-there-a-way-to-get-only-one-program-java-mode-program/11096/4 "2019-05-12T23:22:27Z")

</div>

![tasklist](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a71b58ee185340d59f78ad2b80787dfc5e4debe6.jpeg)

@kfrajer

That’s a good way.  
Thank you.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [May 13, 2019, 1:32pm UTC](https://discourse.processing.org/t/is-there-a-way-to-get-only-one-program-java-mode-program/11096/5 "2019-05-13T13:32:42Z")

</div>

This also depends exactly what trying to open the program again is meant to do? Quite a few programs will open a local server socket, which can only be done by the first. Subsequent attempts will connect and pass data to that first instance. This is how PDE itself handles this I think - see [https://github.com/processing/processing/blob/master/app/src/processing/app/SingleInstance.java](https://github.com/processing/processing/blob/master/app/src/processing/app/SingleInstance.java)

---

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [May 14, 2019, 3:42am UTC](https://discourse.processing.org/t/is-there-a-way-to-get-only-one-program-java-mode-program/11096/6 "2019-05-14T03:42:41Z")

</div>

Okay, I see. Thank you.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 14, 2019, 10:30am UTC](https://discourse.processing.org/t/is-there-a-way-to-get-only-one-program-java-mode-program/11096/7 "2019-05-14T10:30:37Z")

</div>

> [@kfrajer](#):
>
> override the `exit()` method

How is this done please?

Alternatively you can just write yes or no in a file amirunning.txt using saveStrings and loadStrings. When finding yes in setup it exits

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [May 14, 2019, 3:54pm UTC](https://discourse.processing.org/t/is-there-a-way-to-get-only-one-program-java-mode-program/11096/8 "2019-05-14T15:54:37Z")

</div>

@Chrisir These posts [[1](https://forum.processing.org/two/discussion/13467/exit-sketch-but-preserve-parent-process#latest), [2](https://forum.processing.org/two/discussion/comment/31291/#Comment_31291)] are in my records. I also have this snippet. They need to be tested against each renderer separately. For JAVA2D, it works when hiting the x button on the window, Alt+F4 (Windows), pressing ESC or hitting any key. For P2D the Alt+F4 does not work.

Kf

```java
void setup(){
  size(400,600);
  fill(255);
  prepareExitHandler();
}

void draw(){background(0);ellipse(mouseX,mouseY,50,70);}

void keyPressed(){
 if(key==' ') exit(); 
}

private void prepareExitHandler () {

  Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

    public void run () {

      System.out.println("SHUTDOWN HOOK");

      // application exit code here
    }
  }
  ));
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 14, 2019, 4:23pm UTC](https://discourse.processing.org/t/is-there-a-way-to-get-only-one-program-java-mode-program/11096/9 "2019-05-14T16:23:43Z")

</div>

Thanks a lot!  
Really appreciate it.
