Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between System.getProperty("java.class.path") and getClassLoader.getURLs()?

Tags:

java

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?

like image 991
JoJo Avatar asked Sep 05 '13 01:09

JoJo


2 Answers

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 !!

like image 68
Sachin Thapa Avatar answered Oct 18 '22 10:10

Sachin Thapa


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.

like image 36
user207421 Avatar answered Oct 18 '22 09:10

user207421