Java:项目中的两个jars,具有相同的类。 [英] Java: Two jars in project with same class.

查看:209
本文介绍了Java:项目中的两个jars,具有相同的类。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java项目,使用两个导入的jar具有相同的类( com.sun.mail.imap.IMAPFolder )。有没有一种方法来显式地说,在导入类时要使用哪个jar?使用:

I have a java project that is using two imported jars with the same class (com.sun.mail.imap.IMAPFolder). Is there a way to explicitly say which jar to use when importing the class? Using:

import com.sun.mail.imap.IMAPFolder; 

似乎按构建路径顺序使用类,但似乎不是这样由于某种原因导致

would seem to use the class in order of build path order but this does not seem to be the case for some reason causing

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoSuchMethodError: com.sun.mail.imap.IMAPFolder.idle()V
        at com.woodbury.GCM.HelperGmailMonitor.doEmail(HelperGmailMonitor.java:104)
        at com.woodbury.GCM.Launch.listen(Launch.java:16)
        at com.woodbury.GCM.Launch.main(Launch.java:10)
        ... 5 more

。我在eclipse构建项目。

at runtime. I am building the project in eclipse.

推荐答案

你不能在Java源代码中做什么。 Java不是为此而设计的。

You cannot do what you ask just in your Java source. Java was not designed for that.

这是一个不好的情况,只能使用自定义类装载器可靠地处理,每个提供一个你需要的jar。因为你首先提出这个问题,这可能不是你应该去的方式,因为开辟了很多新的耗时的问题。

This is a bad situation which can only be handled reliably with custom class loaders, each providing one of the jars you need. Since you are asking this question in the first place this is probably not the way you should go yet since that opens up a LOT of new time consuming problems.

我会强烈建议您找出为什么您的类路径中有两个不同版本的同一个jar,并重做您的程序,所以你只需要一个版本。

I would strongly suggest you find out why you have two different versions of the same jar in your classpath and rework your program so you only need one version.

这篇关于Java:项目中的两个jars,具有相同的类。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆