如何在运行Java代码的Visual Studio Code中隐藏不需要的日志消息

huangapple go评论83阅读模式
英文:

How to hide unwanted log messages on Visual Studio Code, running Java code

问题

我正在使用Visual Studio Code编写Java程序。
除了运行代码时总是收到不想要的消息外,一切都正常。
例如,我创建了一个非常简单基础的Java项目,其中包含一个App.java文件(位于src文件夹中,默认包):

public class App {
    public static void main(String[] args) throws Exception {
        System.out.println("Hello, World!");
    }
}

我已经安装了Java Test Runner和Debugger for Java扩展。如果我点击“Run”,就会出现以下内容:

PS C:\Users\Light\Documents\J\JVsc>  cd 'c:\Users\Light\Documents\J\JVsc'; & 'c:\Users\Light\.vscode\extensions\vscjava.vsco\extensions\vscjava.vscode-java-debug-0.28.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-11.0.1\bin\javaTF-8' '-cp' 'C:\Users\Ri.exe' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Light\AppData\Roaming\Code\User\workspaceStoragee3f4593863e5815\bin' 'App'       7c280b6c994f0132b01b2\redhat.java\jdt_ws\JVsc_623e5815\bin' 'App'
Hello, World!

这种情况非常不好,我必须为学校制作视频,我不希望这些消息出现。我甚至不知道这是否是调试器的问题。

似乎VSC在运行程序之前调用了“CD命令”。
我尝试过更改launch.json中的设置“console”: “internalConsole”,但这并没有解决问题,用这种方式设置后根本没有显示任何输出。

如果我输入“cd src”,然后手动调用“java App.java”,它会打印出Hello World而不会显示那条消息,但显然我不想手动输入编译和运行命令。

英文:

I am using Visual Studio Code to write Java programs.
Everything works fine except that I always get unwanted messages when I run the code.
For example, I created a very simple and basic Java project that contains a App.java file (in the src folder, default package):

public class App {
    public static void main(String[] args) throws Exception {
        System.out.println("Hello, World!");
    }
}

I have installed the extensions Java Test Runner and Debugger for Java. If I press "Run", I get:

PS C:\Users\Light\Documents\J\JVsc>  cd 'c:\Users\Light\Documents\J\JVsc'; & 'c:\Users\Light\.vscode\extensions\vscjava.vsco\extensions\vscjava.vscode-java-debug-0.28.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-11.0.1\bin\javaTF-8' '-cp' 'C:\Users\Ri.exe' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Light\AppData\Roaming\Code\User\workspaceStoragee3f4593863e5815\bin' 'App'       7c280b6c994f0132b01b2\redhat.java\jdt_ws\JVsc_623e5815\bin' 'App'
Hello, World!

This is very bad to see, and I have to record the video for school purposes, and I don't want these messages to appear. I dont' even know if this is a debugger issue or not.

It seems that VSC calls "CD command" before running the program.
I have tried to change the launch.json setting "console": "internalConsole" but it does not solve the problem, what it happens with this setting set up this way is that NO output at all is shown.

If I digit "cd src" then MANUALLY call "java App.java" it prints Hello World without that message, but obviously I don't want to manually type the compile and run commands.

答案1

得分: 1

有一些解决方法:

  1. 在 settings.json 中添加以下内容:

    "terminal.integrated.enableFileLinks": false,

    如何在运行Java代码的Visual Studio Code中隐藏不需要的日志消息

    这将在集成终端中输出之前显示较少的脚本。

  2. 在 launch.json 中设置 "console": "externalTerminal",这将在真正的输出之前获得一个外部控制台,并且不会显示任何脚本:

    如何在运行Java代码的Visual Studio Code中隐藏不需要的日志消息

  3. 安装扩展 Code Runner 并在 settings.json 中添加以下代码:

    "code-runner.clearPreviousOutput": true,

    "code-runner.showExecutionMessage": false,

    如何在运行Java代码的Visual Studio Code中隐藏不需要的日志消息

英文:

There are some workarounds:

1.Add this in settings.json:

"terminal.integrated.enableFileLinks": false,

如何在运行Java代码的Visual Studio Code中隐藏不需要的日志消息
which will show less scripts before output in integrated terminal.

2.Set "console": "externalTerminal" in launch.json, you'll get a external console and no scripts displayed befor the real output:

如何在运行Java代码的Visual Studio Code中隐藏不需要的日志消息

3.Install the extension Code Runner and add the following code in settings.json:

"code-runner.clearPreviousOutput": true,
"code-runner.showExecutionMessage": false,

如何在运行Java代码的Visual Studio Code中隐藏不需要的日志消息

答案2

得分: 0

1) 安装代码运行扩展

2) 转到“文件”>“首选项”>“设置”
搜索“Code-runner: Run In Terminal”,启用它以接受来自扫描仪的输入。

3) 转到“文件”>“首选项”>“设置”,然后搜索“Executor Map”>在settings.json中点击编辑
并根据编程语言进行以下修改,以Java为例:

Java: "**function prompt{ \\">>\\"} && clear && tput setaf 6 &&** cd $dir && javac $fileName && java $fileNameWithoutExt **&& echo `n && tput setaf 5**"**,

你可以查看我的视频获取详细解释:

https://youtu.be/WM5iW8SyGpk

https://youtu.be/yq8j3AsEOL0
英文:

如何在运行Java代码的Visual Studio Code中隐藏不需要的日志消息

  1. Install code runner extension

  2. go to File> preferences > settings
    search for "Code-runner: Run In Terminal" and enable it to accept input from scanner.

  3. go to File> preferences > settings, then search for "Executor Map" > click edit in settings.json
    and add the following modification corresponding to the programming language, java for example:

Java : "function prompt{ \">>\"} && clear && tput setaf 6 && cd $dir && javac $fileName && java $fileNameWithoutExt && echo `n && tput setaf 5",

you can check my videos for detailed explanation

https://youtu.be/WM5iW8SyGpk

https://youtu.be/yq8j3AsEOL0

答案3

得分: 0

以下是您要翻译的内容:

我们能够实现的最接近的方式是在代码运行之前清除终端。操作步骤如下:

  1. 使用 Code Runner 扩展。
  2. 在您的 settings.json 文件中,将 code-runner.executor map 中的 java 值更改如下,以使其如下所示:
"code-runner.executorMap": {
    "java": "cd $dir && javac $fileName && clear && java $fileNameWithoutExt"
}

一些屏幕截图:

  1. 代码运行器未运行
  2. 点击运行代码按钮
  3. 清除控制台
  4. 回答问题
  5. 所有操作完成后的最终输出
英文:

The closest we can get is by clearing the terminal before code runner runs the code. To do that:

  1. Use the Code Runner extension
  2. In your settings.json file, change the java value in code-runner.executor map so that it'll look like this:
"code-runner.executorMap": {
"java": "cd $dir && javac $fileName && clear && java $fileNameWithoutExt"
}

some screen shots:

  1. code runner not running
  2. just hit the run code button
  3. cleared console
  4. answered questions
  5. final output after everything

huangapple
  • 本文由 发表于 2020年10月11日 03:22:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64297373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定