3

What is the best way to know at runtime if a particular JAR file is already in my classpath ? (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).

Sergio
  • 8,532
  • 11
  • 52
  • 94

4 Answers4

3
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

RNJ
  • 15,272
  • 18
  • 86
  • 131
  • 1
    Use java.io.File.pathSeparatorChar to get the OS dependent character that can be used in String.split(...) to break this up. – ingyhere Sep 09 '14 at 10:41
3

A pragmatic way: Class.forName("com.myclass") where com.myclass is a class that is inside (and only inside) your target jar; if that throws a ClassNotFoundException, then the jar is not on you current classpath.

Bear in mind, though, that loading a jar at runtime is not very simple, you need to mess with classloaders. 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.

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.

Community
  • 1
  • 1
leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • *"Bear in mind, though, that loading a jar at runtime is not very simple"* It only requires a handful of lines of code. What is so hard about that? – Andrew Thompson May 16 '13 at 11:44
  • 1
    @AndrewThompson The hard thing might be the pitfalls when loading the same class with different classloaders and then wondering why instances of these classes are not exchangeable. – mschenk74 May 16 '13 at 11:49
  • 1
    @leonbloy the restriction "where com.myclass is a class that is inside (and only inside) your target jar" cannot be fulfilled in the situation of this problem: "I do not know in advance the name of the jar nor the classes in it." – mschenk74 May 16 '13 at 12:00
  • @mschenk74 Yes, that restriction was added after my answer, I updated it. – leonbloy May 16 '13 at 13:46
1

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
Alex Adas
  • 210
  • 2
  • 6
  • 2
    but what if the system class loader is not a URLClassLoader ? (although probably this is not a common situation) – Sergio May 16 '13 at 11:43
  • I don't know, I also found this solution when searched how to load all classes from classpath. It works fine for me. I think that default implementation of ClassLoader is URLClassLoader, but I can be wrong. – Alex Adas May 16 '13 at 12:07
  • The ClassLoader is custom, for instance, when unit testing using Maven Surefire. In that case, you'll only find a few jars which are contrived by Surefire. Solution: Use the System Property (see RNJ's answer). – ingyhere Sep 09 '14 at 10:38
1

You can't do that in general since there might be classloaders in your application that don't offer information of the jar-files. In the case of URLClassLoaders, you can use the solution of Alex Adas

mschenk74
  • 3,561
  • 1
  • 21
  • 34