vscode-java-debug icon indicating copy to clipboard operation
vscode-java-debug copied to clipboard

Add "run as sudo" to launch.json for VSC Java project

Open FDelporte opened this issue 4 years ago • 1 comments

I'm working on the Pi4J project (https://v2.pi4j.com/) to control GPIOs with a Java application. Pi4J uses a native library to communicate with the GPIOs. For some protocols (like I2C) the library needs to be called with "sudo". I know this is a bad idea and should be avoided, and we are looking for an alternative solution, but in the mean-time I found out you can't run or debug a Java application in VSC as sudo.

You could launch VSC itself as sudo, but this gives the following warning, so that's probably even a worse idea ;-)

$ sudo code
You are trying to start Visual Studio Code as a super user which isn't recommended. If this was intended, please specify an alternate user data directory using the `--user-data-dir` argument.
Environment
  • Operating System: Raspberry Pi OS (Linux, Debian)
  • JDK version: OpenJDK 11.0.9.1
  • Visual Studio Code version: 1.53.2
  • Java extension version: Java Extension Pack 0.12.1
  • Java Debugger extension version: 0.31.0
Steps To Reproduce
  1. Create a launch.json file, e.g.
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Launch CrowPiApp",
            "request": "launch",
            "mainClass": "com.pi4j.example/com.pi4j.example.CrowPiApp",
            "projectName": "pi4j-example-crowpi",
            "args": "-a -p"
        }
    ]
}
  1. It should be possible to add extra setting "sudo": true
Current Result

sudo setting not available

Expected Result

sudo true/false can be defined

Additional Informations

The example project is https://github.com/Pi4J/pi4j-example-crowpi

FDelporte avatar Mar 03 '21 07:03 FDelporte

three years past, is it available?

theremoon-j avatar Jun 24 '24 03:06 theremoon-j

Hi @FDelporte, I'm an AI Support assistant here to help with your issue. While the team reviews your request, I wanted to provide some possible tips and documentation that might help you in the meantime.

There is no built-in sudo flag in launch.json, but here are two workable approaches:

• Avoid needing sudo at all by giving your user access to GPIO/I²C devices: – Add your user to the gpio and i2c groups: bash sudo usermod -a -G gpio,i2c $USER reboot – On Raspberry Pi OS the /dev/gpiomem and /dev/i2c-* nodes will then allow non‐root access.

• Use a wrapper task + attach-style debugging:

  1. In .vscode/tasks.json, define a shell task that starts your app under sudo with JDWP enabled:
    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "run-with-sudo-debug",
          "type": "shell",
          "command": "sudo java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005 -cp target/classes com.pi4j.example.CrowPiApp -a -p",
          "problemMatcher": []
        }
      ]
    }
    
  2. In .vscode/launch.json, add an attach configuration:
    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "java",
          "name": "Attach to sudo app",
          "request": "attach",
          "hostName": "localhost",
          "port": 5005
        }
      ]
    }
    
  3. Run the task (Terminal > Run Task > run-with-sudo-debug), then launch the “Attach to sudo app” debug.

Other references with low confidence

Click to expand

• Java Debugging docs – explains attach debugging in VS Code: https://code.visualstudio.com/docs/java/java-debugging#_attach-to-a-running-application

The team will respond to your issue shortly. I hope these suggestions are helpful in the meantime. If this comment helped you, please give it a 👍. If the suggestion was not helpful or incorrect, please give it a 👎. Your feedback helps us improve!

github-actions[bot] avatar Nov 11 '25 09:11 github-actions[bot]