java - Using ProcessBuilder with argument list as single String -
i try switch runtime.exec(command)
processbuilder
executing imagemagick's convert
java programm. options convert passed-in user string, cannot separate arguments pass them indvidually processbuilder
's constructor. actual command works on (unix) commandline is
convert -colorspace gray -enhance -density 300 in.pdf out.pdf
how can work:
public class demo { public static void main(string[] args) throws ioexception, interruptedexception { string convertoptions = "-colorspace gray -enhance -density 300"; // arguments passed in @ runtime processbuilder bp = new processbuilder(new string []{"convert",convertoptions,"in.pdf","out.pdf"}); process process = bp.start(); process.waitfor(); } }
currently, code justs run
you use regex params in code below:
string regex = "((\\s*|-)\\w+)"; pattern p = pattern.compile(regex); string convertoptions = "-colorspace gray -enhance -density 300"; // //arguments passed in @ runtime matcher matcher = p.matcher(convertoptions); list<string> pargs = new arraylist<string>(); pargs.add("convert"); while(matcher.find()) { string match = matcher.group(); if( match != null ) { pargs.add(match.trim()); } } pargs.add("in.pdf"); pargs.add("out.pdf"); try { processbuilder bp = new processbuilder( pargs.toarray(new string[]{}) ); process process = bp.start(); process.waitfor(); } catch(exception ex) { ex.printstacktrace(); }
Comments
Post a Comment