Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i do getRuntime().exec with asterisk?

Tags:

java

shell

This doesn't work,

Runtime.getRuntime().exec("stat /*");

nor this;

Runtime.getRuntime().exec(new String[] {"stat", "/*"})

Is there any way around it ?

Thanks,

like image 723
Devrim Avatar asked Jan 23 '23 19:01

Devrim


1 Answers

The asterisk is expanded by the shell (this is called globbing). So you actually want to execute the /bin/sh executable (most likely - substitute another shell here if required), and invoke stat /* from that. e.g. execute:

/bin/sh -c "stat /*"

from your Java process. -c specifies that /bin/sh executes whatever is in the string following the -c.

Alternatively you could perform the /* expansion yourself by finding all the files in the root directory in Java, and then pass those as args to stat.

like image 62
Brian Agnew Avatar answered Jan 25 '23 09:01

Brian Agnew