I would like to know whether there’s any functional difference between running my Java app with the -cp command line switch (supplying a folder or a list of jars), and implementing a class loader (for example, using the URLClassLoader class) to load classes.
// -cp approach java -jar -cp ~/folder/with/jars
The other approach:
// URLClassLoader approach. urls is an array that points to the .jars under ~/folder/with/jars URLClassLoader loader = new URLClassLoader(urls); loader.findClass(...);
Well, for one, using a class loader as in your example would mean hardcoding paths in the source, unlike with
Plus you’d be in charge of loading the classes yourself instead of letting the jvm resolve them. Looks like a lot of unnecessary extra code to me unless there’s a good reason to do that.
Another difference (and advantage of a class loader) is being able to load them on demand.