14

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

6
  • how are you executing your file? F5? Commented Jun 12, 2020 at 14:10
  • also what do you mean by " output is "Testing..." in the console?" that is the console in vscode, do you want it in a separate cmd/powershell window? Commented Jun 12, 2020 at 14:22
  • I execute it by f5 yes. I only want the console to say “Testing...” and nothing else. Right now there are a lot of things showing there, all that blue text. Commented Jun 12, 2020 at 14:40
  • 2
    Figured it out! Check ans Commented Jun 12, 2020 at 19:04
  • similar to this check this question Commented Sep 3, 2021 at 11:06

6 Answers 6

13
{ // 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

Sign up to request clarification or add additional context in comments.

11 Comments

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?
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!
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.
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.
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.
|
7

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).

Comments

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!

3 Comments

Best one! Thanks kushal for it and @sukalogika for editing.
Did not work for me.
1

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".

1 Comment

It didn't work for me. And vscode specifically tells me that the setting "code-runner.runInOutput" isn't supported. Also, unless I misunderstood something, this configuration is for the Code Runner extension. That might be good for running a code snippet, but not for running more complex code (like java code in a Maven project, with a complex classpath).
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.

Comments

0

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

1 Comment

That extension seems to only be intended for running code snippets. Using the built in run functionallity in vscode I'm able to run code with the full classpath of Maven. I never were able to do the same with the Code Runner extension.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.