9

I have a classloader application that reads the system property sun.boot.class.path

But I've found in the JDK 9's release note that this property has been removed.

 System.getProperty("sun.boot.class.path"); // In JDK 9/10 this returns null 

But I still want to retrieve this property value in JDK 10. How can it be done?

I'm expecting a value like the following:

/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/resources.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jsse.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jce.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jfr.jar:/usr/lib/jvm/java-8-openjdk-amd64/jre/classes

FYI:

I don't want use the -Xbootclasspath option. Just need the path value.

Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
  • ¿Do you need the classpath at current time or the classpath at boot time? – mondaka May 21 '18 at 15:35
  • 6
    The boot class path is essentially removed since JDK 9 so this is why the undocumented sun.boot.class.path property is removed. -Xbootclasspath/a still works and JVMTI/java agents can append but that is about it. Maybe you could explain what you are trying to do? – Alan Bateman May 21 '18 at 15:41
  • 1
    This looks like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. Why do you need to know the boot classpath in the first place? – Didier L May 22 '18 at 09:02
  • I'm working on a VM like application that have model classes for some of the standard Java classes. If a class is not in the classpath then it loads it from the boot classpath. – Gayan Weerakutti May 23 '18 at 05:57
  • When you say "loads it" then do you mean Class.forName to load the classes into the VM or do you mean something else? If you are just looking for the class bytes then you can use the jrt file system to read the class bytes from the run-time image. All the details in JEP 220. – Alan Bateman May 23 '18 at 07:16
  • 1
    Definitely XY problem. Please explain **what** you want to achieve, not **how** you think it must be done technically. The JVM code handling the bootstrap classpath as well as the patch module path entries are [encapsulated in native C++ code](https://github.com/openjdk/jdk/blob/77bdc3065057b07a676b010562c89bb0f21512b7/src/hotspot/share/classfile/classLoader.cpp). Besides, the Java class loader hierarchy works as you described already: If a class is not found the current class loader, it will ask its parent for it, ultimately ending at the boot class loader. Actually, it is even parent first. – kriegaex Jul 03 '20 at 03:54
  • did you get the solution ? till jdk 8 we used to do it with Launcher.getBootstrapClassPath().getURLs() but now there is no replacement for the same purpose – Indra Yadav Sep 13 '21 at 17:57
  • @IndraYadav As mentioned above by others, there's no such thing as a boot classpath since JDK 9. But there are features like JRT which can be used based on the use case. – Gayan Weerakutti Sep 14 '21 at 05:25
  • @GayanWeerakutti thanks for reply, so can we get bootstrap class loader details since jdk 9 using JRT? – Indra Yadav Sep 15 '21 at 15:36
  • @IndraYadav Not sure about that. Look into https://openjdk.java.net/jeps/220 – Gayan Weerakutti Sep 16 '21 at 05:07

1 Answers1

-5

Delete the boot. it should work like that :

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

To understand more :

The System class has two methods used to read system properties: getProperty and getProperties.

The System class has two different versions of getProperty. Both retrieve the value of the property named in the argument list. The simpler of the two getProperty methods takes a single argument, a property key For example, to get the value of path.separator, use the following statement:

System.getProperty("path.separator"); The getProperty method returns a string containing the value of the property. If the property does not exist, this version of getProperty returns null.

The other version of getProperty requires two String arguments: the first argument is the key to look up and the second argument is a default value to return if the key cannot be found or if it has no value. For example, the following invocation of getProperty looks up the System property called subliminal.message. This is not a valid system property, so instead of returning null, this method returns the default value provided as a second argument: "Buy StayPuft Marshmallows!"

System.getProperty("subliminal.message", "Buy StayPuft Marshmallows!"); The last method provided by the System class to access property values is the getProperties method, which returns a Properties object. This object contains a complete set of system property definitions.

kadar
  • 90
  • 1
  • 1
  • 10
  • 2
    I need boot.classpath which contains the path to java bootstrap classes like the rt.jar – Gayan Weerakutti May 22 '18 at 03:28
  • 2
    @reversiblean there is no `rt.jar` in Java 9. But [as Didier L said](https://stackoverflow.com/questions/50451536/how-to-get-boot-class-path-in-jdk-9-or-later#comment87940523_50451536), it’s an xy problem. The class files are still available, e.g. `Object.class.getResourceAsStream("Object.class")` still works. – Holger May 24 '18 at 18:26
  • This is not the boot class path, that is the classpath ... facepalm. – Angel O'Sphere Oct 12 '19 at 11:37