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(); }
});
Related
My problem description:
I have a server with OS version Windows 2012R2, I have two process running on it.
I have a .bat file to start them.
Below is the detail of .bat file:
e:
cd E:\autostart
echo ========================= 1.RUN PCG =========================
start startWebworksServer_PCG
echo ========================= 2.RUN PHONE =========================
start startWebworksServer_PHONE
Now I create a .jar file and make the jar as a windows service. just like this :
This service will start these 2 processes in the background, I cannot see the windows of these 2 processes.
I want to find a way to have this service start these 2 processes in the foreground, so that I can see these 2 windows after logging in to the server. It is best to use java language.
PS:I tried to use JNA because I was first exposed to JNA and I failed. I tried to use Advapi32.INSTANCE.CreateProcessWithLogonW but it still started the process in the background and I still can't see the process window.
Services usually does not show any window, but as far as I remember, you should have a checkbox "allow this service to interact with the desktop" in the service properties. This of course would work only if there is a user session.
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 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
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.
How to detect if java is run from cron or through interactive shell.
I need to determine if the script is called by a user (show error message in sys.out) or cron(send error as email)
In your your crontab job you can pass an additional command line switch like -Dcron=true and that you can check inside your java code to branch out your logic.
Normally the stdout and stderr of any job run by cron is sent to the owner of the cronjob in email already.
If you want to handle that explicitly just add a command line option such as --cron that is passed to the cron job. Or more generally --email=bob#whatever which allows any invocation to send its output via email.
I would support -D solution as a most portable.
Still, if you need to implement a no-configuration solution, and you're OK to limit yourself to specific flavour of Unix (e.g. Linux), you may walk process tree to find if any of your parents are cron process:
Under Linux, read /proc/self/status:
cat /proc/self/status
Name: bash
State: S (sleeping)
Tgid: 9872
Pid: 9872
PPid: 9870
...
Then, according to PPid entry above, read /proc/9870/status, ... and continue until you reach PPid 1, which is init. If cron nowhere to be found, you're not under cron.
I also pretty sure somewhere under /proc/self you can find a direct specification what console/tty the process is running under (if none, than it must be a daemon, started by cron or not), but I couldn't find it right away.