[英]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:换句话说:
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.