首页 > 解决方案 > 使用 VSCode 并获得 NoClassDefFoundError

问题描述

我通常使用像 IntelliJ 这样的 IDE,但我正在尝试迁移到 VSCode,但我不明白为什么我在 IntelliJ 上工作的项目运行良好,但是当我在 VSCode 中打开该项目时出现该错误。我已经查看了这个问题的其他答案,但他们都提到了我不太熟悉的诸如 bin、src 和 classpath 之类的东西。我想那是如果你通过 cmd 运行 java 但我没有。我该如何解决这个问题?

我在 D:\Antonio\Documents\GitLab\ProjectEuler-Java\Solved_Problems 中的文件

package Solved_Problems;

class Problem_001_MultiplesOf3And5{

  // Multiples of 3 and 5

  /*
   * 
   * If we list all the natural numbers below 10 that are multiples of 3 or 5, we
   * get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the
   * multiples of 3 or 5 below 1000.
   *
   */

  public static void main(String[] args) {

    int totalsum = 0;

    for (int i = 1; i < 1000; i++) {
      if ((i % 3 == 0) || (i % 5 == 0))
        totalsum += i;
    }

    System.out.println(totalsum);

  }

}

输出:

Error: Could not find or load main class Problem_001_MultiplesOf3And5
Caused by: java.lang.ClassNotFoundException: Problem_001_MultiplesOf3And5

[Done] exited with code=1 in 0.835 seconds

标签: javavisual-studio-code

解决方案


我不知道这是否为您解决,但我也花了很长时间才弄清楚。我做了什么:

首先,我使用 Java 插件创建了一个 launch.json 文件。
然后我将mainClass变量编辑为myfilename.java. myfilename如果我只输入mainClass变量,我会遇到和你一样的错误。

作为最后一步,您需要修改classPath以包含项目所需的任何 .jar。


推荐阅读