无法从 jar 导入类

vs9*_*s97 4 java import spring jar maven

我在将 JAR 库中的类导入我的项目时遇到问题。请看截图。

我在 Eclipse 和 IntelliJ 中都尝试了几种方法,直接添加和通过 Maven 添加。这些都没有帮助,我仍然得到一个红色下划线。

在 IntelliJ 中,我尝试过:

  1. 项目结构-模块-依赖-添加jar。
  2. 尝试创建一个 lib 文件夹,将其添加为库并将 jar 放入其中,然后设置为依赖项。
  3. 通过 maven pom.xml 添加到 jar 的直接路径。

在 Eclipse 我试过:

  1. Java 构建路径并将其添加到我的构建路径中。
  2. Maven - 更新项目。

这是我的 pom.xml 来获取本地 jar。

<dependency>
  <groupId>uk.co.pervasive_intelligence.simulator</groupId>
  <artifactId>protocol</artifactId>
  <version>1.0</version>
  <scope>system</scope>
  <systemPath>C:\Users\Vas-DELL\Desktop\simulator-1.2.2.jar</systemPath>
</dependency>
Run Code Online (Sandbox Code Playgroud)

奇怪的是,我能够看到罐子和罐子里的类(截图)。但仍然无法导入它们。如果还有什么我可以提供的,请告诉我。

截屏

Mar*_*oza 5

在项目文件夹的根目录中创建一个 lib/ 目录。把你的罐子放在那里。将此添加到您的 pom.xml:

<dependency>
  <groupId>uk.co.pervasive_intelligence.simulator</groupId>
  <artifactId>protocol</artifactId>
  <version>1.0</version>
  <scope>system</scope>
  <systemPath>${basedir}/lib/simulator-1.2.2.jar</systemPath>
</dependency>
Run Code Online (Sandbox Code Playgroud)

不要使用 \ 作为路径分隔符(即使您使用的是 Windows)

运行mvn clean package命令行

您也可以尝试在本地存储库中手动安装依赖项:

mvn install:install-file -Dfile=simulator-1.2.2.jar -DgroupId=uk.co.pervasive_intelligence.simulator -DartifactId=protocol -Dversion=1.0 -Dpackaging=jar

然后将其添加到您的 pom 中:

<dependency>
  <groupId>uk.co.pervasive_intelligence.simulator</groupId>
  <artifactId>protocol</artifactId>
  <version>1.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

编辑:

jar 文件不具有标准 java 库的结构。为了将该 jar 用作库,类的包应作为 jar 文件的基(或根目录)中的文件夹存在。例如:

/META-INF
    /MANIFEST.MF
/uk
    /co
        /pervasive_intelligence
            /simulator
                /BaseComponent.class
                /SimulatorApplication.class
                /SimulatorException.class
....
Run Code Online (Sandbox Code Playgroud)

作为一个库 jar 文件,那么 MANIFEST.MF 的内容可以很简单

Manifest-Version: 1.0
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助

  • 从你的图像中我可以推断出你要么使用错误的包声明引用类...要么 Jar 文件被错误地打包。您无法从 Jar 文件中的 BOOT-INF 文件夹导入类... (2认同)