简体   繁体   English

VS Code 调试无法解析导入语句中的类,但使用命令行可以正常工作

[英]VS Code debug can't resolve classes from import statements while it works fine using command line

I'm working on some simple Data structures using Java and I'm using Princeton's library to implement the data structures but VS Code can't pick the files used under the import statements while it works fine if I compile and run the programs from the terminal.我正在使用 Java 处理一些简单的数据结构,我正在使用 Princeton 的库来实现数据结构,但是 VS Code 无法选择导入语句下使用的文件,而如果我从终端。

Here's my Java code with comments depicting the situation:这是我的 Java 代码,其中包含描述情况的注释:

//these imports work fine
import java.util.Iterator;
import java.util.NoSuchElementException;

//this is available in my local directory
    //VS code is unable to resolve these imports, however it works fine while using integrated terminal
    import edu.princeton.cs.algs4.Bag;
    import edu.princeton.cs.algs4.StdIn;
    import edu.princeton.cs.algs4.StdOut;


public class Stats {
    public static void main(String[] args) {

        // read in numbers
        Bag<Double> numbers = new Bag<Double>();

        int i = 0;
        while (i < args.length) {
            numbers.add(Double.parseDouble(args[i]));
            i++;
        }
        int n = numbers.size();

        // compute sample mean
        double sum = 0.0;
        for (double x : numbers)
            sum += x;
        double mean = sum / n;

        // compute sample standard deviation
        sum = 0.0;
        for (double x : numbers) {
            sum += (x - mean) * (x - mean);
        }
        double stddev = Math.sqrt(sum / (n - 1));

        StdOut.printf("Mean:    %.2f\n", mean);
        StdOut.printf("Std dev: %.2f\n", stddev);
    }
}

Here's what I receive in VS Code build errors:这是我在 VS Code 构建错误中收到的内容:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    StdOut cannot be resolved
    StdOut cannot be resolved

can someone please help me with this?有人可以帮我吗? I'm using Java Extension Pack from Microsoft我正在使用 Microsoft 的 Java 扩展包

Any IDE that is supposed to compile or run Java code needs to have the required classes available.任何应该编译或运行 Java 代码的 IDE 都需要提供所需的类。 In other words:换句话说:

  1. learn what class path means in Java.了解类路径在 Java 中的含义。
  2. setup your IDE to know about all the 3rd party libraries/classes you intend to use, see the corresponding documentation for example.设置您的 IDE 以了解您打算使用的所有 3rd 方库/类,例如,请参阅相应的文档

To add to GhostCat's answer (I am doing the same Coursera class from Princeton), this piece of information is helpful:要添加到 GhostCat 的答案(我正在普林斯顿上相同的 Coursera 课程),这条信息很有帮助:

https://code.visualstudio.com/docs/java/java-project https://code.visualstudio.com/docs/java/java-project

Tl;dr: I had to add this to my .classpath file in the vscode project directory: Tl; dr:我必须将它添加到 vscode 项目目录中的.classpath文件中:

<classpathentry kind="lib" path="/path_to_stdlib.jar" />

You can use maven to track all the dependence and then use the Maven for Java Extension which keep the track of pom.xml file changes and import the library in the project.您可以使用 maven 跟踪所有依赖项,然后使用 Maven 为 Java 扩展跟踪 pom.xml 文件更改并在项目中导入库。

ref: https://code.visualstudio.com/docs/java/java-build参考: https://code.visualstudio.com/docs/java/java-build

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mysqldump从java运行时返回代码6,但同样的命令从命令行运行正常 - mysqldump returns code 6 when run from java, but the same command works fine from command line 无法使用ProcessBuilder运行程序,可以从命令行正常运行 - Can't run program with ProcessBuilder, runs fine from command line 无法在生成 java 类时使用 maven protobuf-maven-plugin 插件解析 google protobuf 文件中的导入语句 - Unable to resolve import statements in google protobuf file using maven protobuf-maven-plugin plugin while generating the java classes 批处理文件中的一行代码可以很好地嵌套使用,在for循环中不起作用 - A line of code in a batch file works fine unnested, doesn't work while in a for loop 无法使用eclipse和weblogic 8.1进行调试(weblogic 10正常运行) - Can't debug using eclipse and weblogic 8.1 ( weblogic 10 works fine) Spring无法解析命令行参数 - Spring can't resolve command line arguments Java应用程序可以从netbeans正常运行,但不能从命令行或.jar文件运行 - Java application works fine from netbeans but not the command line or a .jar file mvn install 在 Eclipse 中不起作用,但在命令行中工作正常 - mvn install is not working in eclipse but works fine from command line HttpsURLConnection.getInputStream 在 Eclipse 中工作正常,但在导出代码并从命令行运行后服务器返回响应代码 503 - HttpsURLConnection.getInputStream works fine in Eclipse but server returns response code 503 after code is exported and run from command line 从Java执行时,MYSQL命令给出错误。 相同的命令在Linux Terminal上可以正常工作。 可能是什么原因? - MYSQL command gives error while being executed from java. Same command works fine on Linux Terminal. What can be the reason?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM