-2

I'm coding one application for my mongodb server in java, and I need to:

1 - Start my Mongo DB server (in terminal it is: "mongod")

2 - Access the Mongo and drop one collection (executing in terminal: "mongo", "use my_db" and "db.user_coll.drop()")

3 - Import the new collection (in terminal: "mongoimport --db my_db--collection user_coll --file user_coll"

I can make all this in terminal, but when i tried to run from Java, using ProcessBuilder or getRunTime.exec(), don't work :(

This is my code:

String DBPath ="";

if(args.length<=3) {
 System.out.println("Localizando collection em " + System.getProperty("user.home") + "/Desktop/");
 DBPath = System.getProperty("user.home") + "/Desktop/";
}
else { 
 String LastChar = 
 Character.toString(args[3].charAt(args[3].length()-1));
 if (LastChar == "/") {
  System.out.println("Localizando collection em " + args[3]);
  DBPath = args[3];
 }
 else{
  System.out.println("Localizando collection em " + args[3] + "/");
  DBPath = args[3]+ "/";
 }
}

File f = new File(DBPath + "user_coll");
if(f.isFile()) {
 System.out.println("Collection localizada, iniciando servidor MongoDB e derrubando a collection atual...");
 ProcessBuilder proc = new ProcessBuilder(new String[] {"/bin/bash", "-c", "mongod"});
 proc.start().waitFor();
 String echo = "cd "+ System.getProperty("user.dir")+ " && mongo < mongodbscript.js";
 Process proc2 = new ProcessBuilder(new String[] {"bash","-c", echo}).start();
 System.out.println("Importando nova collection...");
 Process proc3 = new ProcessBuilder(new String[] {"bash", "-c", "cd " + DBPath + " && mongoimport --db my_db --collection user_coll --file user_coll"}).start();
 System.out.println("Collection importada...");
}
else {
 System.out.println("Nova collection não localizada, iniciando servidor MongoDB com a collection existente");
 Process proc = new ProcessBuilder(new String[] {"bash", "-c", "mongod"}).start();
}
  • Please provide a [mcve]. Just stating that 'it doesn't work' is typically not enough for us to help you. – Andrew Fan Dec 24 '18 at 05:41
  • *"I can make all this in terminal"* So when you simply run `mongod` in terminal, does the terminal prompt actually come back? Or does the program keep running and print messages in the terminal? – Andreas Dec 24 '18 at 06:38
  • @Andreas when I run `mongod` in terminal the program keep running in background and print messages in the terminal. – Danilo Pereira Dec 26 '18 at 13:14
  • Can you run other commands in that terminal window when you run `mongod`? I'm asking about running `mongod`, not `mongod &`. My guess is not, which means it runs in the foreground, not in the background, and blocks the terminal. Since `mongod` doesn't stop, why would you expect it to stop when running from Java? `mongod` might even stall when the output buffer runs full, since you never consume its output. – Andreas Dec 26 '18 at 16:42
  • Hello @Andreas, I can't run others commands in terminal when I run `mongod`, so I need to open other terminal to run, for example `mongo`. Usually I run `mongod & mongo` (it open 2 terminals). About `mongod` I expect that run in background and don't stop running (at least don't stop until I finish run the code). I tried to run ProcessBuilder and RunTime without `mongod` , for example run the code: "cd ~/Desktop && mongoimport --db my_db --collection user_coll --file user_coll" but also not run. Thanks for helping me. – Danilo Pereira Dec 27 '18 at 18:27
  • @DaniloPereira You **know** that `mongod` continues running and won't stop, so why are you confused that [`waitFor()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Process.html#waitFor--) waits forever? Javadoc says: *"the calling thread will be **blocked** until the subprocess exits"* – Andreas Dec 28 '18 at 00:02
  • @Andreas You are right, it should be just `.start`! But my code is still not working. Don't run `mongod` or the others scripts yet. :( I want to do something like this code `os.system("mongod")` (from python), but in my java code. Thank you so much for your attention. – Danilo Pereira Dec 28 '18 at 22:17

1 Answers1

0

I have been working with this problem in MacOS and I found a workaround.

I add the complete path of the mongoimport command to the exec command:

Runtime r  = Runtime.getRuntime();

Process p = r.exec(path + command);

where path = "/Users/'myname'/Documents/mongoInstallation/bin" and comand = "mongoimport"

Dezso Gabos
  • 2,297
  • 5
  • 21
  • 29