英文:
VSCode + Gradle Java Run and Debug
问题
我正在尝试使VSCode与使用Java版本11的模块化(从Java版本11开始)的Gradle构建文件一起使用。gradle.build的编译工作正常,但我无法让VSCode执行输出的编译后Java类文件。(它总是试图创建自己的。)
VSCode版本:1.48.2
系统信息:Ubuntu 2020.04
Java构建:
openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment(版本11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64位服务器VM(版本11.0.8+10-post-Ubuntu-0ubuntu120.04,mixed mode,sharing)
build.gradle文件:
apply plugin: 'java'
def version = 0.1
sourceSets {
main {
java {
srcDir 'src/easytext/javamodularity/easytext'
}
}
}
位于$projectDir/src/easytext/module-info.java的module-info.java:
// module easytext {
module easytext.javamodularity.easytext {
requires java.base;
}
非常简单的Main.java,位于$projectDir/src/easytext/javamodularity/easytext/Main.java:
//package easytext;
package easytext.javamodularity.easytext;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.out.println("Welcome to EasyText. Please provide a filename as input argument.");
return;
}
}
gradle文件总是想要输出到**$projectDir/build/**,但是VSCode总是想要在$projectDir/bin/easytext/javamodularity/easytext/Main.class中运行文件。
我还遇到了一个我不太理解的VSCode错误:“module-info.java不在项目EasyText的类路径上,仅报告语法错误。”
运行Gradle任务'build'确实成功编译。
我没有launch.json文件,因为我不知道如何将它指向modulePath?还是有什么魔法步骤我漏掉了吗?
英文:
I'm trying to make VSCode work with using a Gradle build file using Java with modules (from Java version 11). The gradle.build compile works just fine, but I cannot make VSCode execute the output compiled Java class files. (It always tries to create its own.)
VSCode version: 1.48.2
System Info: Ubuntu 2020.04
Java Build:
openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)
build.gradle file:
apply plugin: 'java'
def version = 0.1
sourceSets {
main {
java {
srcDir 'src/easytext/javamodularity/easytext'
}
}
}
module-info.java located at $projectDir/src/easytext/module-info.java
// module easytext {
module easytext.javamodularity.easytext {
requires java.base;
}
Really simple Main.java, located at $projectDir/src/easytext/javamodularity/easytext/Main.java:
//package easytext;
package easytext.javamodularity.easytext;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.out.println("Welcome to EasyText. Please provide a filename as input argument.");
return;
}
}
The gradle file always wants to output to $projectDir/build/, but VScode always wants to run the files in $projectDir/bin/easytext/javamodularity/easytext/Main.class.
I am also getting the VSCode error which I do not understand: "module-info.java is not on the classpath of project EasyText, only syntax errors are reported."
Running the Gradle task 'build' does compile successfully.
I do not have a launch.json file because I am a loss for how to point it to the modulePath? Or is there some sort of magic I'm missing?
答案1
得分: 6
如果你正确配置了用于调试 Gradle 任务,你会在 Gradle 任务列表中的运行图标旁边看到一个调试图标。要进入 Gradle 任务列表,点击扩展栏中的 Gradle 标志(由黄色箭头指示)。红色箭头显示了调试任务。
VSCode 不一定配置为显示这个选项。有三个地方可以进行配置。
- 用户的
settings.json
文件。 - 工作区的
settings.json
文件。 - 远程机器的
settings.json
文件。(仅在远程服务器上开发时适用)
这些文件可以通过以下方式进行编辑:
- 按下<kbd>Ctrl</kbd>+<kbd>,</kbd>打开设置窗口。
- 在搜索栏中键入
gradle.javaDebug
。 - 根据需要选择用户、远程[SSH:yourhosthere]或工作区。
- 点击
Edit in settings.json
。 - 在顶层花括号内添加以下代码。
**注1:**在键入时,您将有选项自动完成其余的配置。此外,如果 settings.json
文件中有其他条目,请确保适当地包括逗号。
{
"gradle.javaDebug": {
"tasks": [
"run",
"runBoot",
"test",
"intTest",
"integration"
]
}
}
**注2:**如果您有一个子项目,您应该将 "gradle.javaDebug"
条目添加到工作区的 settings.json
文件中。条目可能类似以下内容。
{
"gradle.javaDebug": {
"tasks": [
"run",
"runBoot",
"test",
"intTest",
"integration",
"subprojectname:run",
"subprojectname:test"
]
}
}
这在 Visual Studio Code 版本 1.61.2 下进行了测试。如果对 settings.json
的更改未解决您的问题,在此发布时可能有一个与您的问题相关的未解决问题。https://github.com/microsoft/vscode-gradle/issues/876
英文:
If you are properly configured to debug gradle tasks, you will see a debug icon next to the run icon in the gradle Tasks list. To get to the gradle task list, click on the Gradle logo in the extensions bar (indicated by yellow arrow). The red arrow shows the debug task.
VSCode is not necessarily configured to display this option. There are 3 places this can be configured.
- The User
settings.json
file. - The Workspace
settings.json
file. - The remote machine's
settings.json
file. (Only applicable if developing on a remote server)
These files can be edited by
- Pressing <kbd>Ctrl</kbd>+<kbd>,</kbd> to open the settings window.
- Type
gradle.javaDebug
in the search bar. - Select User, Remote[SSH:yourhosthere], or Workspace as desired.
- Click
Edit in settings.json
- Add the following code inside the top level curly brackets.
Note 1: While typing, you will get the option to auto complete the rest of the configuration. Additionally, if there are other entries in your settings.json
file, make sure you include a comma as appropriate.
{
"gradle.javaDebug": {
"tasks": [
"run",
"runBoot",
"test",
"intTest",
"integration"
]
}
}
Note 2: If you have a subproject, you should add the "gradle.javaDebug"
entry into the Workspace settings.json
. The entry would look something like the following.
{
"gradle.javaDebug": {
"tasks": [
"run",
"runBoot",
"test",
"intTest",
"integration",
"subprojectname:run",
"subprojectname:test"
]
}
}
This was tested using Visual Studio Code Version: 1.61.2. If the changes to the settings.json
do not fix your problem, there is an open issue (as of the time of this posting) that may be related to your issue. https://github.com/microsoft/vscode-gradle/issues/876
答案2
得分: 2
这是一个老问题,但我遇到了相同的问题,这里是我的解决方案,如果有人需要的话。
-
安装Gradle Visual Studio Code扩展1
或者您可以通过终端运行任务
Linux
./gradlew build
./gradlew run
Windows
gradlew.bat build
gradlew.bat run
英文:
This is an old question but i had the same problem, here is my solution if anyone needs it.
1)Install the gradle vscode extension
2)open the gradle tasks and here you have the run/debug options
Alternatively you could run the task from terminal using
Linux
./gradlew build
./gradlew run
Windows
gradlew.bat build
gradlew.bat run
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论