折腾:
【已解决】Mac中用VSCode调试java代码
期间,继续调试,报错:

Cannot find a class with the main method in the folder ‘铜陵电力’.
来源:Debugger for java (扩展)
才注意到,此处是junit:
1 | import org.junit.Test; |
所以去把调试配置改为:
1 2 3 | // "mainClass": "${file}" "mainClass" : "junit.textui.TestRunner" , "args" : "com.iec.test.Analysis104Test" |
试试,报错,点击Proceed:
结果:

1 2 | / Library / Java / JavaVirtualMachines / jdk1. 8.0_112 .jdk / Contents / Home / bin / java - agentlib:jdwp = transport = dt_socket,server = n,suspend = y,address = localhost: 56028 - Dfile.encoding = UTF - 8 - cp / var / folders / 46 / 2hjxz38n22n3ypp_5f6_p__00000gn / T / cp_6npbs50zhskofshb3z177i66e.jar junit.textui.TestRunner com.iec.test.Analysis104Test Class not found "com.iec.test.Analysis104Test" |
后来,想到了。可能是:
我调试的根目录,不是代码根目录
即此处VSCode的项目的根目录下面子目录
src/refer/java/iec_analysis/src/main/java/com
才是com开头
所以要去加上对应路径?就可以了?
前面在:
【已解决】VSCode中调试java报错:Build failed do you want to continue
解决了所有的error和warning后,此处可以来看看此处的错误了。
然后当前处于
src/refer/java/iec_analysis/src/test/java/com/iec/test/Analysis104Test.java
点击F5,问题依旧。还是报错,无法进入调试。
所以要去搞清楚:
此处,对于一个java的junit的测试的类,想要测试。如何配置。
之前的
.vscode/launch.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | { // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version" : "0.2.0" , "configurations" : [ { "type" : "java" , "name" : "Debug - Current File" , "request" : "launch" , // "mainClass": "${file}" "mainClass" : "junit.textui.TestRunner" , "args" : "com.iec.test.Analysis104Test" } ] } |
肯定要去更新才对。
注:此处特殊是:项目根目录,不是com的上级的src目录,而是更上几级的目录
另外,自己知道,如果VSCode项目根目录,换成src的上级目录:

估计也就没问题了。
但是此处暂时不想这么做。
所以去看看,是否可以解决:
vscode java Cannot find a class with the main method in the folder
vscode java junit test Cannot find a class with the main method in the folder
vscode java junit test Cannot find a class with the main
此处pom.xml中没有lifecycle-mapping之类的配置
没看到junit相关的
F1 -》Clean the java language server workspace


Are you sure you want to clean the Java language server workspace?
点击Restart and delete,重启了VSCode。
问题依旧。
去设置
1 | "cwd" : "${workspaceFolder}" , |
为子目录?
试试
.vscode/launch.json
1 | "cwd" : "${workspaceFolder}/src/refer/java/iec_analysis" , |

结果:
问题依旧。
但是的确有cd过去:
1 2 | ➜ xxx cd / Users / crifan / dev / dev_root / projects / xxx / src / refer / java / iec_analysis ; / Library / Java / JavaVirtualMachines / jdk1. 8.0_112 .jdk / Contents / Home / bin / java - agentlib:jdwp = transport = dt_socket,server = n,suspend = y,address = localhost: 57033 - Dfile.encoding = UTF - 8 - cp / var / folders / 46 / 2hjxz38n22n3ypp_5f6_p__00000gn / T / cp_9genf2idaeseenyjrj6eoyy1t.jar junit.textui.TestRunner com.iec.test.Analysis104Test Class not found "com.iec.test.Analysis104Test" |
其中确认:
1 2 3 4 5 6 7 8 9 | ➜ ~ cd /Users/crifan/dev/dev_root/projects/xxx/src/refer/java/iec_analysis ➜ iec_analysis git:(master) ✗ ll total 24 -rw-r--r-- 1 crifan staff 1.0K 1 8 23:00 LICENSE -rw-r--r-- 1 crifan staff 756B 1 8 23:00 README.md drwxr-xr-x 5 crifan staff 160B 1 8 23:00 docs -rw-r--r-- 1 crifan staff 822B 1 11 10:24 pom.xml drwxr-xr-x 4 crifan staff 128B 1 8 23:00 src drwxr-xr-x 4 crifan staff 128B 1 9 21:01 target |
路径是没问题的。
再去想办法加上:
1 2 3 4 | "sourcePaths" : [ "." , "${fileDirname}" ], |
方面的配置?
试试:
1 | "mainClass" : "${file}" |
不对,此处junit
此处已有
src/refer/java/iec_analysis/.classpath
1 2 3 4 5 6 7 | < classpathentry kind = "src" output = "target/test-classes" path = "src/test/java" > < attributes > < attribute name = "optional" value = "true" /> < attribute name = "maven.pomderived" value = "true" /> < attribute name = "test" value = "true" /> </ attributes > </ classpathentry > |
vscode junit launch.json
vscode junit debug
也提到了:
“Debugging
1. Open Main.java
2. Press F5”
即:普通调试的话,是F5
而对于junit的话,是test,是:
“Testing
1. Open Command Pallete cmd+shift+p (macOS) or ctrl+shift+p (Windonws/Linux)
2. Tasks Run Test Task
3. test”
-》运行对应的测试task任务的。
-》没有F5可以去调试。
【总结】
目前感觉是:
此处已经自动支持JUnit调试了,详见:
但是只是对于JUnit的类能检测出class类,并显示出 Run Test和Debug Test:

这个之前就可以看到,和正常使用:点击Debug Test是可以继续正常调试的。
而想要去F5 去启动此处的juinit的类(此处的Analysis104Test)去测试的话
-》此处没看到有人说支持。看来也是不支持的。
-》而对于上面的错误:
Cannot find a class with the main method in the folder
则:如果换个普通的class,有main方法的,则应该是正常测试的。
转载请注明:在路上 » 【无需解决】VSCode调试java报错:Cannot find a class with the main method in the folder