我有一個 Java 小程式專案,其中一些包無法在 VSCode 中識別。
![如何在 VSCode 中向 JRE 系統庫 [JavaSE-1.6] 添加額外的 .jar?](https://img.uj5u.com/2022/09/15/f0dcf2c2fcc3464c9da4e53ef74679a5.png)
這是因為專案沒有匯入plugin.jar,我在JRE System Library [JavaSE-1.6].
![如何在 VSCode 中向 JRE 系統庫 [JavaSE-1.6] 添加額外的 .jar?](https://img.uj5u.com/2022/09/15/edbf291ad9bc4f5eb2966cc5d4737727.png)
我確定plugin.jar存在于我的C:\Program Files\Java\jdk1.6.0_43\jre\lib檔案夾中。
![如何在 VSCode 中向 JRE 系統庫 [JavaSE-1.6] 添加額外的 .jar?](https://img.uj5u.com/2022/09/15/496e00afac50404984d2fddda670ef3c.png)
如何將其添加plugin.jar到串列中?
順便說一句,我正在使用 Maven,并且我有一個pom.xml檔案。這是它的內容。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<properties>
<project.build.sourceEncoding>big5</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<dependencies>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<pluginManagement>
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
uj5u.com熱心網友回復:
原答案未能解決問題,更新后的答案如下:
使用以下命令將本地.jar包添加到 maven 本地存盤庫。
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
添加成功后,修改pom.xml檔案添加依賴。
<dependencies>
<dependency>
<groupId>group-id</groupId>
<artifactId>artifact-id</artifactId>
<version>version</version>
</dependency>
</dependencies>
原始答案
此類應存盤在jrt-fs.jarJava 檔案夾下。我試過好幾個版本的java(![如何在 VSCode 中向 JRE 系統庫 [JavaSE-1.6] 添加額外的 .jar?](https://img.uj5u.com/2022/09/15/5b49bc0d9ed44495890c4345c7833310.png)
![如何在 VSCode 中向 JRE 系統庫 [JavaSE-1.6] 添加額外的 .jar?](https://img.uj5u.com/2022/09/15/40d3ff0336e8412eb9c83d1a50128a96.png)
即使你洗掉了jrt-fs.jar某個版本下的某個,vscode也會自動參考其他版本下的jar檔案。
所以我認為更好的解決方案是使用官網提供的Java版本。
至于你問的問題。您可以添加.jar如下:
由于您的專案是由 maven 構建的,因此您可以在JAVA PROJECTS面板中看到這兩個依賴管理器:
![如何在 VSCode 中向 JRE 系統庫 [JavaSE-1.6] 添加額外的 .jar?](https://img.uj5u.com/2022/09/15/0eb5c0f9dfc64849ac7aa15c8b2427d8.png)
當然不能改變系統庫,所以只能為maven添加依賴,
點擊Maven Dependencies右側的加號,
![如何在 VSCode 中向 JRE 系統庫 [JavaSE-1.6] 添加額外的 .jar?](https://img.uj5u.com/2022/09/15/da7d13f839e644dc8771dd5a9578021b.png)
輸入要添加的包名稱并搜索它。
![如何在 VSCode 中向 JRE 系統庫 [JavaSE-1.6] 添加額外的 .jar?](https://img.uj5u.com/2022/09/15/f928f5dae7c94b1aa472826eb2fabf2b.png)
選擇一個庫來添加它。
![如何在 VSCode 中向 JRE 系統庫 [JavaSE-1.6] 添加額外的 .jar?](https://img.uj5u.com/2022/09/15/3259ba56a90c4ffcbe5382f03ea857b2.png)
這會修改檔案中的以下內容pom.xml。
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
</dependencies>
如果只是普通的Java專案,點擊Referenced Libraries右邊的加號,在檔案管理器中添加jar。
![如何在 VSCode 中向 JRE 系統庫 [JavaSE-1.6] 添加額外的 .jar?](https://img.uj5u.com/2022/09/15/0336d5ece72c423c85ed305195abc630.png)
![如何在 VSCode 中向 JRE 系統庫 [JavaSE-1.6] 添加額外的 .jar?](https://img.uj5u.com/2022/09/15/47cfbac946c842739afa8a7b4dc85b9f.png)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/507983.html
上一篇:SpringProject-不可決議的匯入POM:找不到io.micrometer:micrometer-bom:pom:2.0.0-M3?
