LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2009, 05:30 AM   #1
abhinav.zoso
LQ Newbie
 
Registered: Jan 2009
Posts: 14

Rep: Reputation: 0
Question Executable JAR file


It is possible to run multiple instances of a jar executable. Is it possible that only a single instance of the jar runs? Wrappers like JSmooth or Launch4J can be made to run as a single instance. But since there are no such wrappers for linux, I need a way to restrict the jar to a single instance. How to embed the jar file into an executable such it runs a single instance?
 
Old 05-05-2009, 06:06 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Come on.

Suppose I take any OS (Linux for that matter) and any virtual machine (QEMU, VirtualBox), and start an number of OS instances in the virtual machines - each of the OS instances running only one instance of your 'jar' file.

Suppose there is some limiter for one user - how can it "fight" with another user (I mean user ID - not necessarily another person) - how can the limiter limit another user's activity ?

If you are serious, you need some kind of license server for your application, but even that is easily circumvented by virtual machines - unless your license server phones home, but then probably nobody would like to deal with you in the first place.
 
Old 05-05-2009, 07:59 AM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by abhinav.zoso View Post
How to embed the jar file into an executable such it runs a single instance?
You could have the first instance create a file using "open" with O_CREAT and O_EXCL. You can add a check for the file or just use the error from "open" to cause any other instances to exit when they try to create the file.
 
Old 05-05-2009, 08:41 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by David1357 View Post
You could have the first instance create a file using "open" with O_CREAT and O_EXCL. You can add a check for the file or just use the error from "open" to cause any other instances to exit when they try to create the file.
From http://www.opengroup.org/onlinepubs/...ions/open.html :


Quote:
O_EXCL
If O_CREAT and O_EXCL are set, open() shall fail if the file exists. The check for the existence of the file and the creation of the file if it does not exist shall be atomic with respect to other threads executing open() naming the same filename in the same directory with O_EXCL and O_CREAT set. If O_EXCL and O_CREAT are set, and path names a symbolic link, open() shall fail and set errno to [EEXIST], regardless of the contents of the symbolic link. If O_EXCL is set and O_CREAT is not set, the result is undefined.
.

So, how will O_EXCL prevent from creating a file in a different directory and/or under different user ID ?

The same questions apply to the "heavier" approach with virtual machines.
 
Old 05-05-2009, 02:10 PM   #5
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Sergei Steshenko View Post
So, how will O_EXCL prevent from creating a file in a different directory and/or under different user ID?
Can you show me where the OP says he needs to prevent execution across virtual machines?

In his original post he described the mechanism whereby a Windows program can detect if it is the first instance or not. That mechanism does not work across virtual machines.

You are trying to solve a problem that the OP does not have.
 
Old 05-05-2009, 02:25 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by David1357 View Post
Can you show me where the OP says he needs to prevent execution across virtual machines?

In his original post he described the mechanism whereby a Windows program can detect if it is the first instance or not. That mechanism does not work across virtual machines.

You are trying to solve a problem that the OP does not have.
The OP says he/she wants single instance of a jar file to run. I do not see in which environment and for which purpose the OP wants it.

FWIW, even if there is a wrapper around Java bytecode interpreter, another Java bytecode interpreter (installed in a different place, having different classpath, etc.) may run another instance of the same jar file.

So, we both are making (different) assumptions.
 
Old 05-05-2009, 06:55 PM   #7
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Sergei Steshenko View Post
The OP says he/she wants single instance of a jar file to run. I do not see in which environment and for which purpose the OP wants it.
If you knew what Launch4J and JSmooth were, you would know he was asking to duplicate the mechanism used by Windows. If you knew what the Windows mechanism was, you would understand what his goal was.

I don't think he's trying to implement anything other than prevent multiple instances of the same application (ignore the Java specifics for now) from running.

Thunderbird is an example of an application that does this. Years ago, you could open multiple instances of Thunderbird. But that turns out to be inconvenient. So they modified it so you can only run one instance.

Quote:
Originally Posted by Sergei Steshenko View Post
FWIW, even if there is a wrapper around Java bytecode interpreter, another Java bytecode interpreter (installed in a different place, having different classpath, etc.) may run another instance of the same jar file.
If both instances are trying to create a file at the same location in the file system (e.g. "/var/tmp/a_file_name"), the second instance will exit. It is a commonly used mechanism.
 
Old 05-05-2009, 11:24 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by David1357 View Post
If you knew what Launch4J and JSmooth were, you would know he was asking to duplicate the mechanism used by Windows. If you knew what the Windows mechanism was, you would understand what his goal was.

I don't think he's trying to implement anything other than prevent multiple instances of the same application (ignore the Java specifics for now) from running.

Thunderbird is an example of an application that does this. Years ago, you could open multiple instances of Thunderbird. But that turns out to be inconvenient. So they modified it so you can only run one instance.


If both instances are trying to create a file at the same location in the file system (e.g. "/var/tmp/a_file_name"), the second instance will exit. It is a commonly used mechanism.
Yes, but the question is "what for/why ?". If the application can exist as only one instance for functional reasons (like Email client) - that's one story, and if the application licenser wants yo limit one instance per seat for license fees reason - that's another story.
 
Old 05-06-2009, 05:07 AM   #9
abhinav.zoso
LQ Newbie
 
Registered: Jan 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David1357 View Post
If you knew what Launch4J and JSmooth were, you would know he was asking to duplicate the mechanism used by Windows. If you knew what the Windows mechanism was, you would understand what his goal was.

I don't think he's trying to implement anything other than prevent multiple instances of the same application (ignore the Java specifics for now) from running.

Thunderbird is an example of an application that does this. Years ago, you could open multiple instances of Thunderbird. But that turns out to be inconvenient. So they modified it so you can only run one instance.


If both instances are trying to create a file at the same location in the file system (e.g. "/var/tmp/a_file_name"), the second instance will exit. It is a commonly used mechanism.

Exactly. I need to achieve that. Like Thunderbird which allows only one instance to run. What I am designing is an input method like SCIM. So, I'm logging all the key presses, processing them and then outputting the converted text to the window which has focus. Basically, it's an input method (Hope you people know what an input method is). Since i'm logging key presses, I need to run a single instance of the program or else it will create a conflict.
 
Old 05-06-2009, 05:26 AM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by abhinav.zoso View Post
Exactly. I need to achieve that. Like Thunderbird which allows only one instance to run. What I am designing is an input method like SCIM. So, I'm logging all the key presses, processing them and then outputting the converted text to the window which has focus. Basically, it's an input method (Hope you people know what an input method is). Since i'm logging key presses, I need to run a single instance of the program or else it will create a conflict.
Then put the mechanism into your app.
 
Old 05-06-2009, 07:24 AM   #11
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Sergei Steshenko View Post
Then put the mechanism into your app.
You are being persistently unhelpful:

Quote:
Originally Posted by abhinav.zoso View Post
So, I'm logging all the key presses, processing them and then outputting the converted text to the window which has focus.
He has an application that grabs all input and modifies it before sending it to whatever application has the focus. This kind of thing is used to help people with disabilities use computers. In this case, he tells us exactly what he is using it for: an input method.

He is trying to help people get around the limitations of their keyboards. He is not trying to prevent them from using his software.

Please read the posts until you understand them. If you don't understand them, ask questions until you do. All this unhelpful noise is just wasting bandwidth.
 
Old 05-06-2009, 11:38 AM   #12
amysaraantony
Member
 
Registered: Apr 2009
Posts: 42

Rep: Reputation: 16
I believe the singleton pattern is your friend.


Debian

Last edited by amysaraantony; 05-15-2009 at 08:12 PM.
 
Old 05-07-2009, 01:30 AM   #13
abhinav.zoso
LQ Newbie
 
Registered: Jan 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Ya even one of my friends told me about singleton. I gotta look into it. Any other alternatives?

Last edited by abhinav.zoso; 05-07-2009 at 01:32 AM.
 
Old 05-07-2009, 02:43 AM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by David1357 View Post
You are being persistently unhelpful:



He has an application that grabs all input and modifies it before sending it to whatever application has the focus. This kind of thing is used to help people with disabilities use computers. In this case, he tells us exactly what he is using it for: an input method.

He is trying to help people get around the limitations of their keyboards. He is not trying to prevent them from using his software.

Please read the posts until you understand them. If you don't understand them, ask questions until you do. All this unhelpful noise is just wasting bandwidth.
???

It was you who suggested to use "open" with O_CREAT and O_EXCL, so why can't the OP use your suggestion, creating/checking fixed name file in fixed directory like /tmp ?
 
Old 05-07-2009, 05:38 AM   #15
amysaraantony
Member
 
Registered: Apr 2009
Posts: 42

Rep: Reputation: 16
Alternatively, you can do this:

At the time of starting the application - write an entry into a certain file to true (lets call it X). Ensure at the time of closing the application this entry is set to false.

And before starting execution of teh application - check whether the value in this file is true or false. If it is true - exit the application.

This will prevent multiple instances of the application from working on one machine.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error tryin to install jar Executable khalil_noura Linux - Newbie 1 12-15-2008 02:13 PM
Java: executable jar files on xp mac and linux redmoon zero Programming 9 03-18-2008 01:35 PM
Fedora Core 1 Associating .jar files with java -jar command pymehta Fedora 0 01-13-2005 05:26 AM
Cannot not run executable jar file after set content type for download antony_csf Programming 1 12-08-2004 12:22 AM
executable jar in java (cannot double click) spyghost Programming 3 09-29-2003 09:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration