user13800089
user13800089

Reputation:

scala get path for current module, not of the calling module

In an intelliJ project, "otherModule" is imported.

The "Main method" in one module, calls "some method" located in "otherModule"

"otherModule" contains resources, for which the path needs to be obtained by the calling module.

What code can be used to get the path to "otherModule", instead of returning the path of the calling module?

eg, if "otherModule" contains code: var thisModulePath = getClass.getResource("/").getPath

it actually returns the path of the calling module.

Upvotes: 1

Views: 181

Answers (1)

Erik van Oosten
Erik van Oosten

Reputation: 1738

On the JVM, resources are loaded by a class loader. Which class loader that is is determined by the Class instance you use.

For example:

// use the class loader which loaded the current class
getClass.getResourceAsStream("/resource.txt")

// use the class loader which loaded the class ClassInOtherModule
classOf[ClassInOtherModule].getResourceAsStream("/resource.txt")

Each class loader can find its own set of resources. However, in practice standalone programs (so not Spark, nor Application containers) use a single class loader for everything so most of the time it doesn't matter much.

'Finding the path of a module' is not really a thing. Modules are compile-time concepts that mostly do not translate to run-time.

Upvotes: 0

Related Questions