Self-modifying classpath in Scala script?

I am trying to replace a bunch of Linux shell scripts with Scala scripts.

One of the remaining issues is how to scan the entire JAR directory and put them in the classpath. This is currently being done in a shell script before calling the Scala JVM. I would like to completely remove the shell script.

Is there an elegant Scala idiom for this?

I found this other question, but in Java, it seems you shouldn't mess with it: How to change CLASSPATH in Java?

+3
source share
3 answers

JVM wild-card . /foo/bar - , JAR, , /foo/bar/* , JAR .

, , , .

+6

Um, , java. ?

scala -cp '/foo/bar/*'
+2

This script recursively scans recursively lib:

import java.io.File
import java.util.regex.Pattern

def cp(root: File, lib: File): String = {
  var s = lib.getAbsolutePath.replaceFirst(
    Pattern.quote(root.getAbsolutePath) + File.separator + "*", "") +
    File.separator + "*"
  for (f <- lib.listFiles; if f.isDirectory)
    s += File.pathSeparator + cp(root, f)
  s
}

For instance:

/project
   lib
   |__dep
   |__dep2
     |__dep3

You call:

var f = new File("/path/to/project")
cp(f, f)

Result:

/*:lib/*:lib/dep2/*:lib/dep2/dep3/*:lib/dep/*
0
source

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


All Articles