Debug/run standard java in Visual Studio Code IDE and OS X?

前端 未结 3 1412

Love the light-weight Visual Studio Code in OS X. Have always wanted the ability to write, debug and run standard java (NOT javascript) from VSC in OS X. Found the following

3条回答
  •  鱼传尺愫
    2020-12-13 01:18

    Code Runner Extension will only let you "run" java files.

    To truly debug 'Java' files follow the quick one-time setup:

    • Install Java Debugger Extension in VS Code and reload.
    • open an empty folder/project in VS code.
    • create your java file (s).
    • create a folder .vscode in the same folder.
    • create 2 files inside .vscode folder: tasks.json and launch.json
    • copy paste below config in tasks.json:
    {
        "version": "2.0.0",
        "type": "shell",
        "presentation": {
            "echo": true,
            "reveal": "always",
            "focus": false,
            "panel": "shared"
        },
        "isBackground": true,
        "tasks": [
            {
                "taskName": "build",
                "args": ["-g", "${file}"],
                "command": "javac"
            }
        ]
    }
    
    • copy paste below config in launch.json:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Debug Java",
                "type": "java",
                "request": "launch",
                "externalConsole": true,                //user input dosen't work if set it to false :(
                "stopOnEntry": true,
                "preLaunchTask": "build",                 // Runs the task created above before running this configuration
                "jdkPath": "${env:JAVA_HOME}/bin",        // You need to set JAVA_HOME enviroment variable
                "cwd": "${workspaceRoot}",
                "startupClass": "${workspaceRoot}${file}",
                "sourcePath": ["${workspaceRoot}"],   // Indicates where your source (.java) files are
                "classpath": ["${workspaceRoot}"],    // Indicates the location of your .class files
                "options": [],                             // Additional options to pass to the java executable
                "args": []                                // Command line arguments to pass to the startup class
            }
    
        ],
        "compounds": []
    }
    

    You are all set to debug java files, open any java file and press F5 (Debug->Start Debugging).


    Tip: *To hide .class files in the side explorer of VS code, open settings of VS code and paste the below config:

    "files.exclude": {
            "*.class": true
        }
    

提交回复
热议问题