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

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

问题描述

在 Eclipse 中,我使用

添加了一个 jar 库

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

VisualStudioCode 中的等价物是什么?我查看了launch.json.定义了一个类路径.将 jars 添加到这个类路径(数组)变量似乎没有效果.

本质上,这是一个重复的问题

保留旧答案以供参考...

VSCode 适用于 maven 或 gradle.但是没有这些,据我所知,他们没有提供任何直接的方法在类路径中添加 jar 文件.

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

例如在下面的文件中,我通过添加一行 <classpathentry exports="true" kind="手动添加了位于 lib 目录中的 common-logging-1.2.jarlib" path="lib/commons-logging-1.2.jar"/>

<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 Export="true" kind="lib" path="lib/commons-logging-1.2.jar"/></classpath>

更新

最后 VSCode 团队正在努力寻找更好的解决方案.这仍在进行中,但您可以参考此链接以获得更好的方法 https://github.com/microsoft/vscode-java-pack/issues/94#issuecomment-573487982

2020 年 4 月更新

VSCode 扩展更新现在支持开箱即用的此功能.现在,VS Code Java 允许您直接使用 JAR 文件,而无需任何构建工具.转到 JAVA DEPENDENCIES 视图,找到 Referenced Libraries 节点并单击 + 图标.

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

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.

解决方案

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

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

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

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