简体   繁体   English

Java NoSuchMethodError相同的类名

[英]Java NoSuchMethodError same class name

I have two .class file with the same name and same package in two different .jar file 我在两个不同的.jar文件中有两个具有相同名称和相同包的.class文件

First jar: 第一罐:
在此处输入图片说明

Second jar: 第二罐:
在此处输入图片说明

When i run the program from eclipse i haven't problem, eclipse use the first .class file (i must use the first . class, i don't need the second .class, i want exclude it). 当我从eclipse运行程序时,我没有问题,eclipse使用第一个.class文件(我必须使用第一个.class文件,我不需要第二个.class文件,我想排除它)。

When i export runnable .jar i saw that is executed the second .class file and then i have NoSuchMethodError exception, because the second .class is different from the first. 当我导出可运行的.jar时,我看到第二个.class文件已执行,然后出现NoSuchMethodError异常,因为第二个.class与第一个.class不同。

How can i use always the first .class and exclude the second? 如何始终使用第一个.class并排除第二个? I don't need the second .class, but i need other class from his library. 我不需要第二个.class,但是我需要他库中的其他类。

Java loads classes from classpath that is defined dynamically when you are running application in eclipse and is controlled by property Class-Path in file MANIFEST.MF located under META-INF in you jar file. Java从类路径中加载类,这些类路径是在eclipse中运行应用程序时动态定义的,并且由jar文件中META-INF下的MANIFEST.MF文件中的Class-Path属性控制。

So, first open jar file using any ZIP tool and take a look on manifest. 因此,首先使用任何ZIP工具打开jar文件,然后查看清单。 Try to change the order of jar files into manifest and run again. 尝试将jar文件的顺序更改为清单,然后再次运行。 I hope this will help. 我希望这将有所帮助。

BUT this is extremely bad that your alive-matchmarker.jar contains file that it should not contain. 但是,这非常糟糕,因为您的alive-matchmarker.jar包含不应包含的文件。 I do not know what library is it but is there a chance that they have other distribution that does not contain their own dependencies? 我不知道它是什么库,但是他们是否有其他不包含自己依赖项的发行版呢? Or probably try to find other version of this library. 或者可能尝试查找此库的其他版本。 The worse thing that can be is if you have different versions of the same class in your classpath: the behavior of your application can be buggy and unpredictable as a result of this duplication because you can never know what version of class is used now. 更糟糕的是,如果您的类路径中具有相同版本的不同版本:由于重复,应用程序的行为可能有错误且不可预测,因为您永远不知道现在使用的是哪个版本的类。

Do not import the whole package like 不要像这样导入整个包

import org.mindswap.*;

You can import specific class you want from any specific package like 您可以从任何特定的包中导入所需的特定类,例如

import org.mindswap.wsdl.WSDLTranslator;

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

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