package in .java file makes class file unuseable

前端 未结 4 1758
忘掉有多难
忘掉有多难 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 17:12

    This is because in Java filesystem files map to classes (e.g. each public class must be in a separate eponymous file) and packages map to directories.

    So if you have a class which is in the com.mycompany.app package it must be in com/mycompany/app directory relative to the classpath.

    In your case you should have an output directory, say and the you should have the class in /com/mycompany/app/App.java. Then you build it, running javac from and giving com/mycompany/app/App.java as parameter, instead of com/mycompany/app/App.java.

    Running the class works in an analogical way, but you give the fully-qualified-name of the class, instead of the directory path.

提交回复
热议问题