从Java调用MongoImport

时间:2016-06-08 07:33:17

标签: java shell mongoimport

我写了一个.bat文件(因为我现在在Windows上测试): -

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

  • 我将.bat文件保存在MongoDB的/ bin文件夹中。

  • 如果直接从Windows命令提示符调用它,.bat文件就可以正常工作。

  • 但是当我使用Java程序调用.bat文件时,mongoImport却没有 跑。该程序也没有给出任何错误。这是我的Java程序: -
    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();

以下是Java控制台输出: 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

1 个答案:

答案 0 :(得分:0)

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"));