简体   繁体   English

使用crontab运行简单的Java类文件

[英]Run simple Java class file with crontab

I'm trying to run a simple java helloworld program with crontab. 我正在尝试使用crontab运行一个简单的java helloworld程序。 I made the following java code: helloworld.java: 我编写了以下Java代码:helloworld.java:

class HelloWorld { 
     public static void main (String args[]) {
         System.out.println("Hello world");
   }
}

I then try to run this from a crontab in the following sequence: 然后,我尝试按以下顺序从crontab运行此命令:

  1. crontab -e
  2. 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 最后我插入这行0,7,10,15,30,46,50,55,59 * * * * root /usr/bin/java /home/shivajividhale/cloudOccular/HelloWorld >/dev/null 2>&1

However, I am not able to see the helloworld putput in the syslog. 但是,我无法在syslog中看到helloworld putput。 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. 但是文件上没有任何处理。

Running the file normally java HelloWorld yields proper output. 正常运行文件java HelloWorld产生正确的输出。 I also made sure the crontab ends with a new line. 我还确保crontab以新行结尾。

I just want to get started with having a class file run by the crontab. 我只想开始使用由crontab运行的类文件。 Oher posts discuss about crontab running bash scripts, I just want to run just this simple program. 其他文章讨论了有关运行bash脚本的crontab的信息,我只想运行这个简单的程序。 I just want to print out Hello World along with the time to ensure program execution at the defined intervals. 我只想打印出Hello World和时间,以确保按定义的时间间隔执行程序。 Any help? 有什么帮助吗?

You should not provide a full path when trying to execute a class with "java". 尝试使用“ java”执行类时,不应提供完整路径。 The "java" command expects to receive just the class name as an argument. “ java”命令希望仅接收类名称作为参数。

That's why this works properly: 这就是为什么它可以正常工作的原因:

java HelloWorld java HelloWorld

But this does not: 但这不是:

/usr/bin/java /home/shivajividhale/cloudOccular/HelloWorld / usr / bin / java / home / shivajividhale / cloudOccular / HelloWorld

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. 为了使后者有效,您只需提供类名,并另外提供一个“ classpath”,以便Java知道在哪里可以找到该类。 You can use the "-cp" option to provide the classpath. 您可以使用“ -cp”选项来提供类路径。

Try this: 尝试这个:

/usr/bin/java -cp /home/shivajividhale/cloudOccular/ HelloWorld / usr / bin / java -cp / home / shivajividhale / cloudOccular / HelloWorld

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM