LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Executable JAR file (https://www.linuxquestions.org/questions/programming-9/executable-jar-file-723817/)

abhinav.zoso 05-05-2009 05:30 AM

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?

Sergei Steshenko 05-05-2009 06:06 AM

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.

David1357 05-05-2009 07:59 AM

Quote:

Originally Posted by abhinav.zoso (Post 3530613)
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.

Sergei Steshenko 05-05-2009 08:41 AM

Quote:

Originally Posted by David1357 (Post 3530734)
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.

David1357 05-05-2009 02:10 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3530782)
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.

Sergei Steshenko 05-05-2009 02:25 PM

Quote:

Originally Posted by David1357 (Post 3531143)
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.

David1357 05-05-2009 06:55 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3531170)
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 (Post 3531170)
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.

Sergei Steshenko 05-05-2009 11:24 PM

Quote:

Originally Posted by David1357 (Post 3531439)
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.

abhinav.zoso 05-06-2009 05:07 AM

Quote:

Originally Posted by David1357 (Post 3531439)
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.

Sergei Steshenko 05-06-2009 05:26 AM

Quote:

Originally Posted by abhinav.zoso (Post 3531918)
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.

David1357 05-06-2009 07:24 AM

Quote:

Originally Posted by Sergei Steshenko (Post 3531931)
Then put the mechanism into your app.

You are being persistently unhelpful:

Quote:

Originally Posted by abhinav.zoso (Post 3531918)
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.

amysaraantony 05-06-2009 11:38 AM

I believe the singleton pattern is your friend.
:)

Debian

abhinav.zoso 05-07-2009 01:30 AM

Ya even one of my friends told me about singleton. I gotta look into it. Any other alternatives?

Sergei Steshenko 05-07-2009 02:43 AM

Quote:

Originally Posted by David1357 (Post 3532012)
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 ?

amysaraantony 05-07-2009 05:38 AM

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.


All times are GMT -5. The time now is 02:52 PM.