How to run external application from a Java EE web application? - java

I have been working on a Web Application for Intranet use only. I work with Apache 7 and Windows Server 2003.
In one of my page, I need to open an external application that we can locate in C:/Program Files/etc... with some parameters already initialized. Of course, the user has to use the web application on the server to make it work (that will be the case).
To open the application, I use the ProcessBuilder object.
The problem:
When I work locally with Eclipse and run the server by hand, it works perfectly. Any application can open itself from a web page.
But when I use the tomcat windows service (and that's what we want to use on the server), it just never launches. Or to be more specific, it launches and stops the application directly. No java errors thrown and process.waitFor() with an exit value of 0. The fact is that we can run processes via the web application (I tried to run simple batch files), but when there is an UI involved, it will never appear.
Again, on the server this time, if I launch tomcat7.exe (that we can find in the %CATALINA_HOME%/bin directory) with a double click or cmd, the UI in the web application will appear. If I launch it with services.msc or tomcat7w.exe or tomcat7 start via cmd, it will not.
I thought of several things:
use another user to start the service
change the way the service is launched (StartMode: jvm, java. I did not succeed with exe)
I read Tomcat 7 Windows Service How-To many times but didn't find out anything to solve my issue.
Have you any idea of what is happening, and how to solve this issue ?
What is the big difference between running tomcat as a service and from the command line?

Option 1
If you open your service's properties window, go to the Log On tab then check the "Allow service to interact with desktop" check box you will get the behavior you want. Also depending on what app you what to run you may need to change the log on account.
see Launching GUI App from Windows Service - Window Does Not Appear
Option 2
Did you try start a cmd and there use
start /c "c:\path to\exe"
in cmd.exe type
help start
Option 3
You will need a daemon service that is not run as a service. windows puts certain restrictions on service apps.
This sleeping app can be started by tomcat or other your self. it can listen on a port or poll a folder for a new file, and when it gets a job to do it starts the app you want. Via port or text file you can send the parameters.

Related

.bat file not running when executed by app as windows service

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.

Windows could not start service, Error 1053: The service did not respond to the start or control request in a timely fashion

I have created a Java program then I need to install it as windows service.
First I run the jar directly from console and it ran as I wish, then I compile the jar to exe and I ran it as Admin ran OK.
But when I run it from windows service I got following error,
Error 1053: The service did not respond to the start or control request in a timely fashion
I also tried to set Account and password on Log On Tab, but not resolved yet.
It's not possible to resolve the actual problem with the information given by you, so I will provide you the steps necessary to debug the problem.
Use logging in your application to see what causes the error: Apache Log4j
You can use the Windows Event Viewer to see what happens when you start your Windows Service. This is very useful if the problem happens before the start of the application itself.
Click on Start / Windows symbol, type Event Viewer and press Enter. In the left hand tree-menu, click Windows Logs and then Application.

Programmatically restart a Java application that can be run as a windows service

I'm trying to get my Java application to be able to restart itself programmatically.
I already have a working solution for when it's executed as a standalone application - the application invokes java.exe with its own classpath, jvm parameters, class name, and command line arguments; and then it exits (see my stack overflow answer).
But this approach doesn't work cleanly when the application is run as a windows service - instead of a restarted service, I end up with a java.exe process executed by the SYSTEM account, and the state of the service is stopped (since it exited). So the application does restart, but is no longer a service.
I know that windows restarts services that exit with a non-zero exit code but a) I'm not sure that it always works (see this question) b) I'm not sure how to identify the fact that the application is running as a windows service in the first place - System.getProperty("user.name") just seemed to return the hostname followed by a dollar sign ($) in my tests.
Can anyone suggest a solution?
The Launch4j package has a <restartOnCrash> parameter for services.

Runtime.exec not showing GUI in JBoss environment

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.

jenkins job not triggering commands and performing ui validations in the targeted machine

I configured a job for doing GUIAutomation(which got developed using java code including running commands and keyboard events) in the targeted machine.
Extra Info:-
I am running a command to launch the installer(installer GUI should come).....but its not happening
When I got any error I am capturing a image using JAVA DefaultToolkit API, but very time its coming as Black, nothing there to watch.
GUIAutomation tool was developed by myself.
Please help me to overcome this problem, since I have to automate it through jenkins.
Usually, problems running GUI programs in Jenkins occur because of the Jenkins service not having privileges to access the display of the slave (or master, you don't mention which) that is running the job. A typical solution is to start the slave via JNLP, which requires logging on to the slave, navigating to the slave's page on the jenkins master (via browser on the slave) and launching the slave process via the button on that page. You can also launch using the javaws command which will be shown on the same page.
On Windows slaves running as a service, you may have success by configuring the service to run as a user with login and desktop privileges for the slave - the default is the Local System user which has no such rights.
I have done the same job as you.
You have to create the jnlp connection. I create one batch file in slave machine such as: jar -jar slave.jar -jnlpurl (your url)/slave-agent.jnlp
If it's remote slave, it's better to use VNC instead of remote desktop when you operate that machine. Because when you close the session, remote desktop will hold the session, so the screenshot would be black.

Categories