将JAVA程序作为计划任务运行 [英] Running a JAVA program as a scheduled task
问题描述
我试图在Windows 7计算机上每天运行一次简单的JAVA程序。
I am trying to run a simple JAVA program once per day on a Windows 7 machine.
我的代码在NetBeans中运行良好。如果我做了一个干净的构建它建议:
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"
当然,由于程序和文件之间的空间,这在DOS提示符下不起作用所以我这样做:
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"
这适用于DOS提示符。
This works from the DOS prompt.
我现在在Windows Scheduler中创建一个任务来运行:
I now create a task in Windows Scheduler to run:
C:\Program Files\Java\jdk1.7.0/bin/java
带参数:
-jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
当我然后运行它,我看到的只是一个DOS框闪烁一秒钟。我希望代码运行大约需要30秒。代码应该将数据保存到数据库中,不会发生更新。
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.
代码也使用 java.util.logging
所以我应该看到日志条目而我不这样做。
The code also uses java.util.logging
so I should see log entries and I don't.
我强烈怀疑我没有正确运行JAVA命令或者它存在一个错误的类路径问题通过从DOS提示符运行时不存在的调度程序运行。
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.
帮助将不胜感激。如果您之前已经看过这个并且可以对它进行排序那么会很棒。如果您可以告诉我如何从Scheduler获取有意义的错误跟踪,那么这也非常有用。
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.
谢谢!
推荐答案
我认为您可以创建一个简单的批处理脚本,以这种方式启动您的程序:
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"
将其复制到记事本中并保存作为java_script.cmd,然后直接安排这个脚本而不是程序。
Copy it into the notepad and save as java_script.cmd and then schedule this script instead of the program directly.
这篇关于将JAVA程序作为计划任务运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!