I am facing a weird issue with executing a system command from JAVA code.
Actually i want to get the Mac OSX
system information from my JAVA App.
For that im using
Runtime.getRuntime().exec("system_profiler -detailLevel full");
This is working fine.If i print the output,it is cool.
But i want to write this information to a plist
file for future use.For that im using the -xml argument of system_profiler
.like,
String cmd = "system_profiler -detailLevel full -xml > "+System.getProperty( "user.home" )+"/sysinfo.plist";
Process p = Runtime.getRuntime().exec(cmd);
Basically this should create a plist file in the current users home directory.
But this seems to be not writing anything to file.
Am i missing something here ?
Christian.K is absolutely correct. Here is a complete example:
If you weren't using a pipe (
|
) or redirect (>
), then you'd be OK withString cmd = "ls -l *.java"
, as in your original command.If you actually wanted to see any of the output in your Java console window, then you'd ALSO need to call
Process.getInputStream()
.Here's a good link: Running system commands in Java applications
My Java is more than rusty, so please be gentle. ;-)
Runtime.exec()
does not automatically use the shell to execute the command you passed, so the IO redirection is not doing anything.If you just use:
Then the string will be tokenized into:
Which also wouldn't work, because
-c
only expects a single argument.Try this instead:
Of course, you could also just read the output of your
Process
instance usingProcess.getInputStream()
and write that into the file you want; thus skip the shell, IO redirection, etc. altogether.