简体   繁体   English

Intellij IDEA编辑器:classpath命令

[英]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: 我试图为此寻找解决方案,但没有任何乐趣:在Intellij IDEA我设置了一组模块,以便我有以下内容:

  • My Web Project (all module dependencies set to 'Runtime') 我的Web项目(所有模块依赖项设置为'Runtime')
  • My Web Project Dependency #1 (+ artifact that copies JAR to MWP's /WEB-INF/lib) 我的Web项目依赖项#1(+将JAR复制到MWP的/ WEB-INF / lib的工件)
  • My Web Project Dependency #2 (+ artifact that copies JAR to MWP's /WEB-INF/lib) 我的Web项目依赖关系#2(+将JAR复制到MWP的/ WEB-INF / lib的工件)

Dependency#1 has a class org.acme.foo, but so does MWP and the definition differs (oh joy). 依赖关系#1有一个类org.acme.foo,但MWP也是如此,定义也不同(哦,欢乐)。 It builds ok - all code in MWP correctly builds against the classes in MWP instead of Dependency#1. 它构建正常 - MWP中的所有代码都是针对MWP中的类而不是依赖性#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. 但是......在它的编辑器中,Intellij给了我红色的波形,因为它更喜欢用它不兼容的定义引用Dependency#1中的类。

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? 忽略这个类/包冲突确实不应该这样的事实,任何人都可以帮助我推动Intellij查看本地模块中的类,而不是依赖类中的类吗?

EDIT: I'm using the latest Intellij 11. 编辑:我正在使用最新的Intellij 11。

EDIT: for anyone finding this question, these troubles went away in Intellij 12+ 编辑:对于任何发现这个问题的人来说,这些麻烦在Intellij 12+中消失了

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. 我遇到了这个问题并且正在使用弗拉基米尔的答案(因此我的upvote)但不幸的是我发现我必须不断重新编辑.iml文件,因为它经常被重写。 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. IntelliJ将按照列表中的顺序将依赖项添加到类路径中。 To move them, simply highlight the dependency you want to move and use the up and down arrows on the toolbar at the bottom. 要移动它们,只需突出显示要移动的依赖项,然后使用底部工具栏上的向上和向下箭头。

I was facing the same issue in IntelliJ 11 (Mac OS X version). 我在IntelliJ 11(Mac OS X版本)中遇到了同样的问题。

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. 我手动编辑了项目的.iml文件,并按正确的顺序重新排列了依赖项:我将包含正确版本的.jar文件的orderEntry元素放在最上面。

Hope this helps. 希望这可以帮助。

In my case, it worked well in Eclipse but happened in IntelliJ. 在我的例子中,它在Eclipse中运行良好,但发生在IntelliJ中。 The reason really is the tools load libs/jars depending on different mechanisms: 原因是工具根据不同的机制加载libs / jars:

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

I have a bunch of libs/jars are in a directory. 我有一堆libs / jars在一个目录中。 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). 该目录包含在* .iml文件中,如下所示<orderEntry type="library" name="external-libs" level="project" />有2个libs / jars存在冲突(均包含“org.joda.time”) .DateTime“在我的情况下)。 But 2 DateTimes have different functions. 但是2个DateTimes具有不同的功能。 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. 按下底部的“+”按钮,包括我想先加载的lib / jar。
  3. Use the up/down buttons to move the just included lib/jar above the conflict directory/jars/libs. 使用向上/向下按钮将刚刚包含的lib / jar移动到冲突目录/ 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被添加到external-libs之上以控制加载序列。 在首先加载外部库之前添加joda-time-2.4.jar。

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 场景1)你的类指的是类路径上的一个类,它比jar中也找到的类更强,但是在import语句中,import语句的形式是

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. 通过填充它们,IDE似乎从同一个jar位置获取该包中的所有类。 By splitting them out into individual import class statements the IDE will pick them up separately. 通过将它们拆分为单独的导入类语句,IDE将单独选择它们。

Scenario 2) You class daisy chains methods for the overridden class. 场景2)您为重写的类分类菊花链方法。

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. 如果getSomethingElse()返回一个在类中没有的对象(所以你不必导入它),那么你仍然会得到错误。 (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. 显然这不是理想的,你不应该为了你的IDE而重构你的代码,但是需要,那些红线会让我烦恼。

I hope these help. 我希望这些帮助。 If you have found a better solution please share! 如果您找到了更好的解决方案,请分享!

Regards, M 问候,M

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM