如何在运行时使用maven找出哪个jar文件提供了一个类? [英] How to find out what jar file provides a class during runtime using maven?

查看:85
本文介绍了如何在运行时使用maven找出哪个jar文件提供了一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用mvn jetty:run

I run my application using mvn jetty:run

在编译时,一切都很好,我的行

At compile time everything is fine, my row

  Tidy tidier = new Tidy();
    tidier.setInputEncoding("UTF-8");

可以正常编译,并且类路径显示适当的jar.但是,在运行时出现以下异常,我无法理解原因:

compiles fine and the classpath shows the appropriate jar. However, at runtime I get the following exception and I cannot undestand why:

2009-11-11 17:48:53.384::WARN:  Error starting handlers
java.lang.NoSuchMethodError: org.w3c.tidy.Tidy.setInputEncoding(Ljava/lang/String;)V

我现在正在考虑在我的类路径中可能有这个Tidy的两个不同版本(一个显然没有命名为tidy,否则我可以在maven所示的类路径中检测到它).我试图找出它是什么jar文件,到目前为止,我已经尝试了以下操作:

I am now thinking maybe there are two different versions of this Tidy in my classpath (the one apparently is not named tidy, otherwise I could detect it in the classpath shown by maven). I am trying to find out what jar file it is and so far I have tried the following:

Class<?> tidyClass = Class.forName(Tidy.class.getName());
ClassLoader tidyLoader = tidyClass.getClassLoader();
String name = Tidy.class.getName() + ".class"; // results in tidyClass=class org.w3c.tidy.Tidy
System.out.println("resource="+tidyLoader.getResource(name)); // results in tidyLoader=org.codehaus.classworlds.RealmClassLoader@337d0f
System.out.println("path="+tidyLoader.getResource(name).getPath()); // results in resource=null

我在某处读到该路径应显示jar,但显然不是使用该类加载器...我如何弄清楚呢?一切都像日食一样具有魅力,但是当我使用Maven运行时,我会感到一团糟.BTW日食说

I read somewhere that the path should show the jar but apparently not with this classloader... how can I figure it out? Everything works like a charm in eclipse but when I run with maven I get this mess.. BTW eclipse says

tidyClass=class org.w3c.tidy.Tidy
tidyLoader=sun.misc.Launcher$AppClassLoader@1a7bf11
resource=null so no jar info either.

推荐答案

尝试如下操作:

    Class clazz = null;
    try {
        clazz = Class.forName( typeName );
        if ( clazz != null && clazz.getProtectionDomain() != null
                && clazz.getProtectionDomain().getCodeSource() != null )
        {
            URL codeLocation = clazz.getProtectionDomain().getCodeSource()
                    .getLocation();
            System.out.println( codeLocation.toString() );
        }
    }
    catch ( ClassNotFoundException e ) {
        System.out.println( e.getMessage() );
    }

其中typeName ="org.w3c.tidy.Tidy".

where typeName="org.w3c.tidy.Tidy".

这篇关于如何在运行时使用maven找出哪个jar文件提供了一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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