2013-08-23 18 views
7

Ho bisogno di impostare la variabile ambientale (PATH) da Scala.Come impostare la variabile ambientale da Scala?

ho provato questo:

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

ma ho ottenuto l'errore:

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 

C'è qualche altro modo per farlo?

+0

Si sta tentando di avviare un altro processo da Scala con le proprietà impostate o si sta tentando di ottenere le proprietà per persistere dopo l'uscita del programma Scala? – joescii

+2

Duplicato di http://stackoverflow.com/q/9443190/1296806, che ha una risposta tersa come la mia in basso. –

risposta

10

Esempio da doc per sys.process.Process:

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

Modifica per più utili verbosità:

Cioè, si specificare l'env quando si spawn un processo figlio.

L'ambiente del processo corrente è di sola lettura; vedere System.getenv o confrontare le astrazioni sys.props e sys.env.

Il fatto che una shell aumenti l'ambiente che conferisce alle sotto shell con le variabili esportate è una convenzione di shell. Vedere 3.7.4 nel riferimento bash, ad esempio:

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.

Questa è la prima volta che la mia risposta è stata più lunga the Daniel Sobral answer duplica.

2

"export" non è un file eseguibile, è un comando integrato della shell. Se stai cercando di impostare il percorso nella shell genitore, beh, non puoi. Puoi impostarlo per una nuova shell che esegui. Questa è davvero più una FAQ su Unix.

Problemi correlati