9

System.getProperty("java.class.path") returns the classpath of my program. However getClassLoader().getURLs() is also providing me with the classpath (see my other post: how to use getClassLoader)

What is the difference between the two mentioned ways?

Community
  • 1
  • 1
JoJo
  • 1,377
  • 3
  • 14
  • 28

3 Answers3

11

Main difference can be found in what they return:

getClassLoader.getURLs()

Returns the search path of URLs for loading classes and resources. This includes the original list of URLs specified to the constructor, along with any URLs subsequently appended by the addURL() method, see link

System.getProperty("java.class.path")

Path used to find directories and JAR archives containing class files. Elements of the class path are separated by a platform-specific character specified in the path.separator property, see link

Looking at definition, here are the differences:

  1. First one returns an array of URL whereas second one returns a String.
  2. First one will also return any URLs appended in runtime using API, second one will not include that.

More or less it depends on what you are trying to achieve when you have to decide which one to pick.

Cheers !!

Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42
  • 3
    'getClassLoader.getURL()' isn't legal Java, and when you add the missing parentheses doesn't correspond to a valid method of ClassLoader. It is difficult but not impossible to see what the OP is talking about, but I don't know where you got all this information about this non-existent method. – user207421 Sep 05 '13 at 06:16
  • @EJP - Thanks for pointing this out, have changed the method name, please see edit. – Sachin Thapa Sep 05 '13 at 14:17
2

One difference is that there is no such method as 'ClassLoader.getURL()'.

The ClassLoader you get the URLs from (although not by the method you mention, which is non-existent), may not have been the system class loader. It may have been for example a URLClassLoader, which has nothing to do with the classpath.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • @Thomas OP mentioned a method that does not exist. You may care to speculate as to what he may have meant. I am answering the actual question, as written. I don't see what your misperceptions about excitement have to do with it. – user207421 Dec 05 '13 at 21:41
-1

A shot in the dark would be that the ClassLoader would need the class path in order to find what to load, getClassLoader() also calls a native method, the JVM probably grabs the classpath directly and loads it into the class loader.

You're accessing the same data in different ways.