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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -