How to stop a process which was started with task scheduler - java

I have a .bat file which has command to run a Java console program.
I created windows scheduler task to invoke the bat file.
When I run the windows task then it invokes the java program, but when I stop the windows task (Right click the task, Select option End) then my java program do not stop.
My question is how to stop an external program which got started from the bat file. Is there way to capture the End event/signal from task which can be passed to bat file or java program to stop the program.
I got similar question here https://superuser.com/questions/1054534/stopping-a-scheduled-task-with-a-batch-file-action-doesnt-stop-the-program-runn
but it is unanswered.
Atul

Related

Executing java application through a bat file stops

I'm executing a java application through a .bat file and it's executing as well. But sometimes the console stops to show the logs (The process is stopped) and pressing enter in the console the process returns its execution. The program doesn't need user interaction and the problem occurs randomly.
I can't put prints here because the network is closed.
I've tried to set thread priorities to MAX_PRIORITY and no success.
The application is running on Windows 10 x64, and JRE 1.8.
The script into .bat is:
java -Dfile.encoding=UTF8 -jar myapplicationname.jar

Batch file in Windows cmd does not know when java program has terminated

I am using a Windows executable to do some optimisation tasks. This executable is run from a Windows Command Prompt (cmd). It reads a batch file that tells it how to run a model interface (it will need to run it multiple times) which reads and writes text files for input/output between it and the optimisation .exe program. This model interface is written in java and has been successfully compiled. My batch file looks like this:
java -cp .;"C:\Program Files\location_of_required_jars\*" name_of_class_file
The java program executes and completes without issue.
My issue is that the executable I am running needs to know when java has terminated. At the moment it hangs after 1 run of the model interface java program, waiting for the java ... line to complete.
Can anyone provide advice on how to get java to inform cmd that it has completed?
If the batch file does not progress further after running first java command that means that java process has not actually finished. You would need to diagnose the problem, I would suggest to first make sure that it indeed does not finish by adding some echo statement after java invocation. Next you would need to run jps command, which is part of JDK. jps will report PIDs of running java processes and then you do jstack <pid> to find out what is preventing your java program from terminating. It can be that main thread did not actually complete or some non daemon threads are still running.

Why the java process is not closing when the app is finished?

I write a java program which is run in the background. And it works fine,it does what is waited from it.
I write a bat file in windows to run it.
#echo off
start .\jre7\bin\java.exe -jar ".\my_jar.jar"
exit
When I run this .bat file I can see it on task manager and It works and when it finished , the java.exe process is closed on the task manager.
It works fin on Windows
But When I run it on linux in .sh file,
It the java program works fine because It does what I wait from it ,but on the Sytem Monitor
the java process is not closing. I want it to close by itself like Windows.
in sh file:
export JAVA_HOME="/app/myfolder/java/jre1.7.0_51"
export I_HOME="/app/myfolder/code"
cd $I_HOME
$JAVA_HOME/bin/java -jar my_jar.jar
RStat=$?
What is the problem?
You can also terminate the application with:
System.exit(0);
Its definitely not normal for it to terminate gracefully on windows and not on linux.
Your jar could just be hanging on linux due to differences in whatever native libraries you're using, have you tried calling System.exit()?
Have you set your thread to a daemon?
public final void setDaemon(boolean on)
From the JavaDoc:
Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads

Cruise Control batch file build never completes

I am using cruise control to trigger a batch deploy script and currently the file executes perfectly BUT cruise control does not see the script end. It just keeps spinning (building) and this goes on forever.
My script launches downloads the build extracts it and then starts tomcat with the application deployed. The script also ends with:
exit /B 0
and yet cruise control does not see the script exiting...
Anyone has an idea
To close the loop on this matter, the tags in the cruise control schedule are dumb when it comes to batch files. If the batch file starts application that are in separate windows the build process will not stop until the windows of those programs are stopped. The script I was using was launching tomcat in its own windows as it was a deployment script. Fortunately if you launch the windows from a vbs file instead of a batch the new window is not considered as a child process which is what we want for cruise control to finish its build.

closing a command line window in Java

I have created a small Java application in which there is some code that executes a batch file. The execution of the batch file leads to the command line window to be opened and to display some output messages. I would like to know if there is some way in Java to call this command line window to be closed from within the program...Thanks!
the command window should close automatically when the batch file completes.
and to run a batch file in background/invisibly, check other questions
Start java by using javaw or javaw.exe.
java (java.exe) runs with an associated console window,
javaw (javaw.exe) is the same but without the console window.
see the documentation for the java command.
On Windows also use start to invoke another shell
start javaw ClassName
I'm not sure for Linux&Co. Try using an & after the command to run it in the background
javaw ClassName &
The other way, closing the window from a batch started by Java:
I don't believe that is possible directly from within Java. You can only close the batch file by itself.
Is hard to help without knowing what that batch file is doing. You may try using the start command on windows or the & in Linux to run the process in the background (start has an option to open the window minimized [/MIN] or in the background [/B]).
Or try some hack like using AutoHotKey or some system functionality (some WinAPI-DLL or equivalent in other systems).
As an addition to NimChimpsky's answer:
If you run a batch file in Windows, Windows will automatically open a command window for the batch file, in case the batch file wants to print output or prompt for input. This also applies if you launch the bat file from a Java process.
Unfortunately, Windows itself apparently provides no way to launch a batch file without such a window. To avoid the window, you will have to run the batch file via some helper program. There are several available; google for "run bat no window" to find some.
If you just want the window to go away after the batch file terminates: That should happen automatically. If it does not, some program launched by the batch file is still running.
start /b [bat file name]

Categories

Resources