How to run multiple mongodb commands from a java code. I need the mongodb commands to get executed in background when i run the java program. This program throws some exception "Exception in thread "main" java.io.IOException: Cannot run program "db.createCollection("employ")": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) . . .".
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class try1
{
public static void main(String[] args) throws Exception{
String command ="mongo";
String command1="db.createCollection(\"employ\")";
Process proc = Runtime.getRuntime().exec(command);
Process proc1 = Runtime.getRuntime().exec(command1);
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
proc.waitFor();
BufferedReader reader1 =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line1 = "";
while((line1 = reader1.readLine()) != null) {
System.out.print(line1 + "\n");
}
proc1.waitFor();
}
}
I need to run a set of mongo db commands from the java program. The program works with other terminal commands like "ls"(only single command). But there is problem if we give both command1 and command as "ls". Only one ls command gets executed. If trying with only one mongo db command say, "mongo" command does not get executed completely(program does not terminate). Is it because of "proc.waitFor()".