0

I cd'd into a directory containing two Scala classes.

C:\Kevin\Workspace\>dir

09/08/2013  09:13 PM               331 Max.scala
09/08/2013  09:13 PM               459 Test.scala
               2 File(s)            790 bytes
               2 Dir(s)  72,008,863,744 bytes free

Then, I opened up REPL:

C:\Kevin\Workspace\>scala

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

scala> import java.lang.Runtime
import java.lang.Runtime

scala> val runtime = Runtime.getRuntime
runtime: Runtime = java.lang.Runtime@55dae09c

Then (with this post's help) I tried to compile my *.scala files via scalac as an argument to Runtime#exec, but it failed.

scala> runtime.exec("scalac Max.scala Test.scala")
java.io.IOException: Cannot run program "scalac": CreateProcess error=2, The system cannot find the file specifie
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
        at java.lang.Runtime.exec(Runtime.java:617)
        at java.lang.Runtime.exec(Runtime.java:450)
        at java.lang.Runtime.exec(Runtime.java:347)
        at .<init>(<console>:12)
        at .<clinit>(<console>)

How can I do the above in Scala?

Community
  • 1
  • 1
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

1 Answers1

2

I think this is not so much a Scala thing as it is a Windows thing, which I infer from your first code block.

On my Mac, I find this:

scala> Runtime.getRuntime.exec("scalac")
res2: Process = java.lang.UNIXProcess@24c63dac

On Windows, try providing the fully qualified path to scalac. If that doesn't work, consider using ProcessBuilder instead.

Vidya
  • 29,932
  • 7
  • 42
  • 70
  • Thanks, Vidya. Using the fully qualified path/absolute path to `scalac.bat` worked. I ran `scalac MyFile.scala`, but I didn't see `MyFile.class` output. Any idea why? – Kevin Meredith Feb 05 '14 at 14:41