使用cronjob运行JAVA的Linux命令

时间:2016-08-31 10:22:56

标签: java linux

我试图设置一个执行shell文件的cronjob(cPanel)来运行java应用程序。但是,我无法让课程路径发挥作用。

JAVA文件夹应用程序路径

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

当通过终端和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

但是,当在文件夹外运行时,它会起作用并返回以下错误

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

所以我尝试了特定的类路径

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

仍显示错误

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)

3 个答案:

答案 0 :(得分:2)

好。你需要改变workdir。您只需使用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

答案 1 :(得分:-1)

您是否尝试过创建脚本来设置环境然后启动java程序?

你的脚本看起来像:

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

您可以将脚本放入crontab,然后根据需要随意旋转,而无需更新条目。

答案 2 :(得分:-1)

您可以在jar包的清单文件中设置类路径

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

然后你可以运行你的jar文件而不传递任何额外的参数