Your second example should work normally. Make sure you are executing the correct class (i.e. don't confuse it with the file name). Quoting form JLS specification -section 1.2. Example Programs:
Most of the example programs given in the text are ready to be executed and are similar in form to:
class Test {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++)
System.out.print(i == 0 ? args[i] : " " + args[i]);
System.out.println();
}
}
On a machine with the Oracle JDK installed, this class, stored in the file Test.java, can be compiled and executed by giving the commands:
javac Test.java
java Test Hello, world.
producing the output:
Hello, world.
The JLS uses programs where top level classes are not public. And I am able to run the above example as it is described above. I have used a filename named AAA.java
though it produces a Test.class
when compiled.