2

I'm new to Scala programming. I have one .sh file. I want to run this file using Scala. I have tried solutions provided in the below link. But those are not working for me.

Execute shell script from scala application

I have tried simple echo command in scala REPL and it's working fine. But when I used the same line of code in Scala program I am getting java.io.IOException like below.

Exception in thread "main" java.io.IOException: Cannot run program "echo": CreateProcess error=2, The system cannot find the file specified

And my sample code looks like below

import java.io._
import sys.process._
object POC {  
  def main( args: Array[String]) {     
    val p = "echo 'hello world'".!!
    println("#############################################       "+ p)   
  }
}

EDIT: As per the Tom's response, I have modified above code like below and it's working fine now and getting Hello world in console.

    import java.io._
    import sys.process._
    object POC {  
      def main( args: Array[String]) {     
        val p = Seq("echo", "hello world")
        val os = sys.props("os.name").toLowerCase
        val panderToWindows = os match {
           case x if x contains "windows" => Seq("cmd", "/C") ++ command
           case _ => command
        }
        panderToWindows.!  
      }
    }

Now my exact issue is to execute my script.sh file. I have directory like below.

src
- bin
  - script.sh
- scala
  - POC.scala

script.sh code:

#!/bin
echo "Hello world"

And my POC.scala consists below code.

        import java.io._
        import sys.process._
        object POC {  
          def main( args: Array[String]) {     
            val command = Seq("/bin/script.sh")
            val os = sys.props("os.name").toLowerCase
            val panderToWindows = os match {
               case x if x contains "windows" => Seq("cmd", "/C") ++ command
               case _ => command
            }
            panderToWindows.!  
          }
        }

I didn't get any console output after executing above code. Please let me know if I missed anything. Thanks.

7
  • Which scala version are you using ? I ran the same code and got the correct output. I am using scala version 2.12.4 Commented Oct 24, 2018 at 11:15
  • @ChaitanyaWaikar I'm using Scala 2.11.2. Is it problem with version I'm using? It's working fine when I executed though scala REPL, but unable to execute though scala IDE. Commented Oct 24, 2018 at 11:34
  • 1
    Maybe you're facing this problem: stackoverflow.com/questions/24320446/… Commented Oct 24, 2018 at 12:23
  • 1
    Your code compiles and runs fine for me. Try putting in the full path to the echo program: /bin/echo (Or whatever it is on your system.) Commented Oct 24, 2018 at 23:52
  • @Tom - Thanks for your reply. It's working fine for simple linux command like 'echo'. But it's not working when I'm trying to execute .sh file. Any thoughts? Commented Oct 25, 2018 at 6:10

1 Answer 1

3

Assuming Linux is used, one can start with a simple "pwd"!, which will display the working directory, and then invoke the shell script using relative or absolute path. E.g.:

import sys.process._

object POC extends App{
  val path = "pwd".!!.trim
  println(path)
  s"$path/src/main/bin/test.sh".!
  "src/main/bin/test.sh".!
}

returns:

/home/user/temp/scala-shell-script
Hello shell
Hello shell

BTW, shell scripts usually have #!/bin/sh (not #!/bin) in the shebang line:

#!/bin/sh
echo "Hello shell"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. I'm trying to run it on windows. Is there any fix?
@Valli69, you can't run a shell script on Windows out of the box because shell interpreter is missing. You'll have to either rewrite the script as a Windows batch file or install an interpreter (see e.g. stackoverflow.com/questions/26522789/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.