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

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.

Related

Using awt.Robot with Tomcat service

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).

Running a Web Service without opening Eclipse or any IDE

I want to develop a Java based Webservice on my laptop. This webservice will take one input parameter, query my SQL Server database and will fetch information and will return it back.
I know, I do not need a webservice here. But, right now, I am just testing my android application which will call this webservice and will show return data on my device.
So, I have developed a java program which connects my SQL Server Database (which is present on my laptop) and returns a value against the parameter passed. I have made it a webservice by creating endpoints and publishing it from another class.
For reference, something like answer on this thread
So, when I run it from my Eclipse, I can go to a browser and run my webservice, pass parameter and get result.
But, once I close my eclipse, its no more accessible. I am new to this and after studying I am guessing that I will need IIS to host it on my laptop.
I want to ask, whether it is possible to run/publish it locally on my laptop as a background process so that I can test my android app by calling the same?
Future scope -
I am going to deploy this webservice in my company which will connect my database. Both webservice and sql server will be on same machine. I am going to call this webservice remotely over internet from my android device to show the results.
I guess, I will need IIS in future right? Is there any other way to fulfill this requirement? Please provide some ideas.
If, within Eclipse, you can get your web application deployed so that it is accessible via a browser, then you definately can do it without eclipse.
Eclipse uses plugins like Tomcat or Glassfish to run your webserver. These programs are available outside of Eclipse as standalone services. You can install these and run them as background processes at the command line.
Just figure out which one you are using in eclipse (or which one you want to use) and look for a standalone copy on the web.
Here's Tomcat, btw.
I am done with this.
What I did is, I went to Eclipse, selected my Project-> Right Click -> Export -> Under General -> Ant Buildfiles
This created, Build.xml in my Project directory.
I deleted all the class files and recompiled them using ant command.
In my case, target name was build-project which compiles all the java files.
So, I did ant build-project
Note - This was done as Eclipse was using different JDK version.
Now, I called my publish class to publish my webservice with
ant publish
This was half done as this was running in interactive mode.
Then I created a bat file with following command -
"path_for_ant_bin_directory\ant" -buildfile "path_to_build_file\build.xml" publish
This was opening up the command box. So, I created a vb script to run the bat file in background
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c path_to_bat_file\my_bat.bat"
oShell.Run strArgs, 0, false
And, now it nicely runs in background and I can access my webservice.
Hope it helps someone with similar requirement.

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.

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.

Trigger a Java program on startup of some windows service or program

I want my Java program or say some method within my class to be triggered when i start some Windows program(Application) .For example i want my program start executing(Trigger) when i start Realplayer.
Here is a psuedo-code of what you have to do:
Java application (Listener), registered as a Windows Service and runs on system stratup.
Listener application keep listening until some .exe file is opened.
Listener execute code (or call another application)
I think you need to make a jar and then use some sort of Java Service Wrapper.... Still not sure.
What you actually require is to register a service in the Windows services.
You can easily do this with DOS...
#echo off
start path\to\someApplication
start path\to\javaApplication
Enter the above code in notepad and save it as whateverNameYouWant.bat
When you run it, the batch file will launch the application you specified in the code AND the Java (.jar) application you specified in the code.

Categories