Run compiled language (C, C++, Java) in Visual Studio Code


Yes! Compiled language! With the coming of Code Runner extension, we could run many interpreted language directly in VS Code without any configuration. And now, running compiled language (currently C, C++, Java) is supported by Code Runner! Many thanks to the contribution of @manoelcampos.

With support of running compiled language, Code Runner now supports 24 languages: C, C++, Java, JS, PHP, Python, Perl, Ruby, Go, Lua, Groovy, PowerShell, BAT/CMD, BASH/SH, F#, C#, VBScript, TypeScript, CoffeeScript, Scala, Swift, Julia, Crystal, OCaml

The default configuration for C, C++, Java are as below.

"code-runner.executorMap": {
  "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
  "c": "gcc $fullFileName && ./a.out",
  "cpp": "g++ $fullFileName && ./a.out",
}

As you could see, several placeholders are supported in configuration:

  • $dir: The directory of the code file being run
  • $fullFileName: The full name of the code file being run
  • $fileName: The base name of the code file being run, that is the file without the directory
  • $fileNameWithoutExt: The base name of the code file being run without its extension

You are free to update or add configuration to support more languages. Come and send a Pull Request for us!

runjava

Comments (2)

  1. Pedro Rubini says:

    One example configuration for C/C++ for Windows using MinGW (gcc/g++) is:

    “code-runner.executorMap”: {

    “c”: “gcc -o $fileNameWithoutExt $fullFileName && $fileNameWithoutExt”,
    “cpp”: “g++ -o $fileNameWithoutExt $fullFileName && $fileNameWithoutExt”
    }

    This will both compile and run the file afterwards. If your file’s name is “Program” (Program.cpp), the output will be Program.exe.

Skip to main content