Running jar and database through batch file (Windows) - java

I have made shortcuts within my file directory to run the startNetworkServer and my jar file. These are two serparate shortcuts that I then run within a single batch file. My batch file starts the network server, then pings 1.1.1.1 with a wait of 5 seconds before then running the jar file, hence opening my java GUI.
My issue is, I wish to stop the network server again upon closing my java GUI program. To do this I know I can run the stopNetworkServer command, however, placing this in the batch file after the starting of my jar file results in the network server stopping before or whilst the jar file is running. I want it to stop upon exiting my java program.
Is there anyway I can check to see if the jar file is open? Or tell it to wait until the jar file is closed? Or even return something in my java code to kick start this?
J

Fixed, just had to add a /W prefix before running my Jar file link. This means that the next process will not start until that one finishes.
The final batch file (.bat) looks like this:
START startNetworkServer.lnk
PING 1.1.1.1 -n 1 -w 5000 >NUL
START /W Run.exe.lnk
START stopNetworkServer.lnk
J

Related

Can not launch agent in windows 10 when running agent.jar via batch command

I am trying to run one of my Jenkins agent which is based on windows 10 via running agent.jar using batch script.
What I did :
My batch file is in the same folder with agent.jar folder
I created the shortcut for the my batch file and placed it inside Microsoft/Windows/Startup folder so that every time computer reboots the agent.jar file execute via batch file
Upon running the file i am getting following warning from jenkins in my cmd and agent also not going online
WARNING: Are you running slave agent from an interactive console?
If so, you are probably using it incorrectly.
See http://wiki.jenkins-ci.org/display/JENKINS/Launching+slave.jar+from+from+console
<===[JENKINS REMOTING CAPACITY]===>rO0ABXNyABpodWRzb24ucmVtb3RpbmcuQ2FwYWJpbGl0eQAAAAAAAAABAgABSgAEbWFza3hwAAAAAAAAAB4=??
My batch file contains:
#ECHO ON
start java -jar C:\Jenkins\agent.jar
echo "The program has been completed"
Why I am getting this warning , because it is not working every time i have to run agent.jar file via cmd manually when i my agent machine restart , Is there trick the my agent runs automatically after restarts so I can get rid of this manual exercise

Prunsrv exe killed via task manager but child process remains

I have a Java application run as a Windows service using procrun (specifically prunsrv). The service is defined as an exe StartMode so a batch file (run-my-app.bat) is run as the StartImage. Why I am not using jvm or java mode is a different story, not related to this issue (I was unable to run spring boot application with procrun, all examples did not work so I resorted to creating a batch file and calling java -jar my.jar). prunsrv.exe is actually renamed according to the application, say myapp.exe. The problem is that if myapp.exe is killed via the task manager, the java process remains! The batch file run-my-app.bat runs the application using the following line:
start "%APP_NAME%" /b "%JAVA_EXE%" -jar myapp.jar --spring.config.location=application.properties --logging.config=log4j2.xml
The batch file completes and the started Java process remains - I know this because if I print a message after the above "start" command I see the message in the log.
Is there any way to stop the java process when the prunsrv.exe (renamed myapp.exe) is killed?
Child processes will only be closed if they were created as Job objects.
IMHO it's not possible to tell prunsrv.exe to start processes as jobs, so the answer to your question is No.
You can of course terminate every single process individually. There are attempts to kill process trees, but be aware that Windows does not maintain parent-child-relationships. That means: If in a chain of 3 processes the middle one dies, the tree is not available any more.

How to call a batch file from a windows service?

we have a java application, which is running as windows service. Actually we need to call a batch file to start windows 'calculator' application. But we can't. If we start java application not as a windows service, it is working fine. I have read a post about the same on Calling a batch file from a windows service and done the below configuration.
"first install the service with giving appropriate path to the batch file or exe file then go to run->services.msc->right click on the service ->properties->logon->check enable service to interact with desktop-make it enable "
After that we got a partial success, as it first prompts a permission window as below:
May I suggest that instead of opening a batch file, you just open the calc.exe file instead?
Runtime.getRuntime().exec("c:\\windows\\System32\\calc.exe", null, new File("c:\\windows\\System32\\calc.exe"));
EDIT: If you still want to run it as a batch file, use this:
Runtime.getRuntime().exec("cmd /c start nameOfTheBatchFile.bat");
Please note that the second variable in the exec method is to set up the directory that you want to be calling the program from.

Restart tomcat through a java webapp running on it

I looked at this post,and they said to use a batch script to restart a tomcat server using a webapp. The issue is that if you use Runtime.exec("batch.bat"), once you do the kill for the tomcat server, the server dies and the runtime dies.
I did a quick test of this by adding some prints to my batch file to be sure:
echo "lol" > file1.txt
net stop tomcatservice
echo "lol2" > file2.txt
net start tomcatservice
echo "lol3" > file3.txt
Only file1 was created as suspected.
Is there a way to do what I want? Is there a way for me to restart the tomcat server on a schedule through code in my webapp?
Use Runtime.exec("cmd /c start batch.bat") it starts a new process so it wont be terminated when the server stops.
Create a batch file and write shutdown.bat followed by startup.bat. Call that batch file from your java code using Runtime. It should do the trick for you.

windows service from batch file failed to execute

i need to execute a batch file as windows service.
For that i had created a batch file.
In this batch file i just add the below code to run a jar file.
java -jar myTest.jar
When i double click on the batch file..no problem .its working fine. It executes the jar file (a java application).
But the same batch file when i used in a windows service on a windows server, its not working.? Its just getting blinked to show the command window and gets closed. None of my code portion inside the jar file gets executed.
Another thing is i had successfully checked this from another windows server. Its working fine there.
Why this strange issue..??Can anyone help me out to solve the issue..
The service is not executed in the same environment as when you run the batch from an interactive Windows session. Make shure in the .bat file that change into the correct (working) directory, even with absolut path (cd \users\my\java\service), and maybe specify the full path to the java.exe. The other server do you mention could have a totally diferent setup of the environment, installed software, etc.
C:
cd \users\my\java\service
"\program files\java\jre\bin\java" -jar test.jar

Categories