简体   繁体   English

如何在运行时知道JAR文件是否已存在于类路径中?

[英]How can I know at runtime if a JAR file is already in the classpath?

What is the best way to know at runtime if a particular JAR file is already in my classpath ? 如果特定的JAR文件已经在我的类路径中,那么在运行时知道的最佳方法是什么? (if that is not the case I should add it at runtime). (如果不是这种情况,我应该在运行时添加它)。

I do not know in advance the name of the jar nor the classes in it. 我事先并不知道罐子的名字,也不知道里面的类。 A user can select it. 用户可以选择它。 The jar represents a runtime plugable component (a driver in my problem). jar表示运行时可插入组件(我的问题中的驱动程序)。

A pragmatic way: Class.forName("com.myclass") where com.myclass is a class that is inside (and only inside) your target jar; 一种实用的方法: Class.forName("com.myclass") ,其中com.myclass是一个在你的目标jar里面(并且只在里面)的类; if that throws a ClassNotFoundException , then the jar is not on you current classpath. 如果抛出ClassNotFoundException ,那么jar不在你当前的类路径上。

Bear in mind, though, that loading a jar at runtime is not very simple, you need to mess with classloaders. 但请记住, 在运行时加载jar并不是很简单,你需要搞乱类加载器。 As a rule (there are exceptions) that's not the way, you should be able to add explicitly the jar to the classpath before running. 通常(有例外)不是这样,你应该能够在运行之前将jar显式添加到类路径中。

Update: the updated question states that we don't know in advance "the name of the jar nor the classes in it"; 更新:更新的问题表明我们事先并不知道“罐子的名称或其中的类”; if so, this answer obviously does not apply. 如果是这样,这个答案显然不适用。 And the answer will depend on your specific classloader. 答案取决于您的特定类加载器。 In the usual scenario, AlexAndas' answer should work. 在通常情况下,AlexAndas的回答应该有效。

String classpath = System.getProperty("java.class.path")

this will give you what is on your classpath. 这将为您提供类路径中的内容。 you can then parse that for the jar file you want 然后,您可以解析所需的jar文件

try using this method: 尝试使用此方法:

public static void isJarExist(String jarName)
    {
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        if (classLoader instanceof URLClassLoader)
        {
            URLClassLoader classLoader2 = (URLClassLoader) classLoader;
            URL [] urls = classLoader2.getURLs();
            for (URL url : urls)
            {
                File file = new File(url.getFile());
                if (file.getPath().endsWith(jarName))
                {
                    System.out.println(jarName + " exist");
                    return;
                }
            }
            System.out.println(jarName + " not exist");
        }
    }
just pass your jar like isJarExist("myjar.jar), you can also modify it to return boolean as you wish 只需像isJarExist(“myjar.jar”)一样传递你的jar,你也可以根据需要修改它以返回boolean

You can't do that in general since there might be classloaders in your application that don't offer information of the jar-files. 您通常不能这样做,因为您的应用程序中可能存在不提供jar文件信息的类加载器。 In the case of URLClassLoaders, you can use the solution of Alex Adas 对于URLClassLoaders,您可以使用Alex Adas的解决方案

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

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