Can't import classes from jar
Asked Answered
H

4

5

I am having problems importing classes from a JAR library into my project. Please see the screenshot.

I have tried several things both in Eclipse and IntelliJ, both adding directly and adding through Maven. None of this helps, I still get a red underline.

In IntelliJ I tried:

  1. Project Structure - Modules - Dependencies - Add jar.
  2. Tried creating a lib folder, add it as a library and place the jar inside and then setup as dependency.
  3. Adding through maven pom.xml with direct path to the jar.

In Eclipse I tried:

  1. Java Build Path and adding it to my build path.
  2. Maven - Update Project.

Here is my pom.xml to get the local 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>

Strangely, I am able to see the jar and the classes inside the jar (screenshot). But still can not import them. Let me know please if there is anything else I can provide.

Screenshot

Hardhearted answered 13/8, 2018 at 13:28 Comment(1)
From your image I can infer that either you're refering to the classes using the wrong package declaration... or the Jar file is wrongly packaged. You cannot import classes from the BOOT-INF folder within a Jar file... Could you show the JAR structure? (you can open the jar using any zip or rar tool)Chamois
C
5

Create a lib/ dir in the root of your project folder. Put your Jar there. Add this to your 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>

Do not use \ as path separator (even though you're using windows)

Run mvn clean package from the command line

You could also try installing the dependecy manually in your local repo:

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

Then add this to your pom:

<dependency>
  <groupId>uk.co.pervasive_intelligence.simulator</groupId>
  <artifactId>protocol</artifactId>
  <version>1.0</version>
</dependency>

EDIT:

The jar file does not have the structure of a standard java library. In order to use that jar as a library, the packages of your classes should exist as folders in the base (or root directory) of your jar file. For example:

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

Being a library jar file then the contents of the MANIFEST.MF can be as simple as

Manifest-Version: 1.0

Hope this helps

Cheekbone answered 13/8, 2018 at 13:31 Comment(6)
Thank you for the steps - still same problem unfortunately. mvn package breaks with many "package does not exist".Hardhearted
From your image I can infer that either you're refering to the classes using the wrong package declaration... or the Jar file is wrongly packaged. You cannot import classes from the BOOT-INF folder within a Jar file...Chamois
Could you show the JAR structure? (you can open the jar using any zip or rar tool)Chamois
Here is the doc with the jar structure: expirebox.com/download/93787331d9fe9db39d50ebc5eabdc2f5.htmlHardhearted
How can we use the classes from the jar file then if the classes cannot be imported?Sext
Hi. What do you mean by "classes cannot be imported" ?Chamois
A
1

Class files in co.uk... package is inside BOOT-INF/classes packed inside simulator jar, and cannot be accessed directly because it not part of jar's default classpath (".").

To understand better about jar classpath please check What's the default classpath when not specifying classpath?

simulator jar need to be packaged with classpath entry to add BOOT-INF/classes/... in classpath to allow access to classes under BOOT-INF/classes.

For example to allow access to classes from package uk.co.pervasive_intelligence.simulator.Component with maven this can be done as

    <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> 
Alathia answered 13/8, 2018 at 14:38 Comment(0)
A
0

If You Use SpringBoot and InteliJ Idea, when you have excute mvn clean package command, you should use packagename.jar.original in maven pom for The jar file have the structure of a standard java library.

/META-INF
    /MANIFEST.MF
/uk
    /co
        /pervasive_intelligence
            /simulator
                /BaseComponent.class
                /SimulatorApplication.class
                /SimulatorException.class
....
Annamarieannamese answered 17/7, 2020 at 6:8 Comment(0)
T
0

I have faced with a similar issue and analyzing the inner jar structure of other working libraries I have noticed that my custom library that I'm trying to import had the incorrect structure descripted by @Martin Zaragoza in his answer:

The jar file does not have the structure of a standard java library. In order to use that jar as a library, the packages of your classes should exist as folders in the base (or root directory) of your jar file.

To fix the issue I have replaced the build pom section of my custom library imported with the following:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
            </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>
            <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>
Trilly answered 24/10, 2023 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.