最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【无需解决】VSCode调试java报错:Cannot find a class with the main method in the folder

Java crifan 5427浏览 0评论
折腾:
【已解决】Mac中用VSCode调试java代码
期间,继续调试,报错:
Cannot find a class with the main method in the folder ‘铜陵电力’.
来源:Debugger for java  (扩展)
才注意到,此处是junit:
import org.junit.Test;
所以去把调试配置改为:
      // "mainClass": "${file}"
      "mainClass": "junit.textui.TestRunner",
      "args": "com.iec.test.Analysis104Test"
试试,报错,点击Proceed:
结果:
 /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
{
  // 使用 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
Cannot find a class with a main method, looking in wrong directory, caps sensetive · Issue #552 · microsoft/vscode-java-debug · GitHub
Flink official demo debug will get error :Cannot find a class with the main method · Issue #478 · microsoft/vscode-java-debug · GitHub
此处pom.xml中没有lifecycle-mapping之类的配置
Run and Debug Java in Visual Studio Code
没看到junit相关的
java – Could not find or load main class – VS Code – Stack Overflow
F1 -》Clean the java language server workspace
Are you sure you want to clean the Java language server workspace?
点击Restart and delete,重启了VSCode。
问题依旧。
java – Visual Studio code could not find or load main class after adding more packages – Stack Overflow
去设置
        "cwd": "${workspaceFolder}",
为子目录?
试试
.vscode/launch.json
"cwd": "${workspaceFolder}/src/refer/java/iec_analysis",
结果:
问题依旧。
但是的确有cd过去:
➜  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"
其中确认:
➜  ~ 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
路径是没问题的。
再去想办法加上:
        "sourcePaths": [
            ".",
            "${fileDirname}"
        ],
方面的配置?
Visual Studio Code – Java – Import Errors and More – Stack Overflow
Test Runner can’t find main class · Issue #448 · microsoft/vscode-java-test · GitHub
debugging – Cannot get “Run Test | Debug Test” options under @Test on vscode for Java – Stack Overflow
试试:
"mainClass": "${file}"
不对,此处junit
java – VSCode not finding unit tests – Stack Overflow
java – JUnit in Visual Studio Code – Stack Overflow
此处已有
src/refer/java/iec_analysis/.classpath
    <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>
Java Unit Tests in Visual Studio Code
Run and Debug Java in Visual Studio Code
junit4 – Visual Studio Code launch.json file for JUnit? – Stack Overflow
visual studio code – Java Error: Could not find or load main class Test – Stack Overflow
vscode junit launch.json
vscode junit debug
.net – How to run all tests in Visual Studio Code – Stack Overflow
java – Visual Studio Code projectName – Stack Overflow
Java Test Runner – Visual Studio Marketplace
Announcing JUnit Support for Visual Studio Code | Java at Microsoft
Supporting JUnit 5 in Visual Studio Code | Java at Microsoft
Java Unit Tests in Visual Studio Code 中文文档教程
visual studio code – How to run Junit Test (Java) from VSCode – Stack Overflow
visual studio code – VSCode Junit output not displaying – Stack Overflow
GitHub – kasecato/vscode-javadebug-sample: Java Debugging sample in Visual Studio Code
也提到了:
“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调试了,详见:
Java Unit Tests in Visual Studio Code
但是只是对于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

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.178 seconds, using 22.12MB memory