使用crontab运行简单的Java类文件 [英] Run simple Java class file with crontab
问题描述
我正在尝试使用crontab运行一个简单的java helloworld程序。
我编写了以下Java代码:
helloworld.java:
I'm trying to run a simple java helloworld program with crontab. I made the following java code: helloworld.java:
class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello world");
}
}
然后,我尝试通过crontab在以下顺序:
I then try to run this from a crontab in the following sequence:
-
crontab -e
- 最后我在这行中插入
0,7,10,15,30,46,50,55,59 * * * * root / usr / bin / java / home / shivajividhale / cloudOccular / HelloWorld> / dev / null 2>& 1
crontab -e
- At the end i insert this line
0,7,10,15,30,46,50,55,59 * * * * root /usr/bin/java /home/shivajividhale/cloudOccular/HelloWorld >/dev/null 2>&1
但是,我看不到syslog中的helloworld putput。一切都正确吗?如何检查类文件是否正在执行。我也尝试将输出打印到带有时间的文本文件中。
However, I am not able to see the helloworld putput in the syslog. Is everything correct? How do I check if the class file is being executed or not. I tried printing the output to a text file with the time on it as well. But nothing is being done on the file.
正常运行文件 java HelloWorld
会产生正确的输出。我还确保crontab以新行结尾。
Running the file normally java HelloWorld
yields proper output. I also made sure the crontab ends with a new line.
我只想开始使用由crontab运行的类文件。 Oher的帖子讨论了有关运行bash脚本的crontab的信息,我只想运行这个简单的程序。我只想打印出Hello World和时间,以确保按定义的时间间隔执行程序。有帮助吗?
I just want to get started with having a class file run by the crontab. Oher posts discuss about crontab running bash scripts, I just want to run just this simple program. I just want to print out Hello World along with the time to ensure program execution at the defined intervals. Any help?
推荐答案
尝试使用 java执行类时,不应提供完整路径。 java命令期望只接收类名作为参数。
You should not provide a full path when trying to execute a class with "java". The "java" command expects to receive just the class name as an argument.
这就是为什么它可以正常工作的原因:
That's why this works properly:
java HelloWorld
java HelloWorld
但这不是:
/ usr / bin / java / home / shivajividhale / cloudOccular / HelloWorld
/usr/bin/java /home/shivajividhale/cloudOccular/HelloWorld
在后面的工作中,您只需要提供类名,并另外提供一个 classpath,以便Java知道在哪里可以找到该类。您可以使用 -cp选项来提供类路径。
To make the latter work, you need to provide just the class name, and additionally a "classpath" so that Java knows where to find that class. You can use the "-cp" option to provide the classpath.
尝试以下操作:
/ usr / bin / java -cp / home / shivajividhale / cloudOccular / HelloWorld
/usr/bin/java -cp /home/shivajividhale/cloudOccular/ HelloWorld
这篇关于使用crontab运行简单的Java类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!