Compiling and running

How javac differs from java, and where the .class file goes.

People read the .java source. The machine runs bytecode from a .class file. The command javac Main.java turns the source into bytecode. The command java Main runs a class by its name, without an extension. If you write java Main.class, the machine looks for a class named Main.class and does not find it.

The compiler checks types before anything runs. A "cannot find symbol" error is not a runtime crash but a refusal to build the class. That is why a typo in a method name shows up at once, not after an hour of the service running. The training sandbox uses the same compiler: code that does not build never reaches the tests.

javac Main.java
java Main

People mix up the directory and the classpath. While the class is in the default package and you are in its directory, java Main is enough. As soon as package app; appears, you have to run from the classpath root: java app.Main. Running from a random folder is the classic "works on my laptop".

The habit: build first, read the compiler error to the end, then run. Do not fix syntax by editing a generated .class. The source is the only thing you commit.

Practice

  1. Which command runs it

    The class Main is already compiled. How do you run it?

    • javac Main
    • java Main.class
    • java Main
    • javadoc Main.java
  2. What appears after javac

    What is the extension of the file the JVM runs?

  3. Add and print

    Print the result of the expression 20 + 22 on one line, with no other text.