1

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 ?

6
  • you have a jar file? a class file? or a .java file? Commented Sep 25, 2014 at 14:51
  • 1
    Try removing the wildcards. Just put the name of the directory containing the packages, or the full name (directory+filename) of the jars. e.g.: -classpath /home/project:/home/project/lib/someJar.jar:/home/project/lib/someOtherJar.jar Commented Sep 25, 2014 at 14:52
  • in lib I have jars, and under pakg_name class (compiled one) file, which I want to run Commented Sep 25, 2014 at 14:53
  • @Benjamin tried but not working Commented Sep 25, 2014 at 14:56
  • Assuming that your class name is my_code in the package pkg_name, the following should work: java -classpath /home/project:/home/project/lib/someJar.jar:/home/project/lib/someOtherJar.jar pkg_name.my_code Commented Sep 25, 2014 at 14:57

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.