package in .java file makes class file unuseable

前端 未结 4 1794
忘掉有多难
忘掉有多难 2021-01-22 16:41

It\'s been too long since I\'ve last done Java, and I can\'t remember why the following happens:

Given this file, created by a standard Maven project, as can be seen her

4条回答
  •  耶瑟儿~
    2021-01-22 16:59

    When you attempt to execute your program, it will look for the class file using the path specified in the package. So, when you have the package statement in the file, your class file must be in the com/mycompany/app/ directory (relative to what directory you're attempting to run it from); if it can't find it, you get that exception.

    Thus, when you remove that package statement, the JVM will look for it in current directory, which is why it works (because you're executing java App in the same directory in which the App.java and App.class files exist).

提交回复
热议问题