launching java program via batch and php, - java

php and batch files.
So i'm executing batch files through php
I have no issues launching the batch.
$str = exec('C:\WINDOWS\system32\cmd.exe /c START C:\MInecraft\_restart.bat');
However the issue im having is getting the batch file to work currectly. When the batch is run, it executes this code
taskkill /IM java.exe /F
java -Xmx1024M -jar craftbukkit.jar -o true
batch file successfully runs the taskkill command when launched with php, however it will not run the next line. When ran manually it launches fine, (bear in mind that the cmd.exe does not exit it stays open with this code)
Any ideas on how i can get this to launch from php?

Maybe the reason is the same as given at Error Executing Batch file of BlazeDS, the java executable is not found by Windows on running the batch file from within PHP script.
The current working directory or the directories listed in environment variable PATH can be different when the batch file is executed from within the PHP script in comparison to running the batch file manually.
Do you have ever tried to specify java.exe with full path in double quotes in the batch file?
You could also add at top of the batch file the commands
dir /w
PATH
to see which directory is the current directory and which directories are listed in environment variable PATH on execution of the batch file from within PHP script.

Forgot to post back here, I specified java through windows path command rather than the batch. The problem is because the java process takes the cmd and turns it into a console, it never finishes the batch file. So, the php never returns and continues the code unless the batch is force ably closed on the server.
pclose(popen("start /B C:\MInecraft\_restart.bat &", "r")); die();
I ended up using this command, which i believe makes it launch the batch and not wait for a reply. Just carry on with the php.
Thanks for your kind reply. I did initially try launching java in multiple ways.

Related

Run batch script in windows scheduler with jar

I am running the batch file from windows scheduler , when I execute batch file directly it works, but when added in scheduler , it is not running.
Run.bat contains
java -cp D:\Test_Automation\LocalExecutable\TestAutomation-1.0-fat-tests.jar my.com.testauto.TestRunner
Should I set anything in batch file.
Just to clarify, Scheduled tasks are not visible to the user, if you were expecting something to happen on screen, then that is one of the reasons you might think it did not happen. I am not able to see what your Java program is built to do. If however it is a path issue, there are manual ways and automated ways. here are some examples.
Either put the full path of the java instance into the batch file.
c:\apps\jdk\bin\java.exe -cp D:\Test_Automation\LocalExecutable\TestAutomation-1.0-fat-tests.jar my.com.testauto.TestRunner
or if you are unsure of the location of java, run a loop to find it for you.
for /f "delims=" %%a in ('where /r c:\ java.exe') do set "fnd=%%~a" & goto init
:init
"%fnd" -cp D:\Test_Automation\LocalExecutable\TestAutomation-1.0-fat-tests.jar my.com.testauto.TestRunner

How to run a JAR file with NOHUP from within a service or bash script

I've want to start a JAR file through a service script file or normal bash script. If I try the following in the command line, it is working all great. The JAR file runs in background and also if I end the SSH session:
nohup /opt/java/jdk8_x32/jre/bin/java -jar /home/test/DoOnLANServerAgent_1.0.0-SNAPSHOT.jar &
If I use exactly the same from within a bash script file, the JAR wouldn't launch and the process is never running. This occurs also, if I use a normal service script in /etc/init.d/myservice. The process won't start, I always have to open up the terminal and manually start the Java program.
Is there any solution for my problem?

Windows batch file : How to redirect console logs of a background process to 'NUL'

I have created a batch file which calls a java process in background. All I want is to redirect the logs of the java process to a 'NUL' output so that the background terminal does not contain any output.
Contents of my batch file:
#echo off
START /MIN java -jar ./myapp.jar >null 2>&1
However when I run the batch, I minimized windows terminal opens up which still displays all the logs.
I require to disable this logging, without changing the app.jar
If you just want to ignore any console input and output, execute your Java program on a Windows platform and need to prevent the opening of a console window, then use the javaw executable instead of java.
dotvav's solution is best in your scenario, but for the record the proximate problem with your batch script as posted is that the redirection applies to the START command, not to the java command.
A corrected version would look something like this:
START /MIN cmd /c "java -jar ./myapp.jar >NUL 2>&1"

Running a batch file as a scheduled task does not behave as it should?

I have a jar file, which uses jdbc to connect to a sql db and add a new record. To execute this with an arg I put the cmd line command in a batch file to run it.
Now if I manually click and run the batch file it works fine and I can see the new record in my sql database, but I have created a scheduled task to run the batch file for me once day which it seems do successfully but there are no new records added to the database.
Any ideas on why this happens?
The batch file is just one line, as it seemed to work when I ran it? :
java -Dvar=daily -jar SQL_JDBC_Bandon.jar
I post this as an answer because it would look ugly in a comment.
To have a log of executions to compare to the task execution log of scheduled tasks create a batch similar to this.
#echo off
echo JDBC executed %DATE% %TIME% >> C:\temp\logs\jdbctask.log
C:
cd \PathToYourJar
C:\Programs\Java\bin\java -version >> C:\temp\logs\jdbctask.log
Dir C:\Programs\Java\bin\java.* >> C:\temp\logs\jdbctask.log
C:\Programs\Java\bin\java -Dvar=daily -jar SQL_JDBC_Bandon.jar >> C:\temp\logs\jdbctask.log
This makes sure that you don't rely on PATH or JAVA_HOME variables that might not be set for the user you run the batch in. Please adjust the paths to your needs.
If this still fails, post the results of the log file.
I figured it out a while back, but all I did was put these 2 lines in a .bat file and it works without any issues as a scheduled task:
cd C:\Users\SQLService\Desktop\ScadaCalcs
java -Dvar=daily -jar SQL_JDBC_Bandon.jar
One thing to note is that the jar and script are both in the ScadaCalcs directory, but I had to cd an absolute path for it to work.

Starting a blank console

IS there any way to start a blank console in Windows platform ?
I'm writing a CLI where i want to open a separate window where user can log in and write their own command. When executing with cmd /c start command, it starts windows standard console.
Is there any other command ???
Assuming you're trying to start a java jar file, use a command like this:
start /d "%~dp0" java -jar "%~dp0\fpa.jar"
%~dp0 expands to the drive letter and path in which the batch file is located. Use that if you want to want to make sure that the PWD when running is the same location as the batch file. Otherwise, juse use
start java -jar "%~dp0\fpa.jar"
This will make sure that the batch file works even if you run it when not in the same directory as the jar file, as long as the jar file is in the same directory as the batch file.
You may need to make sure that java is in your path by having a line like
set path=jre6\bin;%PATH%
Also, you can eliminate the command line windows that comes up (for a GUI program by example) by using javaw instead of java.
You can use batch file in windows which would in turn start application that would accept user input. Something similar to this :
start cmd /c myapp.bat

Categories

Resources