This is a common made error. If you write your code in a package the terminal could give some problems when compiling and running, because as it says: The terminal “could not find or load main class“. It lost track because of the added package. When it is necessary to use a package it is necessary to compile and run like this:
Steps:
expecting you already know how to compile, otherwise first check this page.
- You could overwrite the file but for simplicity I delete the C.class file:As we show, indeed deleted the C.class file.
- Because you are working with a package it is necessary to specify the directory of the class in order to make it accessible for your terminal:because you were working with packages you have to compile the package directory and specify with “-d .” the class.
- As shown with ls the package P is indeed compiled.
- To run the code it is necessary to start with: command “java“, package name, dot and the class name. Gives: java P.C
in this case is the:- package name: P
- class name: C
- Check the code runs successfully, because the code was only a print statement with “hoi”: