0

I have an automation script written in java. Here is the code sample.

 String command = "adb shell date > date.txt";
 Process process = Runtime.getRuntime().exec(command);

When executing the code in windows its working fine but when I am running it in mac its not working properly. Later I found that, in windows this command execution is creating file in my local PC project directory. But in case of Mac its failed when trying to create the file in device directory and error showing. While from Mac terminal its working fine.

My question is why is not working in my mac machine and why file is not creating in my local PC?

0xAliHn
  • 18,390
  • 23
  • 91
  • 111
  • Did you connect any device with your PC? And make sure "adb devices" showing the device in the terminal – 0xAliHn May 04 '18 at 05:55
  • Shall I take it that this is the Android Debug Bridge? And that you've installed it on both your Windows system and your Mac? – kshetline May 04 '18 at 06:00
  • Yes. That's right. Also this working fine using terminal – 0xAliHn May 04 '18 at 06:04
  • Are you sure you are allowed to create a file from the JVM process (permission issues) ? What is the error you got ? Is it linked to `adb shell date` or is a `echo foobar > test.txt` do the same. Can you please try to be a bit more specific, this is too broad, to many possibilities. – AxelH May 04 '18 at 06:07
  • Yes, this is the first thing i checked when found the issue. Now main issue is this command trying to create the file in device directory and eventually failed. – 0xAliHn May 04 '18 at 06:09
  • How, this is on the device... So `adb` read the command shell as `date > date.txt` instead of `date` then process it to`date.txt`... You might want to check [How do I Pipe process output to a file](https://stackoverflow.com/questions/17089875/how-do-i-pipe-process-output-to-a-file-on-windows-and-jdk-6u45?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – AxelH May 04 '18 at 06:12

1 Answers1

0

The Java Runtime environment doesn't necessarily pick up the same environment variables, command path, and aliases that are at work when you use a macOS Terminal.

You might try entering which adb into the Terminal and see what path you get back, if any. If you do get a definite path, try executing that full path, not just adb, from Java.

kshetline
  • 12,547
  • 4
  • 37
  • 73
  • Tried but not worked. My problem is not for adb path its basically the problem when this command trying to create file in the device. why file is not creating in my local PC instead of device? – 0xAliHn May 04 '18 at 07:02