Stolyassa
Stolyassa

Reputation: 113

VSCode can not import external libraries

I am currently trying to learn the VSCode. I am programming in Java 11 now with VSCode. In the example code below, there are 4 lines of imports. First two of them are working. But the next two of them are giving the following errors.

package app;

import java.util.*;
import java.awt.*;
import com.google.common.primitives.Ints;
import org.apache.commons.collections.iterators.ArrayIterator;

/**
 * Board_Space
 */
public class Board_Space {


}
"The import com.google cannot be resolved. Java(268435846) [5,8]"
"The import org.apache.commons.collections cannot be resolved. Java(268435846) [6,8]"

How can I solve this errors and how can I use any kind of external libraries?

Related Environment Info:

 - OS: Windows 10
 - JDK 11 is installed. JAVA_HOME and PATH environment variables are set.
 - apache-maven-3.6.1 is installed. MAVEN_HOME and M2_HOME environment variables are set.
 - VSCode Version: 1.36.1 (user setup) 
 - VSCode Commit: 2213894ea0415ee8c85c5eea0d0ff81ecc191529


Extensions:

 - Name: Checkstyle for Java / Id: shengchen.vscode-checkstyle
 - Name: Code Spell Checker / Id: streetsidesoftware.code-spell-checker
 - name: Debugger for Java / Id: vscjava.vscode-java-debug
 - Name: Java Dependency Viewer / Id: vscjava.vscode-java-dependency
 - Name: Java Extension Pack / Id: vscjava.vscode-java-pack
 - Name: Java Test Runner / Id: vscjava.vscode-java-test
 - Name: Language Support for Java(TM) by Red Hat / Id: redhat.java
 - Name: Maven for Java / Id: vscjava.vscode-maven
 - Name: Visual Studio IntelliCode / Id: visualstudioexptteam.vscodeintellicode

Upvotes: 4

Views: 10624

Answers (2)

侯华为
侯华为

Reputation: 11

global:

settings-> Java › Project: Referenced Libraries in settings.json add:

"java.project.referencedLibraries": [

    "/usr/local/lib/**.jar"
]

local:

at ./.vscode/settings.json, add:

    "pathtojar/**.jar"
]

example

Upvotes: 0

Leo Zhu
Leo Zhu

Reputation: 14956

adding external Jar files whithout maven or gradle .classpath file must be changed for the library.

1.create a folder named lib inside your project and add your .jar file into it

2.Configure paths in the .classpath like:

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

3.if it still thow error,Clean the workspace directory:F1 - input Clean -clean workspace

enter image description here enter image description here

Upvotes: 5

Related Questions