2014-02-17 93 views
2

我試圖得到兩個目錄的差異遞歸,並將它們註冊爲github問題。scala.sys.process.Process拋出RuntimeException

如果目錄有幾個文件,scala.sys.process.Process按我的預期工作。

➜ Desktop ls -la 
(snip) 
[email protected] 5 garbagetown staff 170 Sep 19 22:53 2.1.5 
[email protected] 5 garbagetown staff 170 Sep 19 11:26 2.2.0 
(snip) 
➜ Desktop ls -la 2.1.5/style 
(snip) 
[email protected] 1 garbagetown staff 1206 Sep 19 22:53 book.css 
[email protected] 1 garbagetown staff 278 Sep 19 22:53 external.png 
[email protected] 1 garbagetown staff 175 Sep 19 22:53 header-pattern.png 
[email protected] 1 garbagetown staff 12985 Sep 19 22:53 main.css 

➜ Desktop scala 
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21). 
Type in expressions to have them evaluated. 
Type :help for more information. 

scala> import scala.sys.process.Process 
import scala.sys.process.Process 

scala> Process("diff 2.1.5/style 2.2.0/style").!! 
res0: String = "" 

但是目錄有很多文件和目錄,scala.sys.process.Process拋出RuntimeException如下。

➜ Desktop ls -la 2.1.5/manual 
(snip) 
[email protected] 1 garbagetown staff 9139 Sep 19 22:53 Highlights.md 
[email protected] 1 garbagetown staff 2737 Sep 19 22:53 Home.md 
[email protected] 1 garbagetown staff 8444 Sep 19 22:53 Migration.md 
[email protected] 1 garbagetown staff 15202 Sep 19 22:53 Modules.md 
[email protected] 1 garbagetown staff 670 Sep 19 22:53 User-Groups-around-the-World.md 
[email protected] 1 garbagetown staff 937 Sep 19 22:53 _Sidebar.md 
[email protected] 3 garbagetown staff 102 Sep 19 22:53 about 
[email protected] 7 garbagetown staff 238 Sep 19 22:53 book 
[email protected] 8 garbagetown staff 272 Sep 19 22:53 detailledTopics 
[email protected] 9 garbagetown staff 306 Sep 19 22:53 gettingStarted 
[email protected] 9 garbagetown staff 306 Sep 19 22:53 hacking 
[email protected] 6 garbagetown staff 204 Sep 19 22:53 javaGuide 
[email protected] 16 garbagetown staff 544 Sep 19 22:53 sandbox 
[email protected] 7 garbagetown staff 238 Sep 19 22:53 scalaGuide 

➜ Desktop scala 
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21). 
Type in expressions to have them evaluated. 
Type :help for more information. 

scala> import scala.sys.process.Process 
import scala.sys.process.Process 

scala> Process("diff 2.1.5/manual 2.2.0/manual").!! 
java.lang.RuntimeException: Nonzero exit value: 1 
    at scala.sys.package$.error(package.scala:27) 
    at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.slurp(ProcessBuilderImpl.scala:131) 
    at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$bang$bang(ProcessBuilderImpl.scala:101) 
    at .<init>(<console>:9) 
    at .<clinit>(<console>) 
    at .<init>(<console>:7) 
    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:601) 
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734) 
    at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983) 
    at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573) 
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604) 
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568) 
    at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:756) 
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:801) 
    at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:713) 
    at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:577) 
    at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:584) 
    at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:587) 
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:878) 
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833) 
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833) 
    at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135) 
    at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:833) 
    at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:83) 
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96) 
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105) 
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala) 

我該如何解決這個問題?

回答

6

diff有一個稍微非傳統的溝通退出狀態的方式。通常,POSIX utils成功時返回0,錯誤時返回非零。如果找不到差異,則Diff返回0,如果發現差異,則返回1等。Process認爲這是執行中的錯誤並引發異常。所以你需要解決這個問題。

一種方式來做到這一點是使用這種方法,而不是(doc):

abstract def lines_!(log: ProcessLogger): Stream[String] 

它不會對非零錯誤狀態拋出。

例如:

Process("diff 2.1.5/manual 2.2.0/manual").lines_!.foreach(println) 
+1

哦,太棒了!我按照你的建議解決了這個問題。謝謝! – garbagetown

+0

'.lines_!'不贊成'.lineStream_!' –

相關問題