Visual Studio代码,Java扩展,如何将jar添加到类路径 [英] Visual Studio Code, Java Extension, howto add jar to classpath

查看:746
本文介绍了Visual Studio代码,Java扩展,如何将jar添加到类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Eclipse中,我使用

In Eclipse, I add a jar library using

项目->构建路径->配置构建路径

project -> build path ->configure build path

VisualStudioCode中的等效项是什么?我看了launch.json.有一个定义的类路径.将jar添加到此classpath(数组)变量似乎没有任何效果.

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.

从本质上讲,这是一个重复的问题 Visual Studio Java语言支持添加jar 但是这个问题没有答案.

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

这是一个非常基本的问题,我真的不明白没有在Microsoft的文档或通过Google搜索找到解决方案的问题.

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.

推荐答案

更新为最新版本

前提条件:在VS Code中,您需要打开文件夹,而不仅仅是打开文件以包含用于编译,运行,调试和测试的任何jar文件

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可以在maven或gradle中正常工作,如果使用其中任何一种,则应使用maven或gradle进行依赖项管理.

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

如果您不使用任何依赖项管理,那么有两个选项可以添加jar文件

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

打开.vscode/settings.json文件(如果不存在,则可以创建一个)并在其中添加所需的jar文件,如下所示.在这种情况下,将包含lib目录中存在的所有jar文件+我添加了common-logging-1.1.1.jar,它位于项目目录之外

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"
    ]
}

选项2:通过用户界面

如果启用了Java扩展,则在左侧面板上将存在Java依赖关系. 在其中,您可以单击+符号以添加客户jar文件.同样使用这种方法,settings.json文件也将被更新

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

VSCode可以与Maven或Gradle一起正常工作.但是据我所知,没有这些,他们还没有提供任何直接的方法来在类路径中添加jar文件.

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.

解决方法是手动将条目添加到.classpath文件中.

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

例如在下面的文件中,我通过添加行<classpathentry exported="true" kind="lib" path="lib/commons-logging-1.2.jar"/>

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>

更新

最后,VSCode团队正在努力寻求更好的解决方案.这项工作仍在进行中,但您可以参考此链接以获得更好的方法

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

更新2020年4月

VSCode Extension更新现在支持开箱即用的功能. 现在,VS Code Java使您无需任何构建工具即可直接使用JAR文件.转到"JAVA DEPENDENCIES"视图,找到引用的库"节点,然后单击+图标.

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.

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

这篇关于Visual Studio代码,Java扩展,如何将jar添加到类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆