Running a JAVA program as a scheduled task

Kevan picture Kevan · Aug 20, 2012 · Viewed 38k times · Source

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!

Answer

aleroot picture aleroot · Aug 20, 2012

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.