File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JNLP and Web Start and the fly likes How to prevent JavaWebStart from starting application twice ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JNLP and Web Start
Reply Bookmark "How to prevent JavaWebStart from starting application twice ?" Watch "How to prevent JavaWebStart from starting application twice ?" New topic
Author

How to prevent JavaWebStart from starting application twice ?

Vinay Karve
Greenhorn

Joined: Oct 03, 2001
Posts: 3
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
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1863
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.


1. Have fun @ http://faq.javaranch.com/java/JavaRaq
2. Looking for simple infix2postfix conversion and postfix evaluation package? Click here
Vinay Karve
Greenhorn

Joined: Oct 03, 2001
Posts: 3
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
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
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

Joined: Sep 28, 2000
Posts: 508
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
Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Posts: 15040

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.


My Blog | DZone Articles
Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Posts: 15040

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

Joined: Nov 04, 2001
Posts: 1863
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.
praveen kumar gudapati
Greenhorn

Joined: Sep 26, 2003
Posts: 15
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);
}
 
 
subject: How to prevent JavaWebStart from starting application twice ?
 
Threads others viewed
Simple to solve
quick summary
Single Thread Model
sub
problem with setting PreferencesFactory
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com