简体   繁体   English

在 Visual Studio Code 中获得与 PowerShell 或 CMD 中相同的 JAVA 编译错误

[英]Get same JAVA compilation error as in PowerShell or CMD in Visual Studio Code

I'm on latest Windows 10. I have JDK 15. Latest Visual Studio Code (System).我使用的是最新的 Windows 10。我有 JDK 15。最新的 Visual Studio 代码(系统)。 In VS Code, I have half of the Java Extension Pack Installed, ie Language Support for Java (Red Hat) |在 VS Code 中,我安装了一半的 Java 扩展包,即 Java (Red Hat) 的语言支持 | Debugger for Java (Microsoft) | Java (Microsoft) 的调试器 | Visual Studio IntelliCode (Microsoft). Visual Studio IntelliCode (微软)。 So I did that to just get that run button on the top right (the default installed VS Code didn't have that run button for JAVA programs), below the close button, to that I can run the JAVA programs inside the VS Code.所以我这样做只是为了得到右上角的运行按钮(默认安装的 VS Code 没有 JAVA 程序的运行按钮),在关闭按钮下方,我可以在 VS Code 中运行 JAVA 程序。 I didn't wanna go out to the directory then open Power Shell or CMD and then write java filename.java and run the program... I didn't wanna go out to the directory then open Power Shell or CMD and then write java filename.java and run the program...

Now the issue is that when I click the run button, I think, a Power Shell is opened inside the VS Code and then something other than "java FileName.java" is being written.现在的问题是,当我单击运行按钮时,我认为,在 VS 代码中打开了 Power Shell,然后正在编写除“java FileName.java”之外的其他内容。 Because of that I can't really see what the compilation error is.因此,我无法真正看到编译错误是什么。 I can only see the line number where the problem is , not actually the solution for that.我只能看到问题所在的行号,而不是实际的解决方案。 || || If I run the same in the PowerShell outside the VS Code with this "java FileName.java", I can see that there is some issue at x line and also the solution for the same.如果我在 VS Code 之外的 PowerShell 中使用此“java FileName.java”运行相同的程序,我可以看到x 行存在一些问题,并且解决方案也相同。

So I wanted to know if there is any way to get this type of output inside the Visual Studio Code .所以我想知道是否有任何方法可以在Visual Studio Code中获得这种类型的 output Or if there is any way that Instead of writing a lot of thing like this , we can simple tell the Visual Studio Code to run "java fileName.java" inside VS Code when I click the Run Button at the top.或者,如果有什么方法可以在我单击顶部的“运行”按钮时简单地告诉 Visual Studio Code 在 VS Code 中运行“java fileName.java”,而不是写很多这样的东西。

EDIT:编辑:

The Code that I'm running is this one..... File Name - test.java我正在运行的代码是这个.....文件名 - test.java

import java.io.*; 
public class SOPFileTest{ 
public static void main(String arr[]){
    try{
        // Creating a File object that represents the disk file. 
        PrintStream o = new PrintStream(new File("A.txt")); 

        // Store current System.out before assigning a new value 
        PrintStream console = System.out; 

        // Assign o to output stream 
        System.setOut(o); 
        System.out.println("Test 1"); 

        // Use stored value for output stream 
        System.setOut(console); 
        System.out.println("Test 2"); 
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }  
} 

Now I've noticed somethings, they are:现在我注意到了一些东西,它们是:

-When (FileName == Class Name) -当(文件名 == Class 名称)
---Then (VS Code) ---然后(VS代码)
-----Prints the Last System.out.println in the console -----在控制台打印最后一个 System.out.println
-----A.txt is not created / written inside -----A.txt 不是在里面创建/写入的
---Then (Powershell) ---然后(Powershell)
-----Prints the Last System.out.println in the console -----在控制台打印最后一个 System.out.println
-----A.txt is created and/or written inside -----A.txt 在里面创建和/或写入

-When (FileName != Class Name) -当(文件名!= Class 名称)
---Then (VS Code) ---然后(VS代码)
-----shows the error same as the image that I included above. -----显示与我上面包含的图像相同的错误。
---Then (Powershell) ---然后(Powershell)
-----Prints the Last System.out.println in the console -----在控制台打印最后一个 System.out.println
-----A.txt is created and/or written inside -----A.txt 在里面创建和/或写入

So powershell works as I intend it would, the VS Code isn't...所以 powershell 按我的意愿工作,VS Code 不是......

If the filename is different from ClassName, java extension will detect it and throws probelms, which is build failed and you can choose if continue:如果文件名与 ClassName 不同,java 扩展会检测到它并抛出问题,即构建失败,您可以选择是否继续: 在此处输入图像描述

If you choose proceed, there should be:如果你选择继续,应该有: 在此处输入图像描述

[UPDATE -- Screenshot in Powershell:] [更新 -- Powershell 中的屏幕截图:] 在此处输入图像描述

It's about the same as problems shown in VS Code.它与 VS Code 中显示的问题大致相同。

Java extension requires class must be defined in its own file, so filename should be as the same as ClassName , then everything works well, no matter in integrated Terminal in VS Code, or in the PowerShell outside VS Code: Java 扩展要求class 必须在自己的文件中定义,所以文件名应该与 ClassName 相同,然后一切正常,无论是在集成终端中的 VS Code 中,还是在 Z3D265B4E1EEEF0DDF178VSCode:FA003 外部: 在此处输入图像描述

在此处输入图像描述

So I wanted to know if there is any way to get this type of output inside the Visual Studio Code.所以我想知道是否有任何方法可以在 Visual Studio Code 中获得这种类型的 output。

Keeping the filename and classname same makes sure it could be built and compiled successfully, which is the first step.保持文件名和类名相同可以确保它可以成功构建和编译,这是第一步。

And the text file should be generated in current working directory, check it in your file explorer.并且文本文件应该在当前工作目录中生成,在文件资源管理器中检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM