I am very much new to batch file working. I have a batch file which starts a service. Using the same batch file, I have to execute a jar immediately after this service starts. Can anyone help me to solve this?
Asked
Active
Viewed 275 times
2 Answers
0
You can write those statements concurrently in the same .bat
file like this:
echo "First Statement"
net start postgresql-9.3
echo "Second Statement"
java -jar C:\Users\TenaLi\Documents\NetBeansProjects\SketchBills\dist\SketchBills.jar
cmd /k
Jar will execute after the service have been started:
C:\Windows\system32>echo "First Statement"
"First Statement"
C:\Windows\system32>net start postgresql-9.3
The postgresql-9.3 - PostgreSQL Server 9.3 service is starting..
The postgresql-9.3 - PostgreSQL Server 9.3 service was started successfully.
C:\Windows\system32>echo "Second Statement"
"Second Statement"
C:\Windows\system32>java -jar C:\Users\TenaLi\Documents\NetBeansProjects\SketchB
ills\dist\SketchBills.jar

Raman Sahasi
- 30,180
- 9
- 58
- 71
-
Thanks Raman. In my case, when the first service is started, it does not execute the second one. Some messages are displayed on command prompt indicating that the first service is running. – Megha Feb 15 '16 at 09:25
-
Hi Megha, as you can see that I've done exactly what you wanted, i.e., `started a service` and `executed a jar file` immediately after that. It's working fine from above code. However if you are facing issues then my best guess would be that there may be some problem in code. Can you please share your `.bat` code and also the `messages` that are displayed on command prompt. – Raman Sahasi Feb 15 '16 at 09:51
-
Hi Raman, My requirement is to use only one batch file. From the same file, I have to start a service and then execute jar. I redirected output of the service start into a txt file still the jar execution does not work. – Megha Feb 15 '16 at 13:17
-
......."%_JAVACMD%" %ACTIVEMQ_SUNJMX_START% %ACTIVEMQ_DEBUG_OPTS% %ACTIVEMQ_OPTS% %ACTIVEMQ_SSL_OPTS% -Dactivemq.classpath="%ACTIVEMQ_CLASSPATH%" -Dactivemq.home="%ACTIVEMQ_HOME%" -Dactivemq.base="%ACTIVEMQ_BASE%" -Dactivemq.conf="%ACTIVEMQ_CONF%" -Dactivemq.data="%ACTIVEMQ_DATA%" -Djava.io.tmpdir="%ACTIVEMQ_TMP%" -jar "%ACTIVEMQ_HOME%/bin/activemq.jar" %* after this I have to execute a jar – Megha Feb 15 '16 at 15:54
0
edit again: Sorry I misunderstood the question.
For example, preivously the batch file was written like:
start-service-command
jar something
Now try adding a "start cmd /C " in the first line:
start cmd /C "start-service-command"
jar something
referencing: BAT file: Open new cmd window and enter code in there
-
Can I run the service directly and then execute the jar from the same batch file? – Megha Feb 15 '16 at 13:14
-
Second task is dependent on first. First one must be on to start second on. – Megha Feb 15 '16 at 13:22