如何从 Scala 设置环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18411478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set environmental variable from Scala?
提问by Jacob Jedryszek
I need to set environmental variable (PATH) from Scala.
我需要从 Scala 设置环境变量 (PATH)。
I tried this:
我试过这个:
val cmd = Seq("export", "PATH='bla'")
cmd.lines
but I got error:
但我有错误:
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(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(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(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(Console.scala:23)
at sbt.Console$$anonfun$apply$$anonfun$apply.apply$mcV$sp(Console.scala:24)
at sbt.TrapExit$.executeMain(TrapExit.scala:33)
at sbt.TrapExit$$anon.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?
有没有其他方法可以做到这一点?
回答by som-snytt
Example from doc for sys.process.Process:
来自 doc 的示例sys.process.Process:
apply("java", new java.ioFile("/opt/app"), "CLASSPATH" -> "library.jar")
Edit for more helpful verbiage:
编辑更有用的措辞:
That is, you specify the env when you spawn a child process.
也就是说,您在生成子进程时指定 env。
The environment of the current process is read-only; see System.getenv, or compare the abstractions sys.propsand sys.env.
当前进程的环境是只读的;见System.getenv,或比较抽象sys.props和sys.env。
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 增强了它赋予带有导出变量的子 shell 的环境这一事实是一个 shell 约定。请参阅 bash 参考中的 3.7.4,例如:
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.
在调用时,shell 会扫描自己的环境并为找到的每个名称创建一个参数,自动将其标记为导出到子进程。执行的命令继承环境。export 和'declare -x' 命令允许在环境中添加和删除参数和函数。如果环境中的参数值被修改,新值将成为环境的一部分,取代旧值。任何执行命令继承的环境包括 shell 的初始环境,其值可以在 shell 中修改,减去 unset 和 'export -n' 命令删除的任何对,以及通过 export 和 'declare -x' 进行的任何添加命令。
This is the first time my answer was longer than the Daniel Sobral answerit duplicates.
这是我的答案第一次比它复制的 Daniel Sobral 答案更长。
回答by djh
'export' isn't an executable, it's a shell built-in command. If you're trying to set the path in the parent shell, well, you can't. You can set it for a new shell that you execute. This is really more of a unix FAQ.
'export' 不是一个可执行文件,它是一个 shell 内置命令。如果您试图在父 shell 中设置路径,那么,您不能。您可以为您执行的新 shell 设置它。这实际上更像是一个 Unix 常见问题解答。

