Is it by spec that, given a MyClass.java
file containing
package com.mycorp.foo;
public class MyClass {
public static void main (String[] args) {
The packaging structure mentioned at the top of the class file has less to do with compilation and more to do with Runtime class loading.
You can put any packagename and compile it. e.g. following java class compiles
package com.sa.test.me.yes.no;
public class Test{
public static void main(String[] args){
System.out.println("Hello");
}
}
It compiles even if you don't put inside a folder structure same as package declaration. You can test it by not putting in any folder (e.g java Test.java
)
2 . Packages organization is more important while class loading. The classloader will always search for the class into the folder based on package structure. So when you are trying to run your program, the classloader tries to search for the class file in a folder as per the package structure.