Skip to content

VS Code Java Code

Sharina Stubbs edited this page Jan 6, 2020 · 3 revisions

To write code using just a simple text editor, such as VS Code, with the intention of printing out to the terminal:

In terminal, create a file

$ touch Demo.java

Open VS code

$ code .  

or...

$ code Demo.java

Within the file, make sure that the name of the class matches the name of the file, and write a main method.

public class Demo {
  // main method is the code that will be run when I compile and run this file. 
  // main method will always look the same.
  public static void main(String[] args) {
    // System.out.println is out equivalent of console.log from JS
    System.out.println("hello World!);
  }
}

To run the code, you must first compile it in the terminal.

javac Demo.java

Note that no news is good news, so if there's no error, great! When you check with ls, you'll see a second file has been created, called Demo.class

Then, you can run it, and have the output in the terminal.

java Demo

You should see Hello World outputted in the terminal.

Clone this wiki locally