Can't import classes from jar

vs97 :

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

Martín Zaragoza :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=76311&siteId=1