title: Does the 'java' command compile Java programs?
body: <p>Most websites on the internet say:</p>
  
  <blockquote>
    <p><em>"use the <code>javac</code> command to compile a <code>.java</code> file. Then run it using the <code>java</code> command"</em></p>
  </blockquote>
  
  <p>But today I tried to run a java program without <code>javac</code> and I got a strange result.</p>
  
  <p>Here are the contents of a file called <code>hello.java</code>:</p>
  
  <pre><code>public class Myclass {
   public static void main(String[] args){
      System.out.println("hello world");
    }
  }
  </code></pre>
  
  <p>Then I ran:</p>
  
  <pre><code>$ javac hello.java
  </code></pre>
  
  <p>Which gives me this error:</p>
  
  <pre><code>hello.java:1: error: class Myclass is public, should be declared in a file named Myclass.java
  public class Myclass {
         ^
  1 error
  </code></pre>
  
  <p>But when I run it without the <code>javac</code> command, it executed without any errors.</p>
  
  <pre><code>$ java hello.java
  hello world
  </code></pre>
  
  <p>Does the <code>java</code> command also compile the program? If yes, why do we need the <code>javac</code> command?</p>
  
  <p>The version of my java is:</p>
  
  <pre><code>openjdk version "12.0.2" 2019-07-16
  OpenJDK Runtime Environment (build 12.0.2+10)
  OpenJDK 64-Bit Server VM (build 12.0.2+10, mixed mode)
  </code></pre>