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

System.getProperty("java.class.path") returns the path to my program. However, getClassLoader().getURLs() also provides me with a class path (see My other post: how to use getClassLoader )

What is the difference between these two methods?

+4
source share
3 answers

The main difference can be found in the fact that they return:

 getClassLoader.getURLs() 

Gets the URL search path for loading classes and resources. This includes the initial list of URLs specified by the constructor, as well as any URLs subsequently added by the addURL () method, see link

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

The path used to search directories and JAR archives containing class files. Class path elements are separated by a platform-specific character specified in the path.separator property, see link

Looking at the definition, here are the differences:

  • First returns an array of URLs, and the second returns String.
  • Firstly, any URLs added at runtime using the API will also be returned, and secondly, this will not include.

More or less, it depends on what you are trying to achieve, when you need to decide which one to choose.

Greetings!

+7
source

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

ClassLoader you get the urls (although not by the method you mention that does not exist) may not have been a system class loader. It could be, for example, URLClassLoader, which has nothing to do with the classpath.

+3
source

The snapshot in the dark will be that ClassLoader will need a class path to find what to load, getClassLoader () also calls its own method, the JVM probably grabs the class path and loads it into the class loader.

You access the same data in different ways.

0
source

Source: https://habr.com/ru/post/1500635/


All Articles