Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

Visual Studio Code, Java Extension, howto add jar to classpath

In Eclipse, I add a jar library using

project -> build path ->configure build path

What is the equivalent in VisualStudioCode? I had a look into launch.json. There is a classpath defined. Adding jars to this classpath (array) variable seems to have no effect.

Essentially, this is a duplicate question of Visual Studio Java Language Support add jar But that question is unanswered.

This is such an extremely basic question, that I really don't understand not to find a solution for it in Microsoft's documentation or via Google search.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update for latest version

Pre-requisite: In VS Code you need to open the folder and not just file to include any jar file for compilation, running, debugging and testing

VSCode works fine with maven or gradle, and if we are using any of those, we should use maven or gradle for dependency management.

If you are not using any dependency management, then there are two options to add jar file

Option 1: Manually

Open .vscode/settings.json file (if not present, you can create one) and add required jar file there as shown below. In this case all the jar files present in lib directory will be included + I have added common-logging-1.1.1.jar which is located outside the project directory

{
    "java.project.referencedLibraries": [
        "lib/**/*.jar",
        "c:\path\to\jarfile\commons-logging-1.1.1.jar"
    ]
}

Option 2: Via User Interface

If Java Extension is enabled then on left panel there will be Java Dependencies. Inside there you can click on + symbol to add customer jar files. With this method too, the settings.json file will be updated

Screenshot for Adding Java Dependency

Old Answer kept for reference...

VSCode works fine with maven or gradle. But without those, as far as I know they have not provided any direct way to add jar file in classpath.

The workaround is to add the entry in .classpath file manually.

For e.g. in below file I have added common-logging-1.2.jar located in lib directory manually by adding a line <classpathentry exported="true" kind="lib" path="lib/commons-logging-1.2.jar"/>

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="output" path="bin"/>
        <classpathentry exported="true" kind="lib" path="lib/commons-logging-1.2.jar"/>
</classpath>

Update

Finally VSCode team is working towards a better solution. This is still work in progress, but you can refer to this link for better aproach https://github.com/microsoft/vscode-java-pack/issues/94#issuecomment-573487982

Update April 2020

VSCode Extension update now supports this feature out of the box. Now VS Code Java lets you work with JAR files directly without any build tools. Go to JAVA DEPENDENCIES view, find the Referenced Libraries node and click the + icon.

Reference https://github.com/microsoft/vscode-java-pack/blob/master/release-notes/v0.9.0.md#work-with-jar-files-directly


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...