Invoking cygwin commands on Windows host from Java Runtime.getRuntime().exec(command).
- build
mvn package- run via
CLASSPATH:
java -cp target\example.cygwin.jar;target\lib\* example.App -command ls -arguments /var/log -debug
this outputs
command: ls
arguments: /var/log
args: []
All optons:
ls
/var/log
null
Listing the dirs: /var/log
<OUTPUT>lastlog
setup.log
setup.log.full
sshd.log
</OUTPUT>
Done: ls /var/log
java -cp target\example.cygwin.jar;target\lib\* example.App -command ls -arguments /var/log -debug -bashthis outputs
command: ls
arguments: /var/log
args: []
All optons:
ls
/var/log
null
null
Listing the dirs: /var/log
<OUTPUT>lastlog
setup.log
setup.log.full
sshd.log
</OUTPUT>
Done: ls /var/log
This matches the output of the
$ ls -1 /var/log/command run from Cygwin:
lastlog
setup.log
setup.log.full
sshd.log- try non existent dir, to trigger error:
java -cp target\example.cygwin.jar;target\lib\* example.App -c ls -a /baddirListing the dirs: [/baddir]
Running the command: c:\cygwin\bin\bash.exe -c "/bin/ls -Q $0 $1 $2 $3 $4 $5 $6
$7 $8 $9" /baddirProcess exit code: 2
<ERROR>/bin/ls: cannot access "/baddir": No such file or directory</ERROR>- test on linux:
mvn package
java -cp target/example.cygwin.jar:target/lib/* example.App $(pwd)this produces
Listing the dirs: [/home/sergueik/src/selenium_java/basic-cygwin]
Running the command: ls -Q /home/sergueik/src/selenium_java/basic-cygwin
<OUTPUT>pom.xml
README.md
src
target</OUTPUT>- in version 0.2.0-SNAPSHOT introduced arguments parser. The invocation becomes:
java -cp target/example.cygwin.jar:target/lib/* example.App -command ls -arguments $(pwd)this outputs
Listing the dirs: [/home/sergueik/src/selenium_java/basic-cygwin]
Running the command: ls /home/sergueik/src/selenium_java/basic-cygwin
<OUTPUT>pom.xml
README.md
repo
src
target
</OUTPUT>also, controls options to be provided:
java -cp target/example.cygwin.jar:target/lib/* example.App -arguments $(pwd)complains with
Missing required argument: command
and quote is now an optional argument:
java -cp target/example.cygwin.jar:target/lib/* example.App -command ls -arguments $(pwd) -quoteListing the dirs: [/home/sergueik/src/selenium_java/basic-cygwin]
Running the command: ls -Q /home/sergueik/src/selenium_java/basic-cygwin
<OUTPUT>pom.xml
README.md
repo
src
target
</OUTPUT>one can pass multiple dirs separated by commans via -arguments:
java -cp target/example.cygwin.jar:target/lib/* example.App -command ls -arguments $(pwd),/tmp -quoteTo list regular Windows directories, use cygwin paths like
java -cp target\example.cygwin.jar;target\lib\* example.App -c ls -a /cygdrive/c/Users/sergueik/DesktopOn cygwin there is also a "bash" flag:
java -cp target\example.cygwin.jar;target\lib\* example.App -a /var/l*g -c ls -bthis results in
Running the command: c:\cygwin\bin\bash.exe -c "/bin/ls $0 $1 $2 $3 $4 $5 $6 $
7 $8 $9" /var/l*g
<OUTPUT>lastlog
setup.log
setup.log.full
sshd.log
</OUTPUT>while without the flag
java -cp target\example.cygwin.jar;target\lib\* example.App -a /var/l*g -c lsthe arguments are passed to ls.exe:
Running the command: c:\cygwin\bin\ls.exe /var/l*g
<OUTPUT>lastlog
setup.log
setup.log.full
sshd.log
</OUTPUT>print(123)pass the path to the script as arguments argument:
java -cp target\example.cygwin.jar;target\lib\* example.App -c c:\Python381\python.exe -a "%CD%\test.py" -d<OUTPUT>123
</OUTPUT>
Done: c:\Python381\python.exe c:\developer\sergueik\selenium_java\basic-cygwin\test.py
NOTE: trouble passing inline commands
java -cp target\example.cygwin.jar;target\lib\* example.App --command "c:\Python381\python.exe" --arguments "-c ""print 123"""Aborting after exception org.apache.commons.cli.MissingArgumentException: Missing argument for option: a
"c:\Python381\python.exe" -c "print( 123)"123
See Also (incorrect exaple disussion)[https://qna.habr.com/q/1098350]
One can override the default path to cygwin via
set CYGWIN_HOME=c:\cygwin