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
Related
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.
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.
I am scheduling a task (with windows task scheduler) which simply run a batch file.
this batch is running a jar file (Java program) with a simple "Java -jar test.jar".
When i run the script from the command line manually, the java program runs well and no exception is shown.
but when the task schedular does the same (calling the command), the java program ends with an exception: "ClassNotFoundException" for one of the classes which is in my jar.
What way be the cause of this? what is the diffrence when calling the jar from the command line and from the task scheduler?
Thanks.
I reckon that probably the "current directory" is different, and as a consequence java doesn't find the jar at all. In the first line of your .bat, can you do a cd \path\that\you\expect before you execute java?
Does your jar have any dependencies? Also, it'd be helpful to know what's your folder structure, and how exactly do you run it in the command line.
Anyways, depending on your case, you can do something along these lines:
cd /path/to/exec/folder // set current directory
java -cp /all-classpath-jars/and-or-bin-folders/ test.jar your.package.MainClass [args...]
This has to work, if you specify everything you need correctly.
I am trying to run a simple JAVA program once per day on a Windows 7 machine.
My code runs fine inside NetBeans. If I do a clean and build it suggests this:
C:\Program Files\Java\jdk1.7.0/bin/java -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
This does not work from the DOS prompt of course because of the space between program and files so I do this:
C:\Program Files\Java\jdk1.7.0/bin/java -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar" -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
This works from the DOS prompt.
I now create a task in Windows Scheduler to run:
C:\Program Files\Java\jdk1.7.0/bin/java
with arguments:
-jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
When I then run it, all I see is a DOS box flashing up for a second. I expect the code to take about 30 secs to run. The code should persist data to a database and no updates happen.
The code also uses java.util.logging so I should see log entries and I don't.
I strongly suspect that I am not running the JAVA command properly or that there's a bad classpath issue that it present when running via Scheduler that isn't there when running from the DOS prompt.
Help would be appreciated. If you've seen this before and can sort it that would be great. If you can tell me how to get a meaningful error trace from Scheduler than that would also be really helpful.
Thanks!
I Think that you could create a simple batch script that will launch your program in this way :
#echo off
REM Eventually change directory to the program directory
cd C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\
REM run the program
"C:\Program Files\Java\jdk1.7.0\bin\java.exe" -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
Copy it into the notepad and save as java_script.cmd and then schedule this script instead of the program directly.
I solved it after changing all fonts' references to "SansSerif"
I was using Jasper Reports inside Java to create a PDF file. It was working fine when I double click the batch file or Scheduler with Windows Server 2003 but not working with the Scheduler of 2008.
I tried many different things nothing worked so I though Could it be that Windows Server 2008 is blocking the access?.
Now is working perfect. So, if you are having problems check the references to anything you are using.
The scheduler will run under a different user unless you specify what user to run as. If it isn't running as your user then it won't be able to write to your directories.
The real problem to the original question is a java installation issue on Microsoft systems. Java jre installs into Program Files\java. The executable (java.exe) is only installed in that java\bin directory. Running from the command line, the os looks in the proper location for the java.exe. Running from other MS tools (such as VBA Excel or in this case TaskScheduler), it does not!
You can see that TaskScheduler is looking in the wrong place by viewing the tasks history in the TaskScheduler tool. Double click on some of the history events and one will list the action and return code. The action will show that the TaskScheduler is trying to run
"C:\Windows\system32\java.EXE"
So, copy java.exe from the java\bin directory into the place where the scheduler is looking, and now it will work.
Or update your task and provide the full path to java.exe.
You can also update the environment system path to look for java in the java\bin directory, but that has to apply to all users and sometimes this is faulty as well.
The Problem is as follows:
I have a Java Application packed in a ".jar"-File.
I can start the App (Swing Application) by either
doubleclick on the jarfile
commandline java -jar MyApp.jar
However when I put the commandline command into a batchfile, it will tell me
Unable to access jarfile MyApp.jar
The batch file is in the same directory as the jar. Just where I also started from cmd-line.
I tried to call the bat with Administrator-rights and from an admin-shell - no effect.
I'm working on a Windows 7 (64bit) machine. Any hints will be appreciated.
EDIT
You asked for it :) Here is the batch file:
#ECHO OFF
java -jar MyApp.jar
#PAUSE
EDIT 2
Invoking the batch by double-click and from cmd has the same effect.
Changing the command to
java -jar "C:\Full\Path\MyApp.jar"
has no effect.
EDIT 3
I added SET Path=%Path%;C:\Path\To\Jar to the batchfile. No effect.
I also tried using double backslashes as suggested by Chris' link and supported by Viruzzo. Also no effect.
When you double click on .bat file, the working directory is not set to where the .bat file is, but is usually something like %AppData%. To make it work you need to put the full path to the file inside the bat (i.e. java -jar C:\path\to\jar\MyApp.jar).
I just had to put my path in quotes, even though it had no spaces or odd characters.
java -jar C:\Users\Jacob\executables\some-file.jar failed, but
java -jar "C:\Users\JStamm\executables\some-file.jar" worked.
This isn't the answer to your specific problem, but this will allow you to troubleshoot the problem so you will find an answer. Adding a sleep command will let the command prompt sit, so it doesn't close right away.
ping 1.1.1.1 -n 1 -w 10000
(sleep for 10 seconds)
Make sure you don't have the hide extension option enabled in windows. Maybe the name of your jar ends with .jar.jar? Are you sure the path to the jar is correct?
I havent't yet figured out why, but now it works:
The first batchfile was a copy of another and I just changed the contents to fit my needs.
NOW I renamed it to MyApp.bak and wrote a new MyApp.bat from scratch.
As soon as I find out, what the difference was, I'll update this answer.
UPDATE
It happened again with another batch. This time, we got the reason: For some reason, the batch file did not inherit rights when copied. Creating a new one in the folder had the correct rights. So, in the end it was indeed a rights-issue.