如何从 Scala 设置环境变量? [英] How to set environmental variable from Scala?

查看:89
本文介绍了如何从 Scala 设置环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 Scala 设置环境变量 (PATH).

I need to set environmental variable (PATH) from Scala.

我试过了:

val cmd = Seq("export", "PATH='bla'")
cmd.lines

但我遇到了错误:

java.io.IOException: Cannot run program "export": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at scala.sys.process.ProcessBuilderImpl$Simple.run(ProcessBuilderImpl.scala:68)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.lines(ProcessBuilderImpl.scala:140)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.lines(ProcessBuilderImpl.scala:106)
at .<init>(<console>:12)
at .<clinit>(<console>)
at .<init>(<console>:11)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:914)
at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:546)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:577)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:543)
at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:694)
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:745)
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:651)
at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:542)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:550)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:851)
at xsbt.ConsoleInterface.run(ConsoleInterface.scala:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:73)
at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:64)
at sbt.Console.console0$1(Console.scala:23)
at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)
at sbt.TrapExit$.executeMain$1(TrapExit.scala:33)
at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
    at java.lang.ProcessImpl.start(ProcessImpl.java:130)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
    ... 35 more

还有其他方法可以做到这一点吗?

Is there some other way to do that?

推荐答案

sys.process.Process 文档中的示例:

apply("java", new java.ioFile("/opt/app"), "CLASSPATH" -> "library.jar")

编辑以获得更有用的措辞:

Edit for more helpful verbiage:

也就是说,您在生成子进程时指定了 env.

That is, you specify the env when you spawn a child process.

当前进程的环境是只读的;参见System.getenv,或者比较抽象sys.propssys.env.

The environment of the current process is read-only; see System.getenv, or compare the abstractions sys.props and sys.env.

shell 增强了它赋予具有导出变量的子shell 的环境这一事实是一种shell 约定.参见bash参考中的3.7.4,例如:

The fact that a shell augments the environment it bestows on subshells with exported variables is a shell convention. See 3.7.4 in the bash reference, for example:

在调用时,shell 会扫描自己的环境并创建一个找到的每个名称的参数,自动将其标记为导出到子进程.执行的命令继承环境.出口和‘declare -x’命令允许添加参数和函数到环境中并从环境中删除.如果参数值在环境被修改,新值成为环境,更换旧的.任何继承的环境执行的命令由 shell 的初始环境组成,它的值可以在 shell 中修改,减去被删除的任何对unset 和 ‘export -n’ 命令,以及通过 export 和‘declare -x’命令.

On invocation, the shell scans its own environment and creates a parameter for each name found, automatically marking it for export to child processes. Executed commands inherit the environment. The export and ‘declare -x’ commands allow parameters and functions to be added to and deleted from the environment. If the value of a parameter in the environment is modified, the new value becomes part of the environment, replacing the old. The environment inherited by any executed command consists of the shell's initial environment, whose values may be modified in the shell, less any pairs removed by the unset and ‘export -n’ commands, plus any additions via the export and ‘declare -x’ commands.

这是我的答案第一次比重复的Daniel Sobral 答案更长.

This is the first time my answer was longer than the Daniel Sobral answer it duplicates.

这篇关于如何从 Scala 设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆