Running Powershell script from Java project wrapped as a service - java

I am wrapping two java programs as services using yajsw (yet another java service wrapper) in order to have them run indefinitely on separate servers. I got one of them to work exactly as I expected (the service is installed on a server, behaves exactly as it does when run from cmd and restarts anytime it or the server goes down). However, when I do this with the second java project it does not work. It gets installed as a service and will run, but when it tries to run a powershell script (something the first project does not do) from within java, using ProcessBuilder(), it starts and powershell.exe shows up in task manager, but never closes or produces any of the expected side effects. Both things that work seamlessly when I run the .jar from the command line. Is there something different about the environments that services/processes run in that would explain this?
Update:
Within the script I have the following line:
$outlook = new-object -com outlook.application;
The problem is that $outlook is null after this and the script then fails to parse any emails resulting in no output. What about wrapping the java program as a service would mess up the creation of an outlook application object?

Related

how run java app on background without aid of bash tools etc

Say you have some cli app, which calculates something for very long and outputs it into file. I want to run it as a normal app, say java -jar ..., get control immediately back to shell (let it be bash/... or windows terminal), while the actual java app still runs on background.
In linux I know about & or better nohup java -jar ... &>/dev/null &, I'm not asking about that. I'm asking how to get this 'nohup' behavior, in multiplatform way without using nohup etc.
I never did that, but I guess I could start some launcher jar, run yet another one somehow in detached way (no idea how) and then quit. Is it somehow attainable within single jar?

Call a dockerized python script from another dockerized java application?

I need to create a Java application which sends some input parameters to a python script and sends some output back to my java application.
I cannot run the script in my java code using jython Or similar things as the python scripts are build on demand and I may need to add new scripts every now and then. So this should not impact my java app.
My java application will be running on a container and based on a few condition check it might have to select 1 of the py scripts from suppose 100 scripts and run it. And again the condition later on may change and a different script has to run at that time
I went through many websites and tutorials on the net but did not find anything relevant.
Has someone tried anything similar?

Run a .exe from the a Web-Server Java

I'm wondering if it's possible to run a .exe from A web-server using the domain name as parameter.
It's working fine using a network shared folder
Process x = new ProcessBuilder("http://example.com/MAJ.exe","param1","param2").start();
Absolutely not. The javajavadoc is straight forward:
Constructs a process builder with the specified operating system program and arguments. This constructor does not make a copy of the command list. Subsequent updates to the list will be reflected in the state of the process builder. It is not checked whether command corresponds to a valid operating system command.
That constructor takes a command and arguments to that (as strings). It doesn't take a URL. It is as simple as that. This interface is intended to run a command that exists in the local machine, file system.
Also note the major conceptual flaws here:
what does it mean to run an EXE that lives on a server?
do you want to download it and run it locally?
or should the server invoke it? In what context? Where would the results go?....
So, the real answer is:
either you should provide a service to download that executable to your local machine, to run it locally
or your wrap that executable into some form of service that you can invoke remotely (like any other restful HTTP(S) service)

Launch program from batch file generated by 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.

Launch Java application from another application

What I want to do is to have my main Java application to update another java application using Java Web Start and then run the second application "silently" upon user request.
I know Runtime.exec to call javaws and silently import the second application in the cache. I can do that when the first application runs and then I am sure I have an updated copy of the second application. My problem is how to run the second application without showing the Java Web start "Verifying application" window.
Doing some research I see no way to avoid that if I execute the .jnlp. I am wondering whether I can run the second application calling directly the downloaded jar files by passing Java Web Start.
Any Ideas?
Thanks
You have basically two possibilities here:
you can use the JNLP Api service and use the DownloadService;
or use the URLClassLoader and load the remote class.
I don't know if this what you want to do, may be for you it seems as a trick around, forgive me if so...
Why don't you run it as exe, by using process object?
Process process = new ProcessBuilder("C:\\...Desktop\\MyExe.exe").start();
And you can convert your app to exe easily by using jsmooth

Categories