VSCode中Java代码出现红色波浪线且无法编译的问题求助
Java代码在VSCode中报错但终端运行正常的解决方案
问题现象
- 周二可正常运行的Java代码,今日在VSCode中多处出现红色波浪线,报错包括:
The import java.util cannot be resolvedString cannot be resolved to a typeObject cannot be resolved to a type
- 调试时输出错误:
Error: Unable to initialize main class DSABinarySearchTree Caused by: java.lang.NoClassDefFoundError: [LString; - 已确认Java、Javac版本均为19.0.2,终端通过
javac编译、java运行代码完全正常;重启编辑器和电脑无效,C、Python代码在VSCode中可正常运行。
解决方案
1. 校验VSCode的Java插件配置
- 打开VSCode设置(快捷键
Ctrl+,),搜索java.home,确认路径指向JDK 19.0.2的安装目录,避免插件误识别其他版本JDK。 - 若路径正确,按下
Ctrl+Shift+P,输入Java: Reload Window执行,重新加载Java插件。
2. 清理Java语言服务器缓存
- 按下
Ctrl+Shift+P,输入Java: Clean Java Language Server Workspace,确认后重启VSCode,清除语言服务器缓存文件,解决缓存导致的类型识别异常。
3. 检查项目的JDK运行时配置
- 普通Java项目:右键项目文件夹,选择
Configure Java Runtime,确保项目绑定的JDK版本为19.0.2。 - 构建工具管理的项目:检查根目录下的
.classpath文件,确认JDK引用路径未被意外修改。
4. 排查工作区配置覆盖
- 查看工作区
.vscode/settings.json文件,确认未设置错误的java.configuration.runtimes参数,避免覆盖全局Java配置。
可正常运行的代码
//Outter public class DSABinaryTree import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.util.Scanner; public class DSABinarySearchTree { //Inner private class DSATreeNode private class DSATreeNode { private String m_key; //Unique identifier (String or int), that is used to disinguish nodes private Object m_value; //Value of the node private DSATreeNode m_leftChild; //Where the left child has is less than the right child private DSATreeNode m_rightChild; public DSATreeNode(String inKey, Object inVal) { if (inKey == null) { throw new IllegalArgumentException("Key cannot be null."); } m_key = inKey; m_value = inVal; m_leftChild = null; m_rightChild = null; } //Accessors: public String getKey() {return m_key;} public Object getValue() {return m_value;} public DSATreeNode getLeft(){return m_leftChild;} public DSATreeNode getRight(){return m_rightChild;} //Mutators: public void setLeft(DSATreeNode newLeft) { m_leftChild = newLeft; } public void setRight(DSATreeNode newRight) { m_rightChild = newRight; } } //Outside of private inner class DSATreeNode //Back within outter public class DSABinaryTree private DSATreeNode m_root; public DSABinarySearchTree() { m_root = null; } //Accessor public Object find(String key) { return findRec(key, m_root); //calls the recursive function (Acts as a wrapper), passes a key to find } //Private recusrive accessor private Object findRec(String key, DSATreeNode currNode) { Object value = null; //Value to find //Start at root node if (currNode == null) // { throw new NoSuchElementException("Key " + key + " not found."); //If no child exists (Null), throw an exception } else if (key.equals(currNode.getKey())) //If the keys match, grab value { value = currNode.getValue(); } else if (key.compareTo(currNode.getKey()) < 0) //If key to find is less than node key, go to the left child { value = findRec(key, currNode.getLeft()); //Call recursively to begin the process again, until a matching value is found } else //If the key to find is greater than node key, then go to right child { value = findRec(key, currNode.getRight()); //Another recursive call } return value; } public void insert(String key, Object data) //Wrapper function { m_root = insertRec(key, data, m_root); //Pass in the key to insert, the data/value to insert and the root } private DSATreeNode insertRec(String key, Object data, DSATreeNode currNode) { DSATreeNode updateNode = currNode; //store the current node in a tmp variable if(currNode == null) // If the current node is null (Nothing in the tree), insert the node { //System.out.println("A"); DSATreeNode newNode = new DSATreeNode(key, data); //Create a new node to insert updateNode = newNode; //new node will be returned } else if (key.equals(currNode.getKey())) //If the key matches a key within the tree, do nothing { //System.out.println("B"); } else if (key.compareTo(currNode.getKey()) < 0) //Compare key to insert with the current key { //System.out.println("C"); currNode.setLeft(insertRec(key, data, currNode.getLeft())); //If the compareTo returns a -ve then key is less than current key } else { //System.out.println("D"); currNode.setRight(insertRec(key, data, currNode.getRight())); } //System.out.println(updateNode.getKey()); return updateNode; } public DSATreeNode delete(String key) //Wrapper function { return deleteRec(key, m_root); } private DSATreeNode deleteRec(String key, DSATreeNode currNode) { DSATreeNode updateNode = currNode; if (currNode == null) {/* abort */} else if (key.equals(currNode.getKey())) { updateNode = deleteNode(key, currNode); } else if (key.compareTo(currNode.getKey()) < 0) { currNode.setLeft(deleteRec(key, currNode.getLeft())); } else { currNode.setRight(deleteRec(key, currNode.getRight())); } return updateNode; } private DSATreeNode deleteNode(String key, DSATreeNode delNode) { DSATreeNode updateNode = null; if ((delNode.getLeft() == null) && (delNode.getRight() == null)) //Case: No childs { updateNode = null; } else if ((delNode.getLeft() != null) && (delNode.getRight() == null)) //Case: No right child { updateNode = delNode.getLeft(); } else if ((delNode.getLeft() == null) && (delNode.getRight() != null)) //Case: No left child { updateNode = delNode.getRight(); } else //Case: 2 childs { System.out.println("I am in the case: 2 childs"); updateNode = promoteSuccessor(delNode.getRight()); if (updateNode != delNode.getRight()) { updateNode.setRight(delNode.getRight()); } updateNode.setLeft(delNode.getLeft()); } return updateNode; } private DSATreeNode promoteSuccessor(DSATreeNode curr) { DSATreeNode successor = curr; if (curr.getLeft() != null) { successor = promoteSuccessor(curr.getLeft()); if (successor == curr.getLeft()) { curr.setLeft(successor.getRight()); } } System.out.println(successor.getKey()); return successor; } public void preOrder() { preOrderRec(m_root); // list.displayListForwards(); } private void preOrderRec(DSATreeNode currNode) { if (currNode != null) { System.out.println(currNode.getKey() + " "); preOrderRec(currNode.getLeft()); preOrderRec(currNode.getRight()); } } public void inOrder() { inOrderRec(m_root); // list.displayListForwards(); } private void inOrderRec(DSATreeNode currNode) { if (currNode != null) { inOrderRec(currNode.getLeft()); System.out.println(currNode.getKey() + " "); inOrderRec(currNode.getRight()); } } public void postOrder() { postOrderRec(m_root); // list.displayListForwards(); } private void postOrderRec(DSATreeNode currNode) { if (currNode != null) { postOrderRec(currNode.getLeft()); postOrderRec(currNode.getRight()); System.out.println(currNode.getKey() + " "); } } public void min() { DSATreeNode minNode = minRec(m_root); System.out.println("Key: "+minNode.getKey() +", "+"Node: "+ minNode.getValue()); } private DSATreeNode minRec(DSATreeNode currNode) { DSATreeNode minNode = currNode; if(currNode.getLeft() != null) { minNode = minRec(minNode.getLeft()); } return minNode; } public void max() { DSATreeNode maxNode = maxRec(m_root); System.out.println("Key: "+maxNode.getKey() +", "+"Node: "+ maxNode.getValue()); } private DSATreeNode maxRec(DSATreeNode currNode) { DSATreeNode maxNode = currNode; if(currNode.getLeft() != null) { maxNode = minRec(maxNode.getRight()); } return maxNode; } // public int height() { return heightRec(m_root); } private int heightRec(DSATreeNode currNode) { int htSoFar, iLeftHt, iRightHt; if(currNode == null) { htSoFar = -1; } else { iLeftHt = heightRec(currNode.getLeft()); iRightHt = heightRec(currNode.getRight()); if(iLeftHt > iRightHt) { htSoFar = iLeftHt + 1; } else { htSoFar = iRightHt + 1; } } return htSoFar; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int selection = -1; String key; int value; DSABinarySearchTree tree = new DSABinarySearchTree(); while(selection != 0) { try{ System.out.println("-----------------------------"); System.out.println("What would you like to do?"); System.out.println("[1] Add node"); System.out.println("[2] Delete node"); System.out.println("[3] Find node"); System.out.println("[4] Min, Max & Height"); System.out.println("[5] Display the tree"); System.out.println("[0] Exit Program"); System.out.println("-----------------------------"); selection = sc.nextInt(); switch(selection) { case 1: sc.nextLine(); System.out.println("Enter a key (String Value): "); key = sc.nextLine(); System.out.println("Enter a value (integer value)"); value = sc.nextInt(); tree.insert(key, value); break; case 2: sc.nextLine(); System.out.println("Enter a key to delete (String value): "); key = sc.nextLine(); tree.delete(key); break; case 3: try{ sc.nextLine(); System.out.println("Enter a key to find (String value): "); key = sc.nextLine(); System.out.println("The value for Key = " + key + " is " + tree.find(key)); } catch(NoSuchElementException e) { System.out.println("This key does not exist!"); } break; case 4: try{ System.out.println("Min: "); tree.min(); System.out.println("Max: "); tree.max(); System.out.println("Height: " + tree.height()); } catch(NullPointerException e){ System.out.println("Error: Null pointer exception, nothing in tree"); } break; case 5: sc.nextLine(); System.out.println("Choose display type: "); System.out.println("[1] Pre-order"); System.out.println("[2] In-order"); System.out.println("[3] Post-order"); int x = sc.nextInt(); if(x == 1) { tree.preOrder(); } else if (x == 2) { tree.inOrder(); } else if (x == 3) { tree.postOrder(); } else { System.out.println("Invalid option."); } break; case 0: System.out.println("Good-bye!"); break; default: System.out.println("Invalid input."); break; } } catch (InputMismatchException e) { System.out.println("Error: Incorrect data type entry."); sc.nextLine(); } } } /* public static void main(String[] args) { DSABinarySearchTree tree = new DSABinarySearchTree(); tree.insert("n", 5); tree.insert("l", 7); tree.insert("j", 3); tree.insert("q", 3); tree.insert("p", 3); tree.insert("m", 3); tree.insert("k", 3); tree.insert("o", 3); tree.insert("r", 3); tree.min(); tree.max(); tree.inOrder(); System.out.println(tree.height()); //tree.preOrder(); //tree.postOrder(); //Debugging /* if(tree.m_root != null) { tree.m_root.getValue(); if(tree.m_root.getLeft() != null) { tree.m_root.getLeft().getValue(); } else{ System.out.println("Left child is null"); } if(tree.m_root.getRight() != null) { tree.m_root.getRight().getValue(); System.out.println("Hello"); } else{ System.out.println("Right child is null"); } } else{ System.out.println("Root is null"); } } */ }
内容的提问来源于stack exchange,提问作者jet
相关产品推荐
相关产品推荐

