Visual Studio Code、Java 扩展、如何将 jar 添加到类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50232557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:30:17  来源:igfitidea点击:

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

javavisual-studio-code

提问by gqp21

In Eclipse, I add a jar library using

在 Eclipse 中,我使用添加了一个 jar 库

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.

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

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

本质上,这是Visual Studio Java Language Support add jar的重复问题, 但该问题没有答案。

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.

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

回答by clankill3r

A terrible solution, but for me it works. (Assuming maven is installed).

一个糟糕的解决方案,但对我来说它有效。(假设安装了 maven)。

Locate your maven repository directory, for me it is:

找到您的 Maven 存储库目录,对我来说是:

/Users/username/.m2/repository

/Users/username/.m2/repository

Then create the path required following the package name. If you don't know the package name you can rename the .jarto a .zipand extract the content.

然后在包名称后面创建所需的路径。如果您不知道包名称,您可以将 重命名.jar为 a.zip并提取内容。

Also create a version number, if you don't have one then make one up. Here is a example of a structure that I created for byte-buddy-agentsince that does not have a maven snippet.

还要创建一个版本号,如果没有就补一个。这是我创建的结构示例,byte-buddy-agent因为它没有 maven 代码段。

Having done that you edit pom.xmland add something among the lines of:

完成后,您编辑pom.xml并在以下行中添加一些内容:

<dependency>
  <groupId>net.bytebuddyagent</groupId>
  <artifactId>byte-buddy-agent</artifactId>
  <version>1.9.8</version>
</dependency>

Save the file, reload the changes, and you should be good to go. A bunch of files should be created in the directory.

保存文件,重新加载更改,您应该一切顺利。应该在目录中创建一堆文件。

enter image description here

在此处输入图片说明

回答by Hemang

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

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

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

VSCode 适用于 maven 或 gradle,如果我们使用其中任何一种,我们应该使用 maven 或 gradle 进行依赖管理。

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

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

Option 1: Manually

选项 1:手动

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

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

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

Option 2: Via User Interface

选项 2:通过用户界面

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.jsonfile will be updated

如果启用了 Java 扩展,那么在左侧面板上将会有 Java 依赖项。在那里,您可以单击+符号以添加客户 jar 文件。也用这种方法,settings.json文件将被更新

Screenshot for Adding Java Dependency

添加 Java 依赖的截图

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.

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

The workaround is to add the entry in .classpathfile manually.

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

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

例如,在下面的文件中,我通过添加一行手动添加了common-logging-1.2.jar位于lib目录中<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

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

Update April 2020

2020 年 4 月更新

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.

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

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

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

回答by lazyTank

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

要添加没有 maven 或 gradle 的外部 Jar 文件,必须为库更改 .classpath 文件。例如

<?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="lib" path="lib/log4j-1.2.6.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

But one extension conflicts with vscode-java. https://marketplace.visualstudio.com/items?itemName=georgewfraser.vscode-javac.Disable it if you've installed to suppress error if you find after changing .classpath file. You can check this issue- https://github.com/redhat-developer/vscode-java/issues/956

但是一个扩展与 vscode-java 冲突。 https://marketplace.visualstudio.com/items?itemName=georgewfraser.vscode-javac 。如果您在更改 .classpath 文件后发现已安装,禁用它以抑制错误。你可以检查这个问题 - https://github.com/redhat-developer/vscode-java/issues/956

回答by Nimr Sawafta

It's so easy, anyway:

无论如何,这很容易:

  1. Create project with java ctrl+shift+p
  2. Add the jar file to your .classpath
  3. Add on the top of your project package [app]; the name of your package
  4. We are done, don't forget to refresh it.
  1. 用java创建项目 ctrl+shift+p
  2. 将 jar 文件添加到您的 .classpath
  3. 在你的项目包顶部添加[app]; 你的包裹名称
  4. 我们完成了,不要忘记刷新它。

回答by Dortmunder

I know this is pretty old, but it is still high up on Google results and many may find it. I just want to recommend the following guide to VSCode wich includes a small but sufficient part to including libraries:

我知道这已经很老了,但它在谷歌搜索结果中仍然很高,很多人可能会找到它。我只想推荐以下 VSCode 指南,其中包含一个很小但足以包含库的部分:

https://blog.usejournal.com/visual-studio-code-for-java-the-ultimate-guide-2019-8de7d2b59902

https://blog.usejournal.com/visual-studio-code-for-java-the-ultimate-guide-2019-8de7d2b59902

the answer is pretty simple:

答案很简单:

  1. add the jar to a folder you prefer, e.g. a new directory "lib" in the project folder.
  2. add a line like: <classpathentry kind="lib" path="lib/javaxt-cor.jar"/>to your .classpath.
  3. done. You can simply import it as you are used to.
  1. 将 jar 添加到您喜欢的文件夹中,例如项目文件夹中的新目录“lib”。
  2. 像添加一行:<classpathentry kind="lib" path="lib/javaxt-cor.jar"/>给你的 .classpath
  3. 完毕。您可以按照习惯简单地导入它。

回答by TimSmith-Aardwolf

I'm going to use the Apache Commons Codec http://commons.apache.org/proper/commons-codec/as an example and start from scratch. I've installed the Visual Studio Code Java Pack Installer https://code.visualstudio.com/docs/languages/javaand got a new VSCode window.

我将使用 Apache Commons Codec http://commons.apache.org/proper/commons-codec/作为示例并从头开始。我已经安装了 Visual Studio Code Java Pack Installer https://code.visualstudio.com/docs/languages/java并获得了一个新的 VSCode 窗口。

  1. Ctrl+Shift+Pand type to select Java: Create Java Project
  2. Choose a folder for your project and click Select the location
  3. Give your project a name and press Enter
  4. Create folder in the project called lib and copy in the commons-codec-1.14 folder from the zip file.
  5. Open the .classpath file and add the following line inside the node
    <classpathentry kind="lib" path="lib/commons-codec-1.14/commons-codec-1.14.jar"/>
    NB You mayneed to use the absolute address of the lib file. In my first go the above code worked fine. My demo project for this guide only worked with a full path eg
    <classpathentry kind="lib" path="C:/Users/tim/Desktop/JavaExample/JavaExample/lib/commons-codec-1.14/commons-codec-1.14.jar"/>
  6. Open the src\App.java file and add the following line below the package app;line import org.apache.commons.codec.binary.Base64;
  1. Ctrl+Shift+P并输入以选择 Java: Create Java Project
  2. 为您的项目选择一个文件夹,然后单击 Select the location
  3. 为您的项目命名并按 Enter
  4. 在名为 lib 的项目中创建文件夹并从 zip 文件复制到 commons-codec-1.14 文件夹中。
  5. 打开 .classpath 文件并在节点内添加以下行
    <classpathentry kind="lib" path="lib/commons-codec-1.14/commons-codec-1.14.jar"/>
    NB 您可能需要使用 lib 文件的绝对地址。在我第一次去上面的代码工作正常。本指南的演示项目仅适用于完整路径,例如
    <classpathentry kind="lib" path="C:/Users/tim/Desktop/JavaExample/JavaExample/lib/commons-codec-1.14/commons-codec-1.14.jar"/>
  6. 打开 src\App.java 文件并在该行下方添加以下package app;import org.apache.commons.codec.binary.Base64;

I was then able to run the following code in App.java

然后我就可以在 App.java 中运行以下代码

package app;
import org.apache.commons.codec.binary.Base64;


public class App {
    public static void main(String[] args) throws Exception {
        String message = "Hello World";
        System.out.println(message);
        String encodedMessage = Base64.encodeBase64String(message.getBytes());
        System.out.println(encodedMessage);

    }
}

To produce

生产

Hello World
SGVsbG8gV29ybGQ=

回答by Abhay

You must have the Java Dependency Viewerextension installed which helps import the jar files and add them to the project's classpath ORadd the libraries to the ".classpath" file manually by using <classpathentry kind="lib" path="manual\path\entry\jarfile.jar"/>tags within <classpath></classpath>tags for each jar file.

您必须安装Java Dependency Viewer扩展,它有助于导入 jar 文件并将它们添加到项目的类路径中,或者通过使用每个 jar 文件的标签内的标签手动将库添加到“ .classpath” 文件中。 <classpathentry kind="lib" path="manual\path\entry\jarfile.jar"/><classpath></classpath>

First what you want to do is know how to create a proper Java "project" in VS Code.

首先你要做的是知道如何在 VS Code 中创建一个合适的 Java“项目”。

To do that:

要做到这一点:

  1. Have a folder opened in VSCode
  2. Press Ctrl+Shit+Pto open the Command Palette
  3. Type in Java: Create New Project
  4. Follow the steps according to your requirements and provide the project name
  1. 在 VSCode 中打开一个文件夹
  2. 按下Ctrl+Shit+P打开命令面板
  3. 输入 Java: Create New Project
  4. 根据您的要求执行步骤并提供项目名称

Then : Expand the Java Dependenciessection in your Explorer Panel then Expand your "project name".

然后:展开资源管理器面板中的Java 依赖项部分,然后展开您的“项目名称”。

  1. When you hover over "Referenced Libraries" you will see the "+" sign. Click it.
  2. It will open the explorer. Use it to select all your jar files and click "Select Jar Files" button.
  1. 当您将鼠标悬停在“参考库”上时,您会看到“ +”号。点击它。
  2. 它将打开资源管理器。使用它来选择所有 jar 文件,然后单击“选择 Jar 文件”按钮。

That's it!!!!

就是这样!!!!

By the way you can see all of this in detail at https://code.visualstudio.com/docs/java/java-project#_working-with-jar-files, it has a clear visual representation of everything related.

顺便说一下,您可以在https://code.visualstudio.com/docs/java/java-project#_working-with-jar-files详细查看所有这些 ,它对所有相关内容都有清晰的视觉表示。

Happy Coding!!!

编码快乐!!!