I want to compile and execute this program with "javac" and "java" commands :
package mypackage;
public class Myclass {
public static void main(String[] args){
System.out.println("test");
}
}
The problem I have : When I am in "mypackage" folder and I use "javac" in it, it produces the file "Myclass.class". But when I use the command "java Myclass" (I'm still in the "mypackage" folder), I have this message :
Error: Could not find or load main class Myclass
Caused by : java.lang.NoClassDefFoundError: mypackage/Myclass (wrong name: Myclass)
The solution I found : I should not go inside "mypackage" folder and execute "java Myclass" but go one folder up and execute "java mypackage.Myclass" (it works). https://stackoverflow.com/a/7509317/10104743
My question : I see some people who can run both "javac Myclass.java" and "java Myclass" commands directly without the need to go one folder up as I did. Why does it not work with me and what can I do to fix this ?