tridian
tridian

Reputation: 631

Intellij IDEA editor: classpath order

I've tried to search for a solution for this, but have had no joy: In Intellij IDEA I've set up a set of modules so that I have the following:

Dependency#1 has a class org.acme.foo, but so does MWP and the definition differs (oh joy). It builds ok - all code in MWP correctly builds against the classes in MWP instead of Dependency#1.

But...in its editor, Intellij is giving me the red squiggle because it's preferring to reference the class in Dependency#1 with its incompatible definition.

Ignoring the fact that there really shouldn't be this class/package clash, can anyone help me push Intellij into looking at the class in the local module, and not at the class in the dependency?

EDIT: I'm using the latest Intellij 11.

EDIT: for anyone finding this question, these troubles went away in Intellij 12+

Upvotes: 11

Views: 11877

Answers (4)

Willie Z
Willie Z

Reputation: 506

In my case, it worked well in Eclipse but happened in IntelliJ. The reason really is the tools load libs/jars depending on different mechanisms:

  • Eclipse: .classpath files
  • IntelliJ: *.iml files

I have a bunch of libs/jars are in a directory. The directory is included in the *.iml file like this <orderEntry type="library" name="external-libs" level="project" /> There are 2 libs/jars have conflict (both contain "org.joda.time.DateTime" in my case). But 2 DateTimes have different functions. I have to specify one of them should be loaded before another.

Solution:

  1. Go to "File->Project Structure->Dependencies"
  2. Press the "+" button on the bottom to include the lib/jar I want to load first.
  3. Use the up/down buttons to move the just included lib/jar above the conflict directory/jars/libs.
  4. Rebuild the project.

See the screenshot. joda-time-2.4.jar is added above external-libs to control the loading sequence. joda-time-2.4.jar is added before external-libs to be loaded first.

Upvotes: 1

Tom McIntyre
Tom McIntyre

Reputation: 3689

I had this problem and was using Vladimir's answer which worked (hence my upvote) but unfortunately I found that I had to keep re-editing the .iml file as it was constantly being rewritten. After some digging I found how to change this permanently:

Navigate to File -> Project Structure -> Modules -> -> Dependencies tab

IntelliJ will add the dependencies to the classpath in the order they are listed there. To move them, simply highlight the dependency you want to move and use the up and down arrows on the toolbar at the bottom.

Upvotes: 14

Vladimir Ivanov
Vladimir Ivanov

Reputation: 608

I was facing the same issue in IntelliJ 11 (Mac OS X version).

I manually edited my project's .iml file and re-arranged the dependencies in the right order: I put the orderEntry element containing the right version of the .jar file on top.

Hope this helps.

Upvotes: 11

Mitch Kent
Mitch Kent

Reputation: 584

I have a similar problem that I'm trying to work through.

I have found two situations where I know this occurs and have found a way around one of them.

Scenario 1) You class refers to a class on the classpath further up the chain than the one also found in the jar, however in the import statement the import statement is of the form

import com.company.classes.to.use.*

By bulking them up, the IDE seems to pick up all classes in that package from the same jar location. By splitting them out into individual import class statements the IDE will pick them up separately.

Scenario 2) You class daisy chains methods for the overridden class.

something.getSomethingElse().getAnotherThing().getYetAnotherThing();

if getSomethingElse() returns an object that isn't otherwise in the class (so you dont have to import it) then you still get the error. (Adding the import greys the line as it recognises it isn't used so it doesn't help). I wouldn't condone this method anyway.

edit: Obvious alternative to this is to refactor the code to break the line down to

ObjectToImport obj = something.getSomethingElse();
result = obj.getAnotherThing.getYetAnotherThing();

And then import the temp variable...

Obviously this isn't ideal, and you shouldn't be refactoring your code for the sake of your IDE, but needs must, those red lines annoy the hell out of me.

I hope these help. If you have found a better solution please share!

Regards, M

Upvotes: 0

Related Questions