Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a JAVA program as a scheduled task

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!

like image 680
Kevan Avatar asked Aug 20 '12 08:08

Kevan


People also ask

What is task scheduling in Java?

Java programming language provides a class utility known as Timer Task. It allows one to schedule different tasks. In other words, a task can be executed after a given period or at a specified date and time. A Timer in Java is a process that enables threads to schedule tasks for later execution.


1 Answers

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.

like image 103
aleroot Avatar answered Sep 29 '22 05:09

aleroot