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.
But when I call the .bat file using Java Program, the mongoImport doesn't
run. The program doesn't give any errors also. Here's my Java Program:-
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
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"));
Related
I am trying to run a powershell command which starts the tomcat service.Currently the command is working perfectly when executed directly through the windows powershell. However if i run the same command from java i get and error saying
Start-Process : A positional parameter cannot be found that accepts argument 'net'.
my powershell command is:
Start-Process -verb runas cmd -ArgumentList "/k net start Tomcat7"
my java code:
final String PS_COMMAND = " powershell.exe Start-Process -verb runas cmd -ArgumentList /k net start Tomcat7 " ;
Process p= Runtime.getRuntime().exec(PS_COMMAND);
BufferedReader BR=new BufferedReader(new InputStreamReader(p.getInputStream()));
String l;
while((l=BR.readLine()) != null){
System.out.print(l);
}
I see now you're using -verb runas to force a UAC prompt if needed.
In that case, it's a good approach but at least you can remove the cmd.exe indirection. The reason it isn't working as is though is really just because you didn't quote the values you're passing to the -ArgumentList parameter.
Try this:
final String PS_COMMAND = "powershell.exe Start-Process -verb runas net.exe -ArgumentList 'start Tomcat7'" ;
You are complicating things too much. There's no reason to run powershell.exe to run cmd.exe to run net.exe.
You should do one of the following:
Run PowerShell's command:
final String PS_COMMAND = "powershell.exe Start-Service Tomcat7" ;
Or run net.exe directly:
final String PS_COMMAND = "net.exe start Tomcat7" ;
in my Java Application i start a postgres process to backup my database.
Thread thread = new Thread(() ->{
Process p;
try{
p = Runtime.getRuntime().exec("cmd /c pg_dump -v -a -d "+database+" -h "+server+" -p "+port+" -U "+user+" -n public > " + file.getPath());
p.waitFor();
}catch(Exception e){
return false;
}
});
This works and the backup File was created. But the File size is 0KB while my Application is running. After i close the Java App - the backup file have its normal size.
I dont get it where the Problem is
I am sorry but its hard to me to explain this right.
in my application i try to create a Database dumpfile using pg_dump from PostgreSQL. If i start the dump process then the file will be create after i close the whole application.
i hope i explain it understandable
i tried to predefine the path to file. but it was the same error. I also used the ProcessBuilder intead.
The Problem was that i used the -v Option in the pg_dump command. Without this Option it works fine and the file will be created immediately.
It is option for verbose output. Here is my final Method which works fine:
Process process = new ProcessBuilder("pg_dump", "-a", "-d", database, "-h", server, "-p", port, "-U", user, "-f", file.getPath()).start();
process.waitFor();
I am guessing that it probably has something to do with the file and the way you handle it (my guess is based on file.getPath() in your code).
However, you haven't posted the whole code ... So, The the only thing I can do is guess.
Try the same code with predefined file name (lets say c:\\myfilename.dmp).
e.g.
p = Runtime.getRuntime().exec("cmd /c pg_dump -v -a -d "+database+" -h "+server+" -p "+port+" -U "+user+" -n public > c:\\myfilename.dmp"
How does it behave ?
I want to use "adb logcat -d > C:\Users\lenovo 01\Documents\android\sdk\platform-tools" command line command within my java code. this works directly in command prompt but it doesn't work within java code.
for example:
pb = new ProcessBuilder("adb", "logcat", "-d", ">", "C:\\android\\cellograf.txt");
pc = pb.start();
pc.waitFor();
System.out.println("written");
when I execute this, nothing happens. It writes only "written" but the file is empty. When I run this command in command prompt, it works correctly and writes all logcat output to that file. What am I doing wrong?
Redirecting output to a file is a feature of the command interpeter; it's not something that can be performed by the process itself. Try appending cmd /c to the beginning of your command:
pb = new ProcessBuilder("cmd", "/c", "adb", "logcat", "-d", ">", "C:\\android\\cellograf.txt");
Hello and excuse me for being new to java coding, but what I am trying to do is a java program that calls an executable program with some given parameters in ubuntu. I've found the code above in another stackoverflow question:
ProcessBuilder pb = new ProcessBuilder();
pb.command("bash", "-c", "./runCalculator.sh");
Process process = pb.start();
int retValue = process.waitFor();
But how can I cd to the executable file first and then execute the program, displaying its output, through java?
Thank you.
You have 2 options:
Use absolute path
pb.command("bash", "-c", "/path/to/runCalculator.sh");
Use ProcessBuilder directory method:
pb.directory(new File("/path/to"));
You don't have to cd anywhere, just specify the absolute path.
String path = "/home/Omen/runCalculator.sh";
pb.command("bash", "-c", path);
I would like to execute 2 or more commands sequentially through my Java Application using ProcessBuilder class. I Have tried multiple options as suggested in other responses/forums but no luck.
Here are the things I have tried:
ProcessBuilder processBuilder = new ProcessBuilder("ls", ";", "pwd");
Gives me following error :
Errors : ls: ;: No such file or directory
Errors : ls: pwd: No such file or directory
ProcessBuilder processBuilder = new ProcessBuilder("ls", "&&", "pwd");
Gives me similar error:
Errors : ls: &&: No such file or directory
Errors : ls: pwd: No such file or directory
List<String> command = new ArrayList<String>();
command.add("ls");
command.add(";");
command.add("pwd");
ProcessBuilder processBuilder = new ProcessBuilder(command);
Gives me following error:
Errors : ls: ;: No such file or directory
Errors : ls: pwd: No such file or directory
My OS is Linux/Mac-OSX.
Your approaches are equivalent to calling ls with the specified arguments. In Bash notation, what you're running is:
ls ';' pwd
ls '&&' pwd
If you want ls and pwd to be run as separate commands, you can use Bash (or another shell language) to wrap them into a single command:
bash -c 'ls ; pwd'
which you can call this way:
ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", "ls ; pwd");
I'm using ProcessBuilder to compile java program like this and it works for me:
ProcessBuilder b = new ProcessBuilder("cmd.exe","/c","cd " + dir,
" & javac " + mapClassName + ".java -cp " + pathToProjectClasses);
cmd.exe : it's start the command prompt.
\c : not sure what its doing but its important, you can see this link for more information (\? cmd commands)
cd + dir : is the first command and its change the directory to a certain path which is dir.
& : its mean start the second command after you finish the first one
javac : this word and the rest of the string is the second command
-cp : path to external class used by the class you want to compile.
So I have 2 commands, first one is cd command and second one is javac command and i execute them sequentially using &.
Sorry for my bad writing skills, if I haven't explained my code well please ask me about anything you want to know.
You could get the Process from ProcessBuilder.start() from the first command, wait using waitFor() and then launch the second one.