Problem
Pressing F5, or clicking Run or Debug above the main method in Visual Studio Code with the Java and Kotlin by IntelliJ IDEA extension, produces:
Error: Could not find or load main class com.example.demo.Main
Caused by: java.lang.ClassNotFoundException: com.example.demo.Main
Process finished with exit code 1
The run may also use previously compiled classes and not reflect the latest source changes.
Cause
The extension does not invoke the project build tool before launching, so the classpath either lacks the target class or points to stale output.
Resolution
A fix is planned for the next release of the Java and Kotlin by IntelliJ IDEA extension. Until then, run the build as a preLaunchTask from launch.json.
Step 1. Define the build task in tasks.json
- Open the Command Palette with Ctrl+Shift+P (Windows, Linux) or Cmd+Shift+P (macOS).
- Run Tasks: Configure Task, then select Create tasks.json file from template → Others. When Gradle or Maven is detected, VS Code may list the build task directly.
- Add a build task. The example below runs Gradle; replace
commandwithmvn compileor another build script as needed:
{
"version": "2.0.0",
"tasks": [
{
"label": "Run Gradle Build",
"type": "shell",
"command": "${cwd}/gradlew build -x test",
"group": "build",
"problemMatcher": [],
"options": {
"env": {
"JAVA_HOME": "/Users/user/.vscode/extensions/jetbrains.intellij-server-0.0.10-darwin-arm64/server/jbr/Contents/Home"
}
}
}
]
}
options.env is optional. Set JAVA_HOME when the system does not already have a suitable JDK on PATH. Reuse the JBR bundled with the extension:
- Linux or WSL:
/home/user/.vscode/extensions/jetbrains.intellij-server-0.0.10/server/jbr - macOS (Apple Silicon):
/Users/user/.vscode/extensions/jetbrains.intellij-server-0.0.10-darwin-arm64/server/jbr/Contents/Home - Windows:
C:\Users\user\.vscode\extensions\jetbrains.intellij-server-0.0.10\server\jbr
Adjust the version segment (0.0.10) to match the installed extension build.
Step 2. Reference the task from launch.json
- Open the Run and Debug view with Ctrl+Shift+D (Windows, Linux) or Cmd+Shift+D (macOS).
- Click create a launch.json file, or open the existing file with the gear icon.
- Select intellij_debugger as the environment.
- Add
preLaunchTaskand set its value to the exactlabelfrom Step 1:
{
"type": "intellij_debugger",
"request": "launch",
"name": "Run App",
"mainClass": "com.example.demo.DemoApplication",
"cwd": "${workspaceFolder}",
"preLaunchTask": "Run Gradle Build"
}
Step 3. Run the configuration
- Select Run App from the dropdown at the top of the Run and Debug panel.
- Click the green Play arrow or press F5.
- VS Code runs the build task in the terminal, then launches the configured main class once the build succeeds.
Comments
0 comments
Please sign in to leave a comment.