Search code examples
javacmdjavac

Cannot print on console


I've just installed JDK and set the PATH through the control panel to

C:\Program Files\Java\jdk-15\bin

Then, I tried to run some code i found online:

public class helloworldfirst {

    public static void main(String[] args) {
        // Prints "Hello, World" to the terminal window.
        System.out.println("Hello, World");
    }

}

using the Command Prompt like so: javac helloworldfirst.java , but nothing was displayed in the Command Prompt and instead, a new file was created on my desktop.

Why was this file created? Is it possible to prevent it from being created? How do I print "Hello World!" in the Command Prompt?


Solution

  • Sure, use java and not javac:

    java helloworldfirst.java
    > Hello, World!
    

    note that this requires JDK11 or higher.

    Java is a compiled language; anything but the simplest stuff goes through two steps: First code is compiled (this turns them into .class files), then those can be run. build systems and IDEs generally make this a smooth experience.