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.