代码之家  ›  专栏  ›  技术社区  ›  vs97

无法从jar导入类

  •  1
  • vs97  · 技术社区  · 6 年前

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

    1. 项目结构-模块-依赖项-添加jar。
    2. 通过maven pom.xml添加到jar的直接路径。

    在Eclipse中,我尝试:

    1. 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>
    

    Screenshot

    2 回复  |  直到 6 年前
        1
  •  4
  •   Martín Zaragoza    6 年前

    在项目文件夹的根目录中创建lib/dir。把你的罐子放那儿。将此添加到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>
    

    不要使用\作为路径分隔符(即使您使用的是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>
    

    编辑:

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

    /META-INF
        /MANIFEST.MF
    /uk
        /co
            /pervasive_intelligence
                /simulator
                    /BaseComponent.class
                    /SimulatorApplication.class
                    /SimulatorException.class
    ....
    

    Manifest-Version: 1.0
    

    希望这有帮助

        2
  •  1
  •   Noman Khan    6 年前

    英国公司。。。 包裹在里面 BOOT-INF/类 模拟器 不能直接访问,因为它不是jar的默认类路径(“.”)的一部分。

    What's the default classpath when not specifying classpath?

    模拟器 jar需要与类路径项打包才能添加BOOT-INF/classes/。。。在类路径中允许访问BOOT-INF/classes下的类。

    例如,允许从包访问类 uk.co.perpusive_intelligence.simulator.Component公司 有了maven

        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
    
                <manifestEntries>
                    <Class-Path>BOOT-NF/classes/uk/co/pervasive_intelligence/simulator/Component</Class-Path>
                </manifestEntries>
            </archive>
        </configuration>
    </plugin>