简体   繁体   中英

Visual Studio code could not find or load main class after adding more packages

I started using VS Code recently because I find it much cleaner and faster than eclipse but I am having extreme difficulty getting it to run stuff. Currently I am trying to just start a new project so I add a few classes, put them into packages and try to just run a few basic things but it fails to load for any class. My directory structure is as such : https://i.imgur.com/7auMNnI.png and my launch.json is

"version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug (Launch)-Path",
            "request": "launch",
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "stopOnEntry": false,
            "mainClass": "simulation.utils.Path",
            "args": ""
        },
        {
            "type": "java",
            "name": "Debug (Launch)-Route",
            "request": "launch",
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "stopOnEntry": false,
            "mainClass": "simulation.utils.Route",
            "args": ""
        },
        {
            "type": "java",
            "name": "Debug (Attach)",
            "request": "attach",
            "hostName": "localhost",
            "port": 0
        }
    ]
}

However i can not get it to launch any of these. Previously I had it with just one class in its own package and it worked fine. Upon adding more packages it broke everything. From looking at the issue tracking on the git for VSCode it seems that package structure didn't function at one point but has been fixed so this is clearly something I am doing wrong.

I know the question is old, but for anyone else who may be getting the same issue I found that adding a "sourcePaths" reference in launch.json resolved the issue. eg

"configurations": [
    {
        "type": "java",
        "name": "Debug (Launch)",
        "request": "launch",
        "sourcePaths": [
            ".",
            "${fileDirname}"
        ],
        "cwd": "${workspaceFolder}",
        "console": "internalConsole",
        "stopOnEntry": false,
        "mainClass": "MainProg",
        "args": ""
    },

For me it was a weird bug with the code runner extension. Clicking the (VSCode native) Run command above the main method works fine.

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