• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to prevent JavaWebStart from starting application twice ?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friends,
I am developing a JavaWebStart based application. The application need is such that only one instance of the application should run on the client at any point in time.How do I force JavaWebStart to start only one instance of my application ? Is there any configuration of JNLP which fulfills this ?
I need to find the solution ASAP and any help in this regard will be greatly appreciated. Thanks in advance and hoping to find some solution from you.
Regards,
Vinay
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vinay,
a crude solution...
in ur applications startup class (main class) create a lock file , say file with ext .lck and check for it before creating it. if it exists then display dialog - " you can only launch one instance of the application" and then do System.exit(0); you know..
so algo would be like,
if ( lckFileExists() ) {
// display error
// exit
} else {
// create lock file
// launch application by going further..
}
hope this helps..
i am not sure if JWS has anything in there for us to have the functionality u want..
regards
maulin.
 
Vinay Karve
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Maulin Vasavada:
hi Vinay,
a crude solution...
in ur applications startup class (main class) create a lock file , say file with ext .lck and check for it before creating it. if it exists then display dialog - " you can only launch one instance of the application" and then do System.exit(0); you know..
so algo would be like,
if ( lckFileExists() ) {
// display error
// exit
} else {
// create lock file
// launch application by going further..
}
hope this helps..
i am not sure if JWS has anything in there for us to have the functionality u want..
regards
maulin.



hi Maulin,
thanks for the response. what if my application crashes ? the lock file would have been created and hence the user will never be able to restart it. please let me know of any solution.thanks in advance..
regards,
vinay
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
just a thought:
your application would have to "touch" the lock file every ? minutes to change the modification date. if the application gets started it checks whether the lock file exists and how old it is. if it is older than this interval it would know that something went wrong before.
chantal
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
some more. I just thought of vi:
if vi crashes, the next time you open it (with the same file) it will display a message saying that a swap file exists. similar with older netscape versions.
so instead of checking the date of the lock file, you could display some message:
"There seems to be already running an instance of this program. If you are sure this is not the case, than delete the lock file: /path/prog.lock."
of course, your program has to check regularly whether the lock file is still there. and if it was deleted the program has to shut down (or the lock file would make no sense).
chantal
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That seems way to crude. One reason is because your application would have to gain local file access rights when possibly that would be the only reason for having those rights. So you would have to go through the process of signing your JAR file to grant access to the local file system, plus the end user may be weary of allowing that and for-go running your app. That of course would not be the case in a LAN environment because you just tell everyone to trust it.
However, I don't have a better solution. But I am looking into it.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, after reading the JNLP Specs and API there doesn't seem to be a GOOD CLEAN way of preventing more than one instance of the JWS application running at the same time.
So looks like the other option is best for now. The only other thing is if you had to have Database access, you could write info to a table instead of to a local file??
There are probably several similar options depending on what your application is capable of.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
if we look into JNLP API - PersistenceService then we can do what is expected here by creating a lock file...
i have not used this API for myself ever but it should be simple. i 've seen simple examples here
i will try using this myself and let u know if i'm able to try it...i wish i get enough time for doing this...
the problem that was mentioned about JAR signing should be solved if we use this Persistence API thing as JNLP API is created for that purpose
also, it will be a problem that once the lock file is created and if the application crashes then we are stuck but as suggested we can use timestamp comparision you know. each time we launch the application we can check,
- if the lock file exists
- if it exists then as Gregg pointed out we can popup that "you seem to have already using the the application. if this is not correct press 'launch' button below" and that 'launch' button would be deleting the lock file via Persitence API and restart the application...
- this way the users are not aware of whats going on and they don't endup deleting files themselves etc...
regards
maulin.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that I found this post too late. But I have a good solution for your problem. I think it will be useful for all depending on this forum.
The solution is simple, if you want only a single instance of any application running on a computer. Then you should create a ServerSocket listening on a specific port, which will ENSURE only a single instance.
Here is the example code which should be run on application startup:
try {
listenerSocket = new ServerSocket(11111, 64, InetAddress.getLocalHost());
Runnable socketListner =
new Runnable() {
public void run() {
try {
while (true) {
Socket clientSocket = null;
BufferedReader in;
clientSocket = listenerSocket.accept();
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
String agenturNr = in.readLine();
if (!mainFrame.isApplicationLocked()) {
CicController.getOrCreateCicController().buildCICPanelFromCTI(agenturNr);
}
getLogger().debug("Reading by server " + agenturNr);
}
} catch (IOException e) {
getLogger().warn("Accept failed: 11111", e);
}
}
};
Thread socketThread = new Thread(mainControl, socketListner);
socketThread.setDaemon(true);
socketThread.start();
} catch (java.net.BindException nbe) {
getLogger().warn("Only one instance of CDB can be run at a time", nbe);
System.exit(1);
} catch (final IOException ioe) {
System.exit(1);
}
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic