ygizard
ygizard

Reputation: 51

Java Runtime.getRuntime().exec() with simple quotes

I have a problem with Java Runtime in Linux. I need to execute this command:

blastdbcmd -db '"mydb"' -info

So I use this Java code :

 String[] cmd = new String[] {blastdbcmd ,"-db", "\'","\"", mydb,"\"","\'","-info"}  
 Process p = Runtime.getRuntime().exec(cmd);
 p.waitFor(); 

but it doesn't work: every time I get a java.lang.NullPointerException exception... can anybody help me?

If I try with :

blastdbcmd -db "mydb" -info

it works, but the simple quotes are necessary if I have a command like

blastdbcmd -db "mydb mydb2" -info

Upvotes: 1

Views: 2899

Answers (1)

Stephen C
Stephen C

Reputation: 718708

If the blastdbcmd is expecting arguments with literal double quote characters them:

String[] cmd = new String[] {"blastdbcmd", "-db", "\"mydb mydb2\"", "-info"};

which is equivalent to

$ blastdbcmd -db "\"mydb mydb2\"" -info

If not:

String[] cmd = new String[] {"blastdbcmd", "-db", "mydb mydb2", "-info"};

which is equivalent to

$ blastdbcmd -db "mydb mydb2" -info

Upvotes: 1

Related Questions