Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a compiled java file somefile.class, and when i cd into the right folder and do: java somefile it works but when i call it from another folder: java folder/somefile it doesn't work, it gives me java exceptions, could you help me out?

Edit: I am working on a Mac if that matters..

share|improve this question
Does your project use multiple class files? Do you still get the exception if you add the folder with the somefile.class to your path? – Lord Loh. Jun 20 '11 at 21:23
@LordLoh Do you mean to my environmental PATH variable? – Samuel Jun 20 '11 at 21:31
up vote 5 down vote accepted

If you run java folder/somefile java tries to find a class somefile in the package folder, but your class isn't located in the package folder, it's just located in the path folder. You can adjust the search path for classes using the -cp option (meaning classpath).

For example having a class YourClass in the package your.package, this package is located in /your/path you can execute it from any location with:

java -cp /your/path your.package.YourClass

In your specific case you should run:

java -cp folder somefile

I hope it's clear enough ;-)

share|improve this answer
Thank you! That was perfectly clear and helpful – Samuel Jun 20 '11 at 21:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.