java Runtime.getRunTime().exec & wildcards?
Asked Answered
W

5

15

i'm trying to remove junk files by using

Process p = Runtime.getRuntime().exec();

it works fine as long as i do not use wildcards, i.e. this works:

Process p = Runtime.getRuntime().exec("/bin/rm -f specificJunkFile.java");

while the following throws back "No such file or directory":

Process p = Runtime.getRuntime().exec("/bin/rm -f *.java");

i should be able to do all the nice things as outlined here, right?

Withy answered 21/1, 2010 at 18:57 Comment(0)
P
8

Might I suggest that you let Java do this for you?

  • Use file.listFiles() to get the list of files
  • Use file.getName().contains(string) to filter them if needed
  • iterate over the array performing file.delete()

Advantage: improved portability, saves the cost of an exec()

Poi answered 21/1, 2010 at 19:45 Comment(2)
How do you use wild cards in general. Leave the rm etc. Let's say I have my own binary and I have to launch that from a java code. How would you do that?Jeana
The first two bullets are the handling of the *, the third corresponding to the rm. So you need to know what part of the string is a filename pattern, but otherwise you can stitch together any command line using the list of the files that you get from listFiles() (provided your external command accepts multiple filenames, of course).Solleret
F
16

After a lot of searching I found this: http://www.coderanch.com/t/423573/java/java/Passing-wilcard-Runtime-exec-command

Runtime.exec(new String[] { "sh", "-c", "rm /tmp/ABC*" });
Fubsy answered 29/11, 2011 at 7:0 Comment(1)
This assumes that you are running on a system for which the command interpreter sh is available. Which makes it somewhat non-portable.Solleret
E
9

Those are Bash wildcards. They are interpreted within the Bash shell. You are running rm directly, so there is no shell to interpret the * as 'all files'.

You could use bash as the command. e.g.:

Runtime.getRuntime().exec("/path-to/bash -c \"rm *.foo\"")

Ehtelehud answered 21/1, 2010 at 19:1 Comment(1)
okay i'm doing: Runtime.getRuntime().exec("/bin/bash -c \"rm .foo\""); and i'm getting: ERROR>.foo": -c: line 0: unexpected EOF while looking for matching `"' this i do not understand... the escapes are correct aren't they?Withy
P
8

Might I suggest that you let Java do this for you?

  • Use file.listFiles() to get the list of files
  • Use file.getName().contains(string) to filter them if needed
  • iterate over the array performing file.delete()

Advantage: improved portability, saves the cost of an exec()

Poi answered 21/1, 2010 at 19:45 Comment(2)
How do you use wild cards in general. Leave the rm etc. Let's say I have my own binary and I have to launch that from a java code. How would you do that?Jeana
The first two bullets are the handling of the *, the third corresponding to the rm. So you need to know what part of the string is a filename pattern, but otherwise you can stitch together any command line using the list of the files that you get from listFiles() (provided your external command accepts multiple filenames, of course).Solleret
W
2

Runtime.getRuntime().exec(new String[] { "sh", "-c", "gunzip *.gz" });

Wendywendye answered 3/12, 2014 at 7:29 Comment(0)
H
0
  1. Use exec( String [] { cmd , arg1 , arg2...)

  2. Here's a full example to get the result as a String : Link.

Hayrick answered 1/6, 2013 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.