Ittikorn S. Ittikorn S. - 3 months ago 17
Linux Question

Linux command running JAVA with cronjob

I'm trying to setup a cronjob (cPanel) that execute a shell file which will run java application. However I can't get the class path to work.

JAVA folder application path

/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 -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)

Answer

Well. You need to change workdir. You may do it simply by using 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