vs97
vs97

Reputation: 5859

Can't import classes from jar

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

Upvotes: 5

Views: 14967

Answers (4)

Luca Motta
Luca Motta

Reputation: 331

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>

Upvotes: 0

shizhengle
shizhengle

Reputation: 11

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

Upvotes: 0

Mart&#237;n Zaragoza
Mart&#237;n Zaragoza

Reputation: 1817

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

Upvotes: 5

Noman Khan
Noman Khan

Reputation: 960

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> 

Upvotes: 1

Related Questions