简体   繁体   中英

How to run java code from shell script on linux

I want to set a cron job for my java code and I have tried this from project directory

    java -classpath .:/home/project/lib/* pkg_name.my_code

and it works fine, however I dont know how to run it from any other directory[like in script] I have tried to add diroctry (having compiled classes) in classpath like this

    java -classpath .:/home/project/lib/*;/home/project/pkg_name/* pkg_name.my_code

also tried

    java -classpath ".:/home/project/lib/*;/home/project/pkg_name/*" pkg_name.my_code

but it gives error:

**Error: Could not find or load main class pkg_name.my_code **

can any please help me ?

If you want to run your project from another directory, then you need to include your project in classpath. So you can do this

java -classpath ".:/home/project/lib/*:/home/project" pkg_name.my_code

For example :

java -classpath ".:/home/test/runjavafromanotherdirectory/lib/*:./runjavafromanotherdirectory" com.test.Main

One of your mistake is you are using ; instead of : .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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