2013-08-23 14 views
7

環境変数(PATH)をScalaから設定する必要があります。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 

はそれを行うにはいくつかの他の方法はありますか?

+0

Scalaからプロパティを設定して別のプロセスを起動しようとしていますか、Scalaプログラムの終了後にプロパティを保持しようとしていますか? – joescii

+2

http://stackoverflow.com/q/9443190/1296806の重複していますが、これは以下のように簡潔な答えを示しています。 –

答えて

10

sys.process.Processのためのドキュメントからの例:より有用な言い回しのため

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

編集:

あなたは子プロセスを生成するときにENVを指定し、あります。

現在のプロセスの環境は読み取り専用です。 System.getenvを参照するか、または抽象度をsys.propssys.envと比較してください。

エクスポートされた変数を持つサブシェルに与えられた環境をシェルが強化するという事実は、シェルの規則です。たとえば、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.

これは私の答えは、それが複製the Daniel Sobral answerよりも長かった最初の時間です。

2

'export'は実行可能ではありません。シェルの組み込みコマンドです。親シェルでパスを設定しようとしている場合は、できません。実行する新しいシェル用に設定することができます。これは本当にunix FAQのほうが多いです。

関連する問題