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");
Related
I'm trying to run console program inside windows cmd terminal. It works. But program which I run don't added to history.
String[] command = {"cmd.exe", "/C", "\"start cmd.exe /K \"" + commandLine + "\"\""};
ProcessBuilder builder = new ProcessBuilder().command(command);
start = builder.start();
I want to add ability to users to easily rerun this program lately.
Is there any way to add command to cmd.exe history?
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"));
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);
When I run commands from the console everything is OK:
sudo -u oracle fgrep ...
When I run the same command from Java code using ProcessBuilder, sudo doesn't work, and I need to set chmod to 775 or else I don't have permission to read logs.
Why doesn't this work? Is there an option to read logs without chmod 775?
Here is how I am using ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("bash", "-c", command);
Process shell = pb.start();
InputStream is = shell.getInputStream();
Since you say chmod 775 for log file it works, it's obvious your process doesn't have permission.
You can run your java with sudo:
sudo java ClassFileName
Or just add sudo as the first string in the array that you pass to bash process:
command[0]="sudo -u oracle ";
//command[1]=commandname;
//command[2...n]=Other params;
Assuming user oracle is in sudoers list and won't ask for password, this will run just like how it runs in commandline when you use sudo.
a. You don't need the bash -c, when you're executing the command you have a shell.
b. The command needs to be split on spaces and then passed into the ProcessBuilder as an array.
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.