9

I'm trying to set up VS code for java programming, and I'm kind of done. However one thing in particular bothers me. When I for example run the code below I get the output in the TERMINAL tab along with a lot of other junk that I don't want to see. How can I change it so that the only output is "Testing..." in the console?

public class Hello{
    public static void main(String[] args){
        System.out.println("Testing...");
    }
}

The output after I run the code is shown in the figure below. Even if I click on the other tabs, they are empty and even if I remove/hide the terminal tab, each time I re-run the code it pops up regardless.

enter image description here

Parseval
  • 503
  • 1
  • 4
  • 14

6 Answers6

9
{
    // 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) - Current File",
            "request": "launch",
            "args": "",
            "console": "internalConsole",
            "mainClass": "${file}"
        },
    ]
}

Add this to your launch.json file. The important option for you here is "console": "internalConsole", This will output everything to the Debug Console tab and not terminal. And it will look clean like this.

Output in debug console

tHeSiD
  • 4,587
  • 4
  • 29
  • 49
  • I removed whatever was there and replaced it with what you have provided. However now when I click "Add Configuration" I get prompted a list to choose from. Which should I pick? – Parseval Jun 12 '20 at 19:16
  • Oh if you are creating a new configuration, click on the Add Configuration and select "Java: Launch Program in Current File" and then you can press F5 on whichever file you have opened in the editor to run it. Don't forget to add the "console": "internalConsole", to that too! – tHeSiD Jun 12 '20 at 19:26
  • I don't know, should I create a new config or just save the old json file? But it does not work anyway. When I paste the configuration above, save the json file and run the java file, it simply adds back the old configuration settings. – Parseval Jun 12 '20 at 20:05
  • 1
    Just delete everything in the launch.json file and paste whats in the answer. I updated it to include everything. Don't add any configuration. – tHeSiD Jun 12 '20 at 20:12
  • In the future, if you want to add another configuration you can use ctrl+space (intellisense) to find all the options available. Try to experiment with them to get the result you need! GL! – tHeSiD Jun 12 '20 at 20:20
  • Thanks for your help buddy! – Parseval Jun 12 '20 at 20:24
  • 1
    Can you do this for Python, too? When I replace "terminal" with "console" in the launch.json the print() function still outputs to the terminal. – elementzero23 May 07 '21 at 15:59
  • `console` is not found for me by autocompletion or in properties references https://imgur.com/a/ituCLAc https://code.visualstudio.com/docs/cpp/launch-json-reference – user2226755 Jan 27 '22 at 05:53
4

I found an even easier answer to this problem thanks to the tHeSiD's answer. To solve it and achieve the same result as tHeSiD, you can do it in the vsCode setting config user interface (also this approach will ensure this new setting will work for all other java projects).

To go to the VsCode setting ui, open vscode, then on top right go to file -> preference -> setting. Then once you are there, to apply the new setting, search launch.json in the search settings box and then scroll down and change the setting to this:

enter image description here

Afterward, if you go back to your java program and press f5, your "Hello World" should show up nice and clear in the Debug Console (It should work as I have tested it, but if it doesn't work, try relaunching vsCode).

Ewan
  • 79
  • 4
2

I think I have a better solution than the other answers. Just copy-paste the code below:

{ 
   "workbench.colorTheme": "Solarized Dark",
   "editor.mouseWheelZoom": true,
   "editor.fontSize": 18,
   "git.enableSmartCommit": true,
   "code-runner.clearPreviousOutput": false,
   "editor.snippetSuggestions": "top",
   "window.zoomLevel":0,
   "workbench.startupEditor": "newUntitledFile",
   "code-runner.runInTerminal": false,
   "code-runner.runInOutput": true,
   "code-runner.saveFileBeforeRun":true,
   "type": "c",
   "name": "Debug (Launch) - Current File",
   "request": "launch",
   "args": "",
   "console": "internalConsole",
   "mainClass": "${file}",
   "code-runner.ignoreSelection": true,
   "terminal.integrated.tabs.enabled": true,
   "json.schemas": [

  ],
    "launch": {

    "configurations": [],
    "compounds": []
            }
     }

Go to File > Preference > Search json.setting > setting.json edit and simply paste it there.

"code-runner.runInTerminal": false,
"code-runner.runInOutput": true,

Check these two especially, after doing everything, you will get your output at "output".

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
2

I have an easy solution for that! With this you can see results in output tab instead of terminal.

Follow these steps in vscode:

  • go to manage (on left bar)
  • go settings
  • search for runinterminal
  • uncheck the box that says runinterminal
  • restart vscode

Use the same method above but by checking the box for viewing the results in terminal!

sukalogika
  • 603
  • 4
  • 11
  • 18
kushal 89
  • 39
  • 1
  • Hello, please read [Why is it considered inappropriate and unprofessional to type in all capital letters?](https://www.quora.com/Why-is-it-considered-inappropriate-and-unprofessional-to-type-in-all-capital-letters). Thank you. – Eric Aya Nov 24 '21 at 09:46
  • Best one! Thanks kushal for it and @sukalogika for editing. – rodolfo_r May 31 '22 at 14:26
  • Did not work for me. – Oscar Sep 28 '22 at 01:27
1

I had the same issue. It's is very simple and you don't need to alter any preferences.

1)Just install code runner extension. 2)Restart vscode. 3)And run as code. enter image description here

enter image description here

enter image description here

0

In addition to above and after installing Code Runner, check if the keyboard shortcut you are using to run the code is NOT assigned to 'Python: Run Selection/ Line in Python Terminal'. Else each time you run the code thinking it will 'Run code', you are actually asking VS code to display all results in Python terminal instead of Output window. you can find the keyboard shortcuts under File-->Preferences.