Search code examples
javacompilation

How to run and compile java program?


I am compiling a simple program

class Test {
     public static void main(String[] args) {
        System.out.println("Hello");
     }
}

I typed in command prompt java Test.java, but it did not work.

Could not find or load main class Test.java

I'm not using any IDE. Where should I place the program file on my computer?


Solution

  • You cannot directly run a java source file. You need to compile it first using javac:

    javac Test.java
    

    Then you get a .class file that you can run:

    java Test