java compile terminal

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.

  1. You could overwrite the file but for simplicity I delete the C.class file:As we show, indeed deleted the C.class file.
    java compile problem terminal
  2. 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:java compile problem terminalbecause you were working with packages you have to compile the package directory and specify with “-d .” the class.
  3. As shown with ls the package is indeed compiled.java compile problem terminal
  4. To run the code it is necessary to start with: command “java“, package name, dot and the class name. Gives: java P.Cjava compile problem terminal
    in this case is the:

    1. package name: P
    2. class name: C
  5. Check the code runs successfully, because the code was only a print statement with “hoi”:
    package P; 
    
    public class C {
    
    	C () {
    		
    	}
    	
    	void start () {
    		System.out.println("hoi");
    	}
    	
    	public static void main(String[] args) {
    		new C().start();
    	}
    }