1

I downloaded Visual Studio Code and installed the "Java Extension Pack" by Microsoft.

Afterwards I downloaded the jdk1.8.0_161 and created the required environment variables as described in the "Java Extension Pack" documentation.

I then created a new file with .java ending and wrote a simple "Hello World" test program.

But how can I now compile and run the code?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
hgd6o
  • 43
  • 1
  • 1
  • 5

2 Answers2

0

As a first step, try to compile your programm from te command line. E.g. How do I run a Java program from the command line on Windows? is a good start. You can run the commands directly in VSCode's integrated terminal. Optionally, create a VS Code build task from that command to run builds with the build command.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build Foo",
            "type": "shell",
            "command": "javac foo.java",
            "problemMatcher": []
        }
    ]
}

To run/debug, create a launch config in the debug viewlet, for example:

   // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug (Launch)-Foo",
            "request": "launch",
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "stopOnEntry": false,
            "mainClass": "Foo",
            "args": ""
        },
        {
            "type": "java",
            "name": "Debug (Attach)",
            "request": "attach",
            "hostName": "localhost",
            "port": 0
        }
    ]
}
0

Here are the overall steps:

  • Install the Java Extension Pack --> you did this already
  • Create a Java project: Ctrl + Shift + P and type "Java". Choose the option "Java: Create Java Project"
  • Modify the App.java class and save it: Ctrl + S

When you save it, VS Code automatically compiles the java files for you. In the bin/app directory you'll see a file called "App.class". That's the confirmation you need.

Edit: Running the project: To run the project, hit F5. VSCode will ask if you want to add a configuration. Choose "Java: Launch Program". Make sure you have App.java opened and hit F5 one more time. That's it.

I noticed that VSCode does not deal well with packages, but that is for another question.

Fergara
  • 929
  • 8
  • 15