• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Run a single instance of application at all the times

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi and a warm welcome to Matthew Scarpino and Stephen Holder!

Nice to see you guys around here...

My question is about swings...

I have a stand alone application built on swings and jdbc..
Now, when the user runs the main program...it gets instantiated fine
and runs ok.

However, when the user runs the main program again without
closing the existing application, it instantiates another window

i tried to resolve this issue by making the class a singleton, but it
didnt help...

here is the code
-----------------
Main Program




LoginWindow code...


please help me to solve this issue..
TIA

Vijay
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the user runs the main program twice, it would mean they are 2 different instances of VM unaware of each other.So it would always result in multiple instances of program running.
May be you should store in a file/something indicating that the application is already running.
If the user runs the app again, you might check against the file and accordignly decide what needs to be done.
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that looking at the above code it may not look like a swing related issue...

However, I would like to know how to make such a swing application to run single instance at any given point of time.
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I heard eclipse does something similar to this....

But how??

U mean to say that i should create a file and save it on local system
and check it everytime??
 
gayathri hariharan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vijay Vaddem:
I heard eclipse does something similar to this....

But how??

U mean to say that i should create a file and save it on local system
and check it everytime??



No I dont understand the workings of eclipse.

Yeah creating a file (eg lock.txt) if it does'nt already exist and making an entry would be one of the ways of achieving some kind of lock. The app can check for the presence of the flag in the file and decide the future course.
I mean if flag indicates that another instance is already running, you might inform the user and exit else start another instance.
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vijay Vaddem:
I have a stand alone application built on swings and jdbc..
Now, when the user runs the main program...it gets instantiated fine
and runs ok.



Vijay,
if you program is using a database, you can use that to flag the state of the number of instances of your class... Database access would be a bit overhead than using normal text file. However, its advantage is that the security provided by the database would prevent outside disturbance to unprotected normal text file...

Hope it helps...
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've solved this problem in the past by looking at the names of programs currently running on the computer as one of the first startup tasks. If the name already exists, then I quit the application with a message to the effect: "You are already running program X. Only one instance is allowed."

I admit that I have not done this with the Java language, but I would be very surprised if this is not possible.

Cheers, Jared.
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would be surprised to see how it can be done :-). I don't see any viable solution to retrieve the list of programs running.

--
./pope
[the_mindstorm]
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use this logic on startup of your application



Get it??
I dont remember the socket API clearly now, so I'm not completing it. You will have to do the research, but if I remember correctly, it's not more than 10-15 lines of code. You can do this with anything that let's 2 applications communicate, for example:- named pipes. You can use files or database, but files and database wont let the 2nd instance pass the args to the 1st instance. You could serialize the args to the database or the file, but then you have to make sure that if 3rd and 2nd instance start simultaneously, then your file/database is adequately protected
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijay, if you program is using a database, you can use that to flag the state of the number of instances of your class... Database access would be a bit overhead than using normal text file. However, its advantage is that the security provided by the database would prevent outside disturbance to unprotected normal text file...



But im not sure about this........... how to use the database
to flag the state of number of instances of a application??

Also, my initial login screen will not have any DB activity until user enters some login information....
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijay why do you want to complicate your life with db interaction on program entry point? The solutions with sockets or file locking are enough good to solve your problem.

--
./pope
[the_mindstorm]
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK....let me try with sockets...... BRB
 
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In response to my post about just looking for the list of running programs, I did this for a program written in Centura with the help of a windows dll file (user32.dll).

If you wanted to, you could try linking your java program to native code in user32.dll and try running that function. You might need to do some investigating to see what function it is.

I have not tried, but java can run native code. This of course assumes that you are running your java on windows.

If this does not work, I would stay away from using a database to manage the number of instances and go for the lock.txt file option described.

Best of luck, Jared.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jared I was just kidding. However, I wouldn't go for that kind of solution. The file lock mechanism or socket check are just enough.

--
./pope
[the_mindstorm]
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jared Cope:
Hi,

In response to my post about just looking for the list of running programs, I did this for a program written in Centura with the help of a windows dll file (user32.dll).

If you wanted to, you could try linking your java program to native code in user32.dll and try running that function. You might need to do some investigating to see what function it is.

I have not tried, but java can run native code. This of course assumes that you are running your java on windows.

If this does not work, I would stay away from using a database to manage the number of instances and go for the lock.txt file option described.

Best of luck, Jared.





I beleive under Windows you need admin privileges on the local machine to go through the list of processes. I'm not 100% sure though. I had done something like that in C++, and I can hazily remember that there were some restrictions
 
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may be right on the restrictions with listing the running programs. Would need to write a test app.

Also just wanted to mention that the drawback with the lock file solution is that if the user gets clued into how that works, they could just delete that file after startup and then launch the program again.

Most users are not that investigative, but it is something to consider.

Cheers, Jared.
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

A server socket might be the simplest way. You don't actually have to do anything but set it up on a particular port, about two or three lines. The first instance of your program will be able to set up the server socket, but subsequent instances will throw exceptions because that port is no longer available.

The problem with a locking file is that if your program exits without deleting it, your users are forever unable to run your program.

Hope that helps.

Ed
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I've solved this problem in the past by looking at the names of programs currently running on the computer as one of the first startup tasks. If the name already exists, then I quit the application with a message to the effect: "You are already running program X. Only one instance is allowed."

I admit that I have not done this with the Java language, but I would be very surprised if this is not possible.



Although this is an eminently sensible idea, its not possible with Java. The process name for a Java process if typically the name of the JVM process, not the name of the application running in the JVM. So if you could guarentee that your app is the only Java app running in the OS then it would work, but if there are any others then there is ambiguity.
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its working guys!

Just wrote 3 lines of server socket creation and it started
to work like a charm!

Thanks again..

Vijay
[ January 21, 2005: Message edited by: Vijay Vaddem ]
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Told you that was enough!

--
./pope
[the_mindstorm]
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congrats!!!

It's pretty much standard way of doing this. In my Windows programming days, I had a similar solution that uses named pipes. I even had a reusable class somewhere, but I lost it since I switched to Java

Jayesh
 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic