executing cd command and ffmpeg in java - java

How to run cd command(In linux Ubuntu) and ffmpeg on the changed directory. The following jsp program not working for me.
String cmd = "cd "+getServletContext().getRealPath("/")+"Files/videos/";
out.println(cmd);
ProcessBuilder pb = new ProcessBuilder(
"/bin/sh", "-c",
cmd + "&& ffmpeg -i nature.MP4");
Process p = pb.start();
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()) );
String line;
out.println("Meta-data...");
while ((line = in.readLine()) != null) {
out.println(line);
}
in.close();
Thanks in advance...

Related

Java Execute an external program and capture the output

So i try to Execute an external program and capture the output.
Currently the part that execute command works fine (using .bat file) and i can see the output on the cmd window.
The part that need to read the output not and it seemt that it stack inside my while
This is what i have try:
String[] command = {"cmd.exe", "/C", "Start", "d:\\batFile.bat"};
Process process = Runtime.getRuntime().exec(command);
InputStream is = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
Update
This is my .bat file:
docker volume create --name=mydb
timeout 3
docker run -d -p 27017:27017 -v mydb:/data/db mongo
timeout 3
Maybe you can try to redirect the output like this :
Process runtimeProcess1;
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C","Start","d:\\batFile.bat");
pb.redirectOutput(Redirect.INHERIT);
pb.redirectError(Redirect.INHERIT);
pb.redirectInput(Redirect.INHERIT);
runtimeProcess1 = pb.start();
int processComplete1 = runtimeProcess1.waitFor();

Start filebeat using a Java code?

I'm using filebeat to read some log files and I need to start filebeat using a Java program. And the filebeat commands are executed using the Windows PowerShell. I used the following code but it didn't work.
try {
ProcessBuilder b1 = new ProcessBuilder("powershell.exe", "/c", "cd \"C:\\Program Files\\Filebeat\" && ./filebeat -e -c filebeat.yml -d \"publish\"\\");
b1.redirectErrorStream(true);
Process p1 = b1.start();
BufferedReader r1 = new BufferedReader(new InputStreamReader(p1.getInputStream()));
String line1;
while (true) {
line1 = r1.readLine();
if (line1 == null) { break; }
System.out.println(line1);
}
} catch(Exception e) {
}
The below code worked.
ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\Filebeat\\filebeat.exe", "-c", "C:\\Program Files\\Filebeat\\filebeat.yml", "-e");
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = bri.readLine()) != null) {
System.out.println(line);
}
p.waitFor();
You're confusing PowerShell with CMD. The latter has a parameter /c, the former doesn't. Also, instead of using cd in the comandline you should simply set the working directory on the ProcessBuilder object, and each token of your commandline should be a separate array element.
Change your code to something like this and it should do what you expect:
ProcessBuilder b1 = new ProcessBuilder("cmd.exe", "/c", "filebeat", "-e", "-c", "filebeat.yml", "-d", "\"publish\"");
b1.directory(new File("C:\\Program Files\\Filebeat"));
b1.redirectErrorStream(true);
Process p1 = b1.start();

Java ProcessBuilder and bash

I am trying to execute a bash script from Java with ProcessBuilder
my code is :
Process createUser = buildProcess(
"/bin/su",
"-c",
"\"/opt/somedir/testdir/current/bin/psql",
"--command",
commandForUserCreation,
/* "'select * from users'", */
"--dbname",
"mydbname\"",
"myuser"
);
The problem is that I receive error:
/bin/su: unrecognized option '--dbname'
If I put echo in first place of my commands it prints correct command in bash and if I copy/paste this command it works!
Please, help me to resolve this issue.
You need to supply the whole command to execute by su as a single argument. Try this:
Process createUser = buildProcess(
"/bin/su",
"-c",
"/opt/vmware/vpostgres/current/bin/psql --command " + commandForUserCreation + " --dbname mydbname",
myuser
);
This is what I use in processBuilder:
String[] command = new String[] {"echo", "Hello"};
String workspace = "/bin/su";
System.out.println("Trying to run command: "+ Arrays.toString(command));
ProcessBuilder probuilder = new ProcessBuilder(command);
probuilder.directory(new File(workspace));
Process process = probuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:\n",Arrays.toString(command));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
I hope it helps.

How to run unix enq command in java program

Here is the unix command for adding a file to the queue.
enq -P QueueName:PrinterName FileName
Is it possible to run the above command using java.
Yes, it's possible using ProcessBuilder:
ProcessBuilder builder =
new ProcessBuilder("enq", "-P", "QueueName", "FileName");
Process process = builder.start();
InputStreamReader streamReader = new InputStreamReader(process.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
See: enq syntax
Process p = Runtime.getRuntime().exec(new String[]{"enq", "-P", "QueueName:PrinterName FileName"});

Execute mysql database import script from java on linux machine

I want to execute database import from .sql file from java program. My program is working fine on windows. But I am facing problem on linux machine.
Code -
try {
ProcessBuilder builder = new ProcessBuilder("mysql -u root -p password db-name < db_script.sql");
builder.redirectErrorStream(true);
Process pr = builder.start();
InputStream is = pr.getInputStream();
// Now read from it and write it to standard output.
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
I am getting - java.io.IOException: Cannot run program "mysql -u root
-p password db-name < db_script.sql":
java.io.IOException: error=2, No such file or directory
The above command is working fine on linux terminal.
Some one please advice me on this.
Thanks in advance
The < redirection is a shell thing. Try something like this:
ProcessBuilder builder = new ProcessBuilder("/bin/sh", "-c", "mysql -u root -p password db-name < b_script.sql");
UPDATE:
Otherwise, if you're using java 7+, you can do the redirection in java:
ProcessBuilder builder = new ProcessBuilder(
"mysql", "-u", "root", "-p", "password", "db-name");
builder.redirectInput(ProcessBuilder.Redirect.from(new File("b_script.sql")));
have you checked that your code is executed from proper dir??
that if your db_script.sql is inhome then you are running from home as
home>mysql -u root -p password db-name < db_script.sql
or provide full path of db_script.sql in the command
A simple way to achieve it.
String DBUSERNAME = "";
String DBUSERPASSWORD = "";
String sqlfilename = "";
PrintWriter writer = new PrintWriter("tmp.dmp", "UTF-8");
writer.println("mysql --user " + DBUSERNAME + " --password=" + DBUSERPASSWORD + " < " + sqlfilename);
writer.close();
runCommand("chmod 777 tmp.dmp");
runCommand("./tmp.dmp");
runCommand("rm tmp.dmp");
public static void runCommand(String command) throws IOException {
String s = "";
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = null;
BufferedReader stdError = null;
try {
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} finally {
if (stdInput != null) {
stdInput.close();
}
if (stdError != null) {
stdError.close();
}
}
}

Categories