简体   繁体   English

Linux命令用cronjob运行JAVA

[英]Linux command running JAVA with cronjob

I'm trying to setup a cronjob (cPanel) that execute a shell file which will run java application. 我正在尝试设置一个执行shell文件的cronjob(cPanel)来运行java应用程序。 However I can't get the class path to work. 但是我无法让类路径工作。

JAVA folder application path JAVA文件夹应用程序路径

/home/user/public_html/version_1/data/downloader

This command inside .sh file is working when running through terminal and inside the JAVA application folder 当通过终端和JAVA应用程序文件夹内部运行时,.sh文件中的此命令正在运行

java -cp .:download.jar:log4j.jar:commons-httpclient.jar:commons-fileupload.jar:commons-logging.jar:commons-codec.jar:JAxe-1.0.jar portal.client.Main login

However when running outside the folder it doens't work and returns the following error 但是,当在文件夹外运行时它不起作用并返回以下错误

Error: Could not find or load main class portal.client.Main

so I try to specific class path 所以我尝试了特定的类路径

java -cp /home/user/public_html/version_1/data/downloader/*:.download.jar:log4j.jar:commons-httpclient.jar:commons-fileupload.jar:commons-logging.jar:commons-codec.jar:JAxe-1.0.jar th.or.set.portal.client.Main login

It still showing error 它仍然显示错误

log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:297)
    at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:315)
    at portal.client.Main.main(Main.java:32)
log4j:ERROR Ignoring configuration file [log4j.properties].
log4j:WARN No appenders could be found for logger (portal.controller.MainController).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.IllegalArgumentException: InputStream cannot be null
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:117)
    at portal.client.DownloadParameter.parseLoginParam(DownloadParameter.java:177)
    at portal.controller.MainController.execute(MainController.java:45)
    at portal.client.Main.main(Main.java:130)

Well. 好。 You need to change workdir. 你需要改变workdir。 You may do it simply by using cd 你可以简单地使用cd来做到这一点

cd /home/user/public_html/version_1/data/downloader/
java -cp .:download.jar:log4j.jar:commons-httpclient.jar:commons-fileupload.jar:commons-logging.jar:commons-codec.jar:JAxe-1.0.jar portal.client.Main login

Have you tried creating a script to set your environment up and then fire your java program? 您是否尝试过创建脚本来设置环境然后启动java程序?

your script would look something like : 你的脚本看起来像:

#!/bin/sh
export CLASSPATH=<paths to files>
java <your program>

You can drop your script into the crontab then and twiddle with it as much as you like without having to update your entry. 您可以将脚本放入crontab,然后根据需要随意旋转,而无需更新条目。

You can set the class path in the manifest file of your jar package 您可以在jar包的清单文件中设置类路径

Manifest-Version: 1.0
Implementation-Title: myapp
Implementation-Version: 1.0.1
Class-Path: lib/dep1.jar lib/dep2.jar

then you can run your jar file without passing any additional arguments 然后你可以运行你的jar文件而不传递任何额外的参数

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

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