简体   繁体   中英

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). In VS Code, I have half of the Java Extension Pack Installed, ie Language Support for Java (Red Hat) | Debugger for Java (Microsoft) | Visual Studio IntelliCode (Microsoft). 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. 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. 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.

So I wanted to know if there is any way to get this type of output inside the Visual Studio Code . 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.

EDIT:

The Code that I'm running is this one..... File Name - 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)
---Then (VS Code)
-----Prints the Last System.out.println in the console
-----A.txt is not created / written inside
---Then (Powershell)
-----Prints the Last System.out.println in the console
-----A.txt is created and/or written inside

-When (FileName != Class Name)
---Then (VS Code)
-----shows the error same as the image that I included above.
---Then (Powershell)
-----Prints the Last System.out.println in the console
-----A.txt is created and/or written inside

So powershell works as I intend it would, the VS Code isn't...

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: 在此处输入图像描述

If you choose proceed, there should be: 在此处输入图像描述

[UPDATE -- Screenshot in Powershell:] 在此处输入图像描述

It's about the same as problems shown in 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: 在此处输入图像描述

在此处输入图像描述

So I wanted to know if there is any way to get this type of output inside the Visual Studio Code.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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