I wanted to know is there any way I could control the name the process my jar is starting, i.e,
I created a .jar file in java and whenever I am clicking on it, it is causing a process named javaw.exe and I want to control this name.
I want to do so because when I click on my jar file then if it already running it should stop and a new one should start, i.e., I want to run a new thread (process) everytime I click on it by stopping the previous one.
If I kill the process named javaw.exe, all processes with name javaw.exe would die (if I am running more than one program) . So, I need to change its name.
Plz help.
Thanks !
There's nothing wrong with javaw running. From the documentation:
The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.
Ultimately, javaw runs your program without a console window. Changing that name could lead to some serious issues later, so you'd want to keep that particular program name.
Why reinvent the wheel? There are already standard ways to prevent two copies of the same program from running. Typically it involves creating a "flag" file, since filesystems guarantee that directory updates are atomic. On UNIX-like systems, the file would be /var/run/program-name.pid. If it already exists, then the second copy will exit with an error.
You could setup a controller process, that manages your instances:
First you try to connect to the controller on localhost via tcp/ip at a specific port (your programs "name" from now) and if sucessful, you send a message like 'new instance started' to that controller. If the connection was not sucessful, you start a new controller-thread in the current vm (and send the message again to this thread)
the controller loops, waiting for messages and if one matches 'new instance started', it does what you describe.
such a controller can be built easily using a simple ServerSocket, a small HTTP-server or many other messaging libs.
Related
I'm using a third-party software (MFT) in my program, which is used for updating and receiving information from Network Cards.
In order to get information, I need to use the command mst status which requires Admin privileges.
So far, I've been using ProcessBuilder to execute commands directly out of my software. However, when my programm reaches this command, I just get "Only administrators can get the devices list" as answer.
I've tried creating a .bat-File, and then creating a shortcut to it, selecting "Run as Administrator" in its properties. However, with this approach the software returns "" as result.
Through so manual testing with the files themselves, I figured out that the shortcut never returns anything, and if I start the bat File without using the shortcut, I get the old "Only administrators..." again
Is there any other way I could execute this command with Administrator Privileges?
My Java program uses the Windows Registry to store some values. (and yes, this cannot be done differently)
The problem is that I implemented a Drag and Drop Feature, but Windows does not allow drag and drop if a process is elevated, and the drag source is not.
Like dragging a folder from the Windows Explorer to the Program.
Now my Question is, is it possible to partially elevate my program, or use a subprogram that is elevated, and passes the values to the main program somehow?
And yes, I know that the explorer can be elevated as well, but no user has this by default.
Used solution:
Main Application is launched, creates a ServerSocket, then starts the Helper Application, which is wrapped in an .exe, which requests Admin privileges on start.
Meanwhile, the main app waits for the Helper to connect via a Socket, the Helper than sends all read values from the registry via an ObjectOutputStream.
The Helper application stay in while(true) and waits for further instructions from the main app, if the main app is shutdown, it will send an "exit" command to the Helper, the helper will then shutdown.
I made a way to make my program, written in Java, update itself. The final JAR is wrapped in a EXE file, through Launch4j tool.
You need to know this piece of code:
System.getProperty("java.class.path").replaceAll("\\;\\.$", "")
gives me the actual path of the EXE. I tested it and it seems to be always working. This is important for the problem.
Now, basically the program pings a webpage and reads a series of values, which one of them is the latest version of the program. If it's greater, the program notifies the user for the update. So, the program downloads the remote data (updated EXE file) and stores them in the current running EXE file, whose filename is obtained through the method explained above. It works, but here comes the problem.
I could simply launch the downloaded EXE file and System.exit the current one, but I cannot do this, because my program works with smart cards: if two or more programs use the same smart cards, the new one won't work (I don't know why, I even restart the provider each time, but this is another story). So I prevent users from starting multiple istances of the program.
(My customers are not so smart to manually open the program each time they need it, so I needed to override the close button to make it stay in traybar, and wake up everytime it is needed. I even make it starts when Windows boots up).
So I have to close the current instance of the program, and launch again.
How I do this? I write a batch file which will basically look like this:
#echo off
taskkill /f /pid <pid of the exe program>
ping 127.0.0.1 -n 3 (this is a way to wait. I will eventually lower the waiting)
C:\Users\Mark\Desktop\program.exe (string generated by the method above. It should launch the program)
exit
Once written to disk, I execute it through Java:
Runtime.getRuntime().exec("cmd /c start " + batchFile.toString());
"batchFile" variable is a File object.
The problem is that the new downloaded program is not launched. A console window appears, shows the result of "taskkill" and "ping" (I will eventually mute them), but the program does not start. If I launch the batch file manually, it does.
Why? I really don't understand this behaviour.
Do you have any advice?
Thanks in advance!
TL;DR version:
The batch file executed by my Java program does not start the exe file written in it. Why?
I have the feeling you are trying to overwrite an executable file (EXE) that is currently running. AFAIK Windows locks such files and thus your updates should never happen.
To resolve your problem: I would split your application in two.
One part ensures the other part has the latest version, then executes that latest version.
For Java, something like this has been developed many years ago as WebStart technology, was marked as deprecated for Java 9 and removed thereafter. Meanwhile there is the project https://openwebstart.com/ that you might want to check out.
I am trying to access the command window contents using the code :
cmdWinDoc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance;
This works perfectly in MATLAB environment but when I deploy the app as a standalone application through the compiler my GUI shows no contents of the command window.
What files or lines needs to be included so that I can get the command window handle or its property active even in standalone apps ?
Thanks in advance !!
The question, and what you're trying to achieve, don't really make sense.
There is no command window in deployed applications, so attempting to retrieve a handle to it is not going to work.
You mention in a comment that you're trying to do this in order to get the messages generated by the deployed application. By default, when you deploy an application, messages that would have been delivered to the command window are instead displayed at the location from which you launched the application - for example, if you call it from a DOS or UNIX command line, they will display there.
If you're doing something like creating a Windows GUI, and there's nowhere for the messages to display, they will get swallowed up by Windows. In this case the appropriate thing for you to do is to modify your code, replacing the display commands (such as disp, fprintf etc) with commands that display the output within your GUI.
If you need to have behaviour that varies between in-MATLAB and deployed versions, place that code within an if block, using if isdeployed ... else ... end.
Q. Is there a way to have a java program run twice on a mac like on windows?
You do not specify if you want to start a simple .jar or an application-bundle so I will give examples for both. To run multiple instances of an application-bundle on OS X, you can use the following trick; open the Terminal and start the application with this command:
open -n /path/to/your/java.app
Each time you call this command, a new instance is opened up.
Attention: Just because it is possible to start several instances does not mean it is a good idea to do so. Make sure you will not run into trouble with concurrent write-access of multiple instances with the same file.
If you are trying to run a jar, you can simply call
java -jar /path/to/your/java.jar
several times to start up several instances.
To start up the java-application from inside a java-application under OSX, you have to do something like this:
In the case of a simple jar:
File jarFile = new File("/path/to/your/jarFile.jar");
Runtime.getRuntime().exec(new String[] { "java", "-jar", jarFile.getAbsolutePath() });
In the case of an application bundle:
File jarFile = new File("/path/to/your/jarFile.app");
final String[] command = { "open", "-n", jarFile.getAbsolutePath() };
Runtime.getRuntime().exec(command);
I don't really understand the problem. But why don't you abstract it to a method instead of naming it program and call that subroutine twice. Or spawn two threads?
Maybe this is a trick question, but I would open up two terminal windows and run it once on each terminal...
This depends on the nature of your Java program. If your program is running as both server and client, it may cause the problem when your run multiple instances. In many server program, it uses a fixed port number to simplify the setting and implementation. Since a given port number cannot be use by more than one application, you cannot open more than one instance of that application unless you can change the port number in your application settings.
Many Java application uses this trick to prevent user to open multiple instances of their program by checking if a certain port is in use. If this the case, then you cannot run more than one instance of the program.
For other Java application that do not use port or ports do not crash, you can open it twice or more via the terminal.
Assuming that you are running client version of the code on your system and trying to connect to a host. First you need to have the server running on both the machines, B and C in your case. Secondly the client code you are using should be reading the IP address and port to connect. It should not be hard coded or else you will have to change the code and rebuild it for server B. This should help you.
Guess I have answered your query.
:)