I have a Java application installed as Windows Service using Apache Common Daemon (windows server 2008).
I need to run excel.exe command within my application, so I set up my service in order to be capable of interacting with desktop (by checking the box in Logon tab on service's properties).
In this way, when I call start() method of ProcessBuilder the popups shown here appear to me.
Is there a way to avoid this?
My intention is to run my java app as service and run multiple instances of excel.exe in parallel in order to process several .xlsm files simultaneously, but I do not want to interact in any way.
I have already read this article about Session 0 Isolation, so I'm wondering if it is really possible to start many excel.exe (or any other "GUI command") from a Windows service on WinServer 2008?
Thank you.
This is the solution I've found until now (seems working at the moment)
Following zapl's suggestion I've made a .vbs in order to exec my macro directly using the script.
Then I've modified the user running excel to match with the one used to start the service (thanks to this)
After this I've changed my ProcessBuilder from this:
ProcessBuilder pb = new ProcessBuilder("cscript.exe", excelPathArg....)
to this:
ProcessBuilder pb = new ProcessBuilder("cmd", "/C", "cscript.exe", excelPathArg....
Now, even if the user used to start the service is logged off ProcessBuilder seems to be triggered correctly.
I'll provide more details as soon as I'll dig more into it
Related
So, I have this client-server app which is written in 4th Dimension Language, that runs as a service so it restarts automatically if something happens to the server. This language has a built in function which allows you to run cmd commands and I have another java app in the same folder, that is in charge of sending emails, something my server side app cant handle. We use a command for running this jar from cmd and send the parameters from there, creating and xml for it to create an html from that and send it via email. The thing is when I run this command with the app running as a service, the command simply does not run, but, if I run the app normally, it works like charm, with no problems whatsoever.
At first I thought it could be the paths, so I got all the paths to be absolute, using the full route, yet it doesnt work still. Also I tried exporting the command as a bat and running it by hand, in the exact same path were the server is, and it works just fine. I thought that maybe the service needs some sort of admin privileges, so I started it as Admin from the service, but it changed nothing.
Is there any chance the service has some sort of limitation which doesnt allow the app to execute external commands? If so, is there anyway to bypass this limitation?
Well, I couldn't make it work when it was running by service, so I made a bat with the command the service uses and pasted it in the Windows Startup Folder, so it "opens manually" when windows starts. It's not the real answer, but is workaround for needing to use the service.
Developing a solution using ProcessBuilder to launch a perl script on Windows and Linux. The script is launched by a REST API. The Perl script does a stop/start on web server(Liberty Websphere). The REST API waits the result of such script. But since the server is shutting down, the rest api is disconnected and the perl script stops.
I´m trying to find way out to start the perl script that does not depend on Webserver process.
The main goals is to update a few resource on the web application by loading a patch file and then execute the perl script over this patch file.
Dissociate the process from the parent (orphaning).
http://enwp.org/Orphan_process
A low-level approach is to fork twice, running the desired process in the grandchild, and immediately terminating the child. The grandchild process is now orphaned, and is not adopted by its grandparent, but rather by init.
See the Proc-Daemon source code for details.
There are many Q & A on Runtime.exec, but still I found a strange behavior. I am running desktop application in which jobs (a kind of javax.resource.spi.work.Work) are submitted and executed in JBoss application server. Following is the code to run some script, which is blocking whenever it needs to launch any GUI. For example, if the script or command is
start notepad
The GUI is not launched, but i can see the notepad.exe in Task Manager. I dont know what is blocking to launch the notepad GUI. Following is the code:
String pathString= "D:\\folder\\abcd.bat";
pro = runtime.exec(new String[] {
pathString
});
Content of abcd.bat file is
start notepad
No error logs or exception found. The above code works very well in normal Java class.
EDIT: Issue found only with .exe files which launch GUI. Some .exe which does not require launching GUI but running some background task is executed very well with my code.
EDIT: Forgot to say that my application is running as service.
Starting from Windows vista, services not allowed interacting with desktop. More details here . It is windows security restriction to not allow services to interact with desktop. I have done changes in regedit and Log On properties of my service, it worked well.
I have an application with GUI. GUI is used for settings management only. Now I need to turn this into Windows service. I thought of splitting my app into 2: service itself and GUI-tool for providing settings. Main problem is that I'm not sure, how this tools should "communicate". Settings are stored mostly in files, and after new settings applied, service should restart.
As for now, I thought of admin-tool sending few requests to service over TCP/IP, that also allows to control service from the network. Problem is that I need to hardcode, or use some text file, to set default port on which service would listen for admin-tool connections after it's installation.
May be there is any alternative solution, which is more suitable here?
You are creating a service. If all you need in the communication layer is being able to stop and start the services, then you don't need to open a port and start listening. The system gives you means to do that with commands that you can run. you are talking about windows, so for example you can run the command "sc start MyServiceName" to start service "MyServiceName". there is also a command called "net" which allows you to start and stop services. These OS commands can be then called from java code in various ways that are available to execute external code.
here is a link that shows how to do that with sc command, check the accepted answer: start-windows-service-from-java
here is another link that shows difference between two commands "sc" and "net": net-start-service-and-sc-start-what-is-the-difference
Note that "sc" supports starting services on remote machines: simplest-way-to-restart-service-on-a-remote-computer
We are facing trouble restarting closing a running *.JAR file started with the "java -cp".
Killing java.exe worked fine.
But... isn't there any smoother way for the application? (It saves data on shut-down)
Also, how can one simulate any key input at the following message "Enter any key to continue..." with a batch file?
Thanks for all help!
The following set of batch scripts can be used to start/stop jars/any program
start-jar.bat
start "win_title" java -jar [jar file] [jar options]
this basically starts your jar(the program) in a window with title set to "win_title".
you could use another batch to kill the window started
stop-jar.bat
TASKKILL /FI "WINDOWTITLE eq win_title
Use the PAUSE [message] to wait for a key press:
PAUSE Hit any key to continue..
As for killing your app, there are JMX ways to do it - but I find an easy way to have a socket listening on a local port and then you can easily send a kill commnad to it and let your program handle the shutdown.
The excellent Java Service Wrapper will let you effortlessly install signal handlers for your Java app.
Have your app create a temp file on startup and periodically check if it still exists. Your batch script can just delete that file to terminate the app.
If you need to do some cleaning up before your process is shutdown, take a look at Shutdown Hooks.
from the Q&A in the link:
Okay, but won't I have to write a lot of code just to register a simple shutdown hook?
No. Simple shutdown hooks can often be written as anonymous inner classes, as in this example:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() { database.close(); }
});