Tasawer Nawaz
Tasawer Nawaz

Reputation: 925

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 ?

Upvotes: 1

Views: 3517

Answers (1)

Ramsharan
Ramsharan

Reputation: 2064

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 :.

Upvotes: 1

Related Questions