Reputation: 1109
I have written a .bat file (as I am testing on Windows for now):-
echo Program Starts
mongoimport.exe --host 127.0.0.1 -d myDB -c things --type csv --file
D:\MOCK_DATA.csv --fields id,Name.f_name,Name.l_name,email,gender
echo Program Ends
I kept the .bat file in /bin folder of MongoDB.
The .bat file works fine if I call it directly from Windows Command Prompt.
ProcessBuilder pb = new ProcessBuilder("Path to my .bat File");
Process process = pb.start();
BufferedReader is = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = is.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
return builder.toString();
Following is the Java Console output:
echo Program Starts
Program Starts
mongoimport.exe --host 127.0.0.1 -d myDB -c things --type csv --file D:\MOCK_DATA.csv --fields id,Name.f_name,Name.l_name,email,gender
echo Program Ends
Program Ends
Upvotes: 0
Views: 1062
Reputation: 1109
I found the solution to the problem. Following lines helped in identifying the error:-
pb.redirectErrorStream(true);
pb.redirectOutput(new File("D:\\output.txt"));
The issue was that I didn't set the 'directory' where .bat file commands will run.
pb.directory(new File("\\MongoDB\\Server\\3.2\\bin"));
Upvotes: 0