如何使用相对路径添加引用的库 JAR 文件 [英] How to add referenced library JAR files using relative path

查看:69
本文介绍了如何使用相对路径添加引用的库 JAR 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了这个

解决方案

基本上来说,java 扩展从参考库中寻找 jars.

以下是我对您问题的回答:

  1. 右键单击jar并选择复制相对路径然后将其添加到设置中,单击刷新按钮然后添加的jar应显示在Referenced Libraries选项下:

  2. CLASSPATH 变量是告诉应用程序(包括 JDK 工具)在何处查找用户类的一种方式.在添加 jar 时,设置 java.project.referencedLibraries 胜出.

  3. 无法选择jar 文件夹,但您可以使用键盘快捷键选择所有 jar,然后添加它们.

  4. 有一个设置叫做 "java.configuration.checkProjectSettingsExclusions",默认情况下是 true,所以 .project.classpath 不会显示在 VS Code 中.

  5. Java:Configure Classpath命令在我的机器上运行,可以自定义当前项目.

  6. 我的猜测是当你打开一个新项目时,vscode 会弹出一个窗口问你是否信任它,而你选择了,那么项目就是只读的.信任工作区然后尝试命令Java:再次配置类路径,它应该是可写的.

I checked this question and many other posts to help me resolve my issue, but nothing worked.

I opened an existing Java Project using vscode Open Java Project option. I was able to add referenced libraries to an existing regular Java project (it doesn't look like is Maven or any special project) using an absolute path to the src/lib folder from the main project root folder. However, when I add a relative path, then the path is translated to the absolute path starting with C: drive, then the Project Name from the .project file. Then, I get a lot of errors indicating that the referenced libraries are not found.

When I changed the referenced library to an absolute path, all worked fine without any issue. Note that I changed the path to the library in the .classpath file. When I try to add the libraries from Referenced Libraries (Read Only) under JAVA PROJECTS tree in vscode by clicking +, it has no effect. This step will modify the file settings.json under .vscode folder by adding the relevant values to the property "java.project.referencedLibraries". Only when I change the .classpath file under the Project Root Folder, then it will take effect.

I am confused about how to configure the referenced libraries for Java projects in vscode. Following is a summary of questions:

  1. How to add a library using a relative path to a Java Project based on the project root folder?
  2. What is the difference between adding a library using the .classpath file and by modifying settings.json file? Which one will win?
  3. How to add a folder with all JAR files to be as the referenced library to the Java Project? This is to avoid adding one file at a time.
  4. Why the .classpath and .project files are not showing in vscode explorer view? It will only show when you open the file in vscode from the Windows File Explorer.
  5. I found command Configure Classpath but it is read-only, which I think it is the same as the .classpath file. Is there a way to change it from vscode UI?
  6. When I remove .classpath file or when I removed the classpathentry lines from the .classpath file, the node 'Referenced Libraries (Read Only)' under JAVA PROJECTS in vscode view was removed. Why?

See the snapshots below for more details.

I appreciate your help.

解决方案

Basically speaking, java extension looks for jars from Referenced Libraries.

Here're my answers to your questions:

  1. Right click the jar and choose copy relative path then add it to settings, click the refresh button then the added jar should be displayed under the option Referenced Libraries:

  2. The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. When it comes to add jars, settings java.project.referencedLibraries wins.

  3. No way to select jar folders but you can use keyboard shortcuts to select all jars then add them.

  4. There's a setting called "java.configuration.checkProjectSettingsExclusions", and it's true by default, so .project and .classpath won't be shown in VS Code.

  5. The command Java: Configure Classpath works on my machine, which can customize current project.

  6. My guess is when you open a new project, vscode popped up a window and ask you if trust it, and you chose Not, then project is read-only. Trusting the workspace then try the command Java: Configure Classpath again, it should be writeable.

这篇关于如何使用相对路径添加引用的库 JAR 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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