Running a shell command in Scala code on Windows seems to require the full absolute path to the command


Cui Pengfei

When I try to run the shell command on the Mac it works as expected like this:

scala> import scala.sys.process._
import scala.sys.process._

scala> """protractor --version"""!
warning: there were 1 feature warning(s); re-run with -feature for details
Version 0.24.0
res12: Int = 0

scala>

However, if I do this on Windows, I get this:

scala> import scala.sys.process._
import scala.sys.process._

scala> """protractor --version"""!
warning: there were 1 feature warning(s); re-run with -feature for details
java.io.IOException: Cannot run program "protractor": CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)

It seems I have to do this on Windows:

scala> import scala.sys.process._
scala> """C:\Users\twer\AppData\Roaming\npm\protractor.cmd --version"""!
warning: there were 1 feature warning(s); re-run with -feature for details
Version 0.24.0
res11: Int = 0

scala>

I have to provide the full absolute path to the command.

But I'm sure the command is available in the path.

Is there anyway to avoid this?

Daniel Nixon

You can try the following:

val command = Seq("protractor", "--version")
val os = sys.props("os.name").toLowerCase
val panderToWindows = os match {
  case x if x contains "windows" => Seq("cmd", "/C") ++ command
  case _ => command
}
panderToWindows.!

Related


Why does the "ln" command require an absolute path?

cainiaofei I made a symlink using the command ln -s source target. In my first attempt, I used relative paths and I ended up with abroken symbolic link... While searching online, I read that I have to use absolute paths . I'm interested in why it needs an abso

Why does the "ln" command require an absolute path?

cainiaofei I made a symlink using the command ln -s source target. In my first attempt, I used relative paths and I ended up with abroken symbolic link... While searching online, I read that I have to use absolute paths . I'm interested in why it needs an abso

Why does the "ln" command require an absolute path?

cainiaofei I made a symlink using the command ln -s source target. In my first attempt, I used relative paths and I ended up with abroken symbolic link... While searching online, I read that I have to use absolute paths . I'm interested in why it needs an abso

Why does the "ln" command require an absolute path?

cainiaofei I made a symlink using the command ln -s source target. In my first attempt, I used relative paths and I ended up with abroken symbolic link... While searching online, I read that I have to use absolute paths . I'm interested in why it needs an abso

Windows Shell command to get full path of current directory?

username Is it possible to get the full path of the current working directory using a Windows command line command? Also, how can I store that path in a variable used in the batch file? Trevor Bramber Use cdit without arguments if you are using the shell direc

Windows Shell command to get full path of current directory?

username Is it possible to get the full path of the current working directory using a Windows command line command? Also, how can I store that path in a variable used in the batch file? Trevor Bramber Use cdit without arguments if you are using the shell direc

Snakemake: How to specify absolute path to shell command

Vkkodali I'm writing a snakemake rule that uses multiple commands like this: rule RULE1: input: 'path/to/input.file' output: 'path/to/output.file' shell: 'path/to/command1 {input} | /path/to/command2 | /path/to/command3 {output}' If /path/to/command1it'

Snakemake: How to specify absolute path to shell command

Vkkodali I'm writing a snakemake rule that uses multiple commands like this: rule RULE1: input: 'path/to/input.file' output: 'path/to/output.file' shell: 'path/to/command1 {input} | /path/to/command2 | /path/to/command3 {output}' If /path/to/command1it'

Snakemake: How to specify absolute path to shell command

Vkkodali I'm writing a snakemake rule that uses multiple commands like this: rule RULE1: input: 'path/to/input.file' output: 'path/to/output.file' shell: 'path/to/command1 {input} | /path/to/command2 | /path/to/command3 {output}' If /path/to/command1it'

Shell command to output binary absolute path

Dr. Strangelove Is there a shell command for outputting the absolute path of a specified program? I would like to know where the executable binary is. username Attempt to which lsdiscover the full path lsof the command

Snakemake: How to specify absolute path to shell command

Vkkodali I'm writing a snakemake rule that uses multiple commands like this: rule RULE1: input: 'path/to/input.file' output: 'path/to/output.file' shell: 'path/to/command1 {input} | /path/to/command2 | /path/to/command3 {output}' If /path/to/command1it'

Pass variable to command by absolute path

and I'm trying to import a certificate and CA for storage using certutil.exe in PowerShell. Here is my script: $appDir="C:\Program Files\My App" $certFilespec = $appDir + "\mycert.pfx" certutil -p '""' -importPFX $certFilespec $certFilespec = $appDir + "\myca.

Pass variable to command by absolute path

and I'm trying to import a certificate and CA for storage using certutil.exe in PowerShell. Here is my script: $appDir="C:\Program Files\My App" $certFilespec = $appDir + "\mycert.pfx" certutil -p '""' -importPFX $certFilespec $certFilespec = $appDir + "\myca.

Add absolute path option to command

Ewan Delanoy Given a shell command (let's call it "oldcommand"), is there a simple shell script newcommand.sh that behaves the same as oldcommand except it has a new "-location" option, e.g., newcommand -location path -nonfileoption1 x1 -fileoption2 x2 -file

Add absolute path option to command

Ewan Delanoy Given a shell command (let's call it "oldcommand"), is there a simple shell script newcommand.sh that behaves the same as oldcommand except it has a new "-location" option, e.g., newcommand -location path -nonfileoption1 x1 -fileoption2 x2 -file

Find running command in PATH?

Laurent I'm trying to run the command via, QProcessbut it doesn't work. My order is like this utility -someflag /path/to/file. utilityin the PATH, but QProcessthis environment variable doesn't seem to be used. The command just fails and I need to specify the f

VS Code - How to open file by full path in command palette?

Sergey Massipa I need some functions like: VS Code -> F1 (opens command palette) -> OpenFileFeature : /home/user/blablabla/code.py -> (Enter) -> (file opens in editor) In this case /home/user/blablabla/code.py can be an external file (relative to the current f

CD with full path to each command

newbie In a shell script, what's a faster or more efficient way to first Cd into a directory and then execute the command or write the full path? For example: it is better to write cd /directory1/then then mkdir subdirectory/or mkdir /directory1/subdirectory/.

CD with full path to each command

newbie In a shell script, what's a faster or more efficient way to first Cd into a directory and then execute the command or write the full path? For example: it is better to write cd /directory1/then then mkdir subdirectory/or mkdir /directory1/subdirectory/.

CD with full path to each command

newbie In a shell script, what's a faster or more efficient way to first Cd into a directory and then execute the command or write the full path? For example: it is better to write cd /directory1/then then mkdir subdirectory/or mkdir /directory1/subdirectory/.

Running Android Shell command from Java code returns empty output

gentlemen. Gaussian I'm trying to run a shell command ( netstat -an -pt, cat /proc/net/tcpto check for open TCP ports on an Android device, from my Java code). The device is non-rooted (on a rooted device, this is no problem). I found out that there are two ty

Running Android Shell command from Java code returns empty output

gentlemen. Gaussian I'm trying to run a shell command ( netstat -an -pt, cat /proc/net/tcpto check for open TCP ports on an Android device, from my Java code). The device is non-rooted (on a rooted device, this is no problem). I found out that there are two ty

Running Android Shell command from Java code returns empty output

gentlemen. Gaussian I'm trying to run a shell command ( netstat -an -pt, cat /proc/net/tcpto check for open TCP ports on an Android device, from my Java code). The device is non-rooted (on a rooted device, this is no problem). I found out that there are two ty

Running Android Shell command from Java code returns empty output

gentlemen. Gaussian I'm trying to run a shell command ( netstat -an -pt, cat /proc/net/tcpto check for open TCP ports on an Android device, from my Java code). The device is non-rooted (on a rooted device, this is no problem). I found out that there are two ty