Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Running only one instance of an application.

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a simple application called "Test.java" which looks like this
public class Test {
public static void main(String[] args) {
while(true) {
Thread.sleep(60000);
System.out.println("Alive "+this);
}
}
When I run this program through my IDE (netbeans), i think the java runtime spawns of a process corresponding to this application (which shows up in my task manager as a java process). How can I make it so that when I try to run this program again, I kill the original process and start the new one, i.e., when I press (say F5 for run..) the original Java process is killed
and a new one corresponding is created. So at any given time I can have only one java process corresponding to that application
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would a "singleton" class help here? i.e. a class that has just one instance of itself.
 
Alok Pota
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I afraid not. I just realized that running different instances of a public static main() class actually show up as different java processes in your task manager. I am not sure if there is a good way to limit the number of such java processes programmtically from within java, as there are no hooks provided to deal with the underlying OS.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic