Since you're running it from command prompt, you need to make sure your classpath is correct. If you set it already, you need to restart your terminal to re-load your system variables.
If -classpath
and -cp
are not used and CLASSPATH
is not set, the current directory is used (.
), however when running .class
files, you need to be in the folder which consist Java package name folders.
So having the .class
file in ./target/classes/com/foo/app/App.class
, you've the following possibilities:
java -cp target/classes com.foo.app.App
CLASSPATH=target/classes java com.foo.app.App
cd target/classes && java com.foo.app.App
You can check your classpath, by printing CLASSPATH
variable:
- Linux:
echo $CLASSPATH
- Windows:
echo %CLASSPATH%
which has entries separated by :
.
See also: How do I run Java .class files?