简体   繁体   English

通过源代码确定jar文件的优先级

[英]Prioritize jar file over source code

I have a java project in eclipse 3.2.1 我在eclipse 3.2.1中有一个java项目

I have a java class com.abc.Dinesh in source. 我在源代码中有一个java类com.abc.Dinesh And I also have a some jar file that has a class with the same name and package. 我还有一个jar文件,它有一个具有相同名称和包的类。

I imported the jar file in my project. 我在我的项目中导入了jar文件。 However, in my source code when I'm referencing class Dinesh, the class is resolved to the source and not the binary version. 但是,在我的源代码中,当我引用类Dinesh时,该类被解析为源而不是二进制版本。 I want to refer to the class file in from the jar. 我想从jar中引用类文件。

How can I do this? 我怎样才能做到这一点?

~Thanks 〜谢谢

You could try the following: 您可以尝试以下方法:

right click on your project > Build Path > Configure Build Path...

Then go to the tab 然后转到选项卡

Order and Export

Select (simple click) your jar and press Top or just Up until it is above the src entry. 选择(简单点击)你的jar并按下Top或只是Up直到它在src条目之上。

The only way to resolve your problem (within or outside Eclipse) is to code you own ClassLoader to prioritize Jar files over class files. 解决问题(在Eclipse内部或外部)的唯一方法是编写您自己的ClassLoader,以便在类文件上优先处理Jar文件。 :

see http://kalanir.blogspot.fr/2010/01/how-to-write-custom-class-loader-to.html http://kalanir.blogspot.fr/2010/01/how-to-write-custom-class-loader-to.html

And then setting the jvm "java.system.class.loader" property with the new one 然后使用新的属性设置jvm“java.system.class.loader”属性

Workaround : Do not refer to the .java files. 解决方法:不要引用.java文件。 Remove the .java file / move it to a different project? 删除.java文件/将其移动到另一个项目? and do not refer to that project in this, only to the jar, either directly or to a copy of the version you want in your lib 并且不要将此项目直接引用到jar中,也不要引用到jar中所需版本的副本

The load order of classloaders is Bootstrap CL-->System CL-->Extended CL 类加载器的加载顺序是Bootstrap CL - > System CL - > Extended CL

Now the jar & your source files are in System CL. 现在jar和你的源文件都在System CL中。 Move the JAR file to ext folder in you JDK. 将JAR文件移动到JDK中的ext文件夹。 That way you give precedence to classes on system classpath over jars. 这样,您可以优先于jar上的系统类路径上的类。

If you have two class with same name in classpath in different packages. 如果在不同包中的classpath中有两个具有相同名称的类。 In this case if you are using both the class in same code you will need to qualify with its package. 在这种情况下,如果您在同一代码中同时使用该类,则需要使用其包进行限定。 eg to reference Dinesh you will need to do something like 例如,要参考Dinesh你需要做类似的事情

com.abc.Dinesh dinesh = new com.abc.Dinesh();

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

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