Launch program from batch file generated by Java - java

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.

Related

How to move Java runtime console to different terminal window?

I executed a Java program from the command line in terminal app A. I want to move the console to terminal app B without having to exit and re-execute the program.
I can think of a few potential ways to solve this, ranging from:
A) In Java implement a new InputStream and OutputStream that somehow can be wired to a new process started in terminal app B.
...to
B) Find a way to put the main Java process in terminal app A in the "background" so that original process can be reopened in a terminal app B.
Ideally, I want to be able to "log in to" and "log out of" my Java process from any terminal on my computer. Has anything like this already been accomplished, and which approach would be best to make it myself? I am open to solutions that involve Java code, shell scripts, or both.
My specs:
OSX: 10.12.4
Usually running zsh on iTerm
If I was using Linux, the perfect solution would be reptyr, a command line tool that allows you to easily switch terminal windows.
On Mac, the best solution I have found is screen. It can also be used to switch terminal windows but must be invoked before running java in order to work and seems a lot more complex.

Controlling Process Name of my Jar

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.

Java program running in background

I have a simple java program which is just a single piece of code that reads from a database and modifies the contents of the database based on certain conditions. Now, what I want is that this program should start automatically at the startup and silently run in the background unless someone kills it from the task manager.
I have never done something like this before and don't know exactly how to go about it. Can someone help me out as to how this can be done?
Thank you..
Follow the these steps to do the job :(Assuming you are using windows and jre is installed )
First compile your java program and place the class file at one location.
Now create a bat file and place java LOCATION TO THAT CLASS FILE/MyProgram in that.
Put your bat file in start up programs
Restart the system, you will get your program running in back ground..!
Hope this will help you.
There are two problems here
How to add this program to the startup
Windows - Run Java application at Windows startup
Linux - Linux start-up script for java application
Run the program as a daemon (background process)
Simplest way to do is using a while loop and sleep for required time interval in the while loop. Then perform the database
operation.
Also for windows, you can check this JSL
http://www.roeschter.com/
Thanks.
first create you jar bash and then add it to you crontab task list.

checking if windows update or any installer instace is running before starting my installer( java)

want to make a check in my installer before starting installation if any other installation is running beforehand. Like I want to make a check if windows update or any other installer is running i'll not start my installer.
I'm planning to check if any msiexec instance is running before hand. Is there any better approach, and will that be same for checking windows update. FYI my installer is in java
You should know that msiexec.exe will still be running for a couple of minutes after an installation is finished. This is a default behavior in the OS, it keeps the process alive for a couple of minutes, in case the user will start another installation, to save time from starting it all over again. So checking for the process could give you incorrect data.
Also, if you have your installer written in Java can you please explain why do you need to check for msiexec.exe processes?
Since your installer is in Java, I see no reason to check whether other installers are running, moreover there's no robust way to do so.
Does your installer try to replace system files? It should not.
Does your installer try to update a file in use? It must do it gracefully. And ask user to close an offending application; if it's not possible or user does not want to close the application right away, your installer asks user to restart the system when it completed installation.
Too much to care about, without other installers running. That's why it's wiser to use a specialized installer tool.
To check the OS for installations in progress you can use the following registry entry:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\InProgress
Please note that Windows Installer does not allow multiple InstallExecuteSequences to be executed simultaneously, however you can launch multiple installation UIs from different packages. The package enters InstallExecuteSequence usually at the moment you press "Install" and grant all the permissions for starting the system changes (creating registry, copying files, etc...).
Here you can find more information about InstallUISequence and InstallExecuteSequence:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa372404(v=vs.85).aspx
Thank u guys for your suggestions, I final decided to go with creating a windows native dll to check the status of WindowsInstaller. The Windows Installer service is currently running if the value of the dwControlsAccepted member of the returned SERVICE_STATUS_PROCESS structure is SERVICE_ACCEPT_SHUTDOWN. Then used JNI to to call it from my java class.

DOS batch file to enter commands in proprietary java app and receive feedback?

I'm working on a project in which I'd like to be able to turn lights on and off in the Duke Smart Home via a high frequency chirp. The lighting system is called Clipsal Square-D and the program that gives a user access to the lighting controls is called CGate. I was planning on doing some signal processing in Matlab, then create a batch file from Matlab to interact with Cgate. Cgate is a proprietary Java app that, if run from a DOS command line, opens up another window that physically looks like the DOS command prompt. I have a batch file that can check to see if Cgate is running and if not, open it.
But what I can't figure out how to do is actually run commands in the Cgate program from the batch file and likewise, take the response from Cgate. An example of such a command is "noop," which should return "200 OK."
Any help would be much appreciated! Thank you very much in advance :)
(here's my existing batch file by the way)
#ECHO off
goto checkIfOpen
:checkIfOpen
REM pv finds all open processes and puts it in result.txt
%SystemRoot%\pv\pv.exe
%SystemRoot%\pv\pv.exe > result.txt
REM if result has the word notepad in it then notepad is running
REM if not then it opens notepad
FIND "notepad.exe" result.txt
IF ERRORLEVEL 1 START %SystemRoot%\system32\Clipsal\C-Gate2\cgate.exe
goto end
:end
I don't know how to do this on Windows, but on UNIX, there is a program called expect that is designed for such a task. If you install Cygwin, you should be able to use the expect utility on Windows.
You're calling start cgate.exe, which will cause cgate.exe to be launched in a new window. First off, you probably want to run cgate in the same window, which means you should drop the start.
Secondly, you can use shell redirection to pass commands to the STDIN of cgate from a text file, like so:
cgate.exe < commands.txt
This will probably work, but it might not, depending on how cgate.exe is actually expected to receive its data.
If you want to have two-way communication, where you send in data, get the response, then send in more data depending on what the response was, you'll have to use something other than a batch file. Most scripting languages (perl, python) could be used for this purpose, or C or anything else.

Categories