wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Run a single instance of application at all the times Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Run a single instance of application at all the times" Watch "Run a single instance of application at all the times" New topic
Author

Run a single instance of application at all the times

Vijay Vaddem
Ranch Hand

Joined: Feb 13, 2004
Posts: 243
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
gayathri hariharan
Greenhorn

Joined: Jan 11, 2005
Posts: 27
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.


thanks,<br />Gayathri
Vijay Vaddem
Ranch Hand

Joined: Feb 13, 2004
Posts: 243
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

Joined: Feb 13, 2004
Posts: 243
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

Joined: Jan 11, 2005
Posts: 27
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.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Jared Cope
Ranch Hand

Joined: Aug 18, 2004
Posts: 243
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.


SCJP 1.4 91%, SCJP 1.5 88%, SCJD B&S
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
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]


blog - InfoQ.com
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
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

Joined: Feb 13, 2004
Posts: 243
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

Joined: Jul 12, 2004
Posts: 995
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

Joined: Feb 13, 2004
Posts: 243
OK....let me try with sockets...... BRB
Jared Cope
Ranch Hand

Joined: Aug 18, 2004
Posts: 243
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

Joined: Jul 12, 2004
Posts: 995
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

Joined: Nov 05, 2004
Posts: 502
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

Joined: Aug 18, 2004
Posts: 243
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.
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
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


The nice thing about Standards is that there are so many to choose from!
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Vijay Vaddem
Ranch Hand

Joined: Feb 13, 2004
Posts: 243
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

Joined: Jul 12, 2004
Posts: 995
Told you that was enough!

--
./pope
[the_mindstorm]
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Run a single instance of application at all the times
 
Similar Threads
Can someone please help
Problem in redirecting to the index screen
formatting text and password fields
Runtime
JDialog modality issue on Win7 taskbar click