如何使用 kotlin 打印当前的类路径?

SHo*_*oko 2 classpath kotlin

我需要在 Kotlin 程序中打印当前的类路径。有哪些替代方案?

我的应用程序打包在 jar 文件中,我使用 Kotlin 1.3.61 和 OpenJDK 8。

SHo*_*oko 5

我们可以用:

(java.net.URLClassLoader.getSystemClassLoader() as java.net.URLClassLoader).getURLs().forEach({println(it.getFile())})
Run Code Online (Sandbox Code Playgroud)

或者

println(System.getProperty("java.class.path"))
Run Code Online (Sandbox Code Playgroud)

但行为上有差异,更多详细信息请参见:What's the Difference Between System.getProperty("java.class.path") and getClassLoader.getURLs()?