Using awt.Robot with Tomcat service - java

I am trying to simulate user input (keyboard) using Robot library from java.awt, and it is working on my development enviroment, but when deploying to Tomcat (installed as a service on a different machine), it wont work at all. My OS is Windows.
Is there any configuration that I am missing? I already tried "let tomcat interact with the desktop" option, changing users to run the service, changing java used by tomcat (from jre/ to jdk/jre/..), but still I can't make it work.
Also, if it is not possible, is there another way to achieve this?

I finally solved this, not in the best way I guess.
I stopped Tomcat Service, and then started it from CMD as the current user.
Steps to open tomcat from cmd:
Open cmd.exe
Path-to-tomcat\bib\tomcat7.exe
And then opened my browser, went to the application, tried the proccess and the robot worked correctly!
I know this is not the best way as it can led to security issues, but I couldn't make it work using it as a Service because the different user sessiones (Session 0).

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.

How do I get Apache and tomcat docker containers to talk to each other?

I am a total newb at this and I want to learn. I have a webapp running in a docker container currently on Ubuntu 16.04TLS. I can connect to it over the Internet and it works fine. I want to run an Apache frontend in a separate container and enable SSL. I worked on this for 8 hours today and I know I was close. I had to restore the VM in the end so i could put the webapp back online.
I followed this tutorial https://medium.com/#jmarhee/running-multiple-web-applications-on-a-docker-host-with-apache-85f673f02803 and was successful at being able to connect to Apache (I got the default home page with the install) but was unable to get Apache to proxy for Tomcat. I got a 503 error when I enabled the proxy pass through. I am all about reading if you folks are good with pointing me in the right direction. Since I want to learn, I may come back with more questions. I did read another article http://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach but I did not understand how to apply it to my situation.
TIA for any help you can provide me with.

How to run sudo poweroff in java

I am a jsp developer. I need to shut down my slow, unresponsive, laggy, (you get the point) development ubuntu "server" sometimes, when its keyboard stops responding to input. I already have tomcat configured properly, and even when my keyboard stops responding, I can use it as a server (accessing webpages served by tomcat). My idea is to make a jsp page that calls sudo poweroff. But I have no idea how to do that. I have tried
Runtime.getRuntime.exec("sudo poweroff");
but it does not work for me. I have already edited the sudoers file to allow the tomcat user to perform this action without requiring a password. How can I make this happen?
If the user running tomcat has sudo perms (w/o a password), it should be doable - but the call should be something like this:
Runtime.getRuntime().exec(new String[]{"sudo", "poweroff"});
The command and parameters can't all be passed in one big string.
Check out the docs for more info: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[])
PS: This is probably a bad idea. :)

Tomcat as service unable to work with system clipboard?

I made a password manager as java web application (Tapestry to be precise) and idea is as follows, when you click on button next to password label, the matching password should be copied to system clipboard. It works perfectly when I start tomcat the standard way (run startup.bat), but that's not what I need. I need my tomcat to start as Windows service at startup, but in this case, everything in my application works perfectly except coping to clipboard. No error occurs, nor I get anything wrong in the log, text just doesn't copy.
Can someone tell me why is this the case, and what can cause such behavior? Can it be that service doesn't have right to mess with clipboard and if so, can I make it work?
Any help is welcome
When tomcat runs as a service, it does not run was a user linked to the main windows GUI, so all the awt functionalities are disabled. The only way to get some of them back is to add
-Djava.awt.headless=true
to the tomcat startup parameters - see http://support.sas.com/kb/12/599.html
However, this may be not enough as each user has his own console, so tomcat will copy into its own user console - you should run tomcat with the same user as the local logged in user, making the "run as a .bat" the easiest option.

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

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.

Categories