I'm trying to run
docker-compose up -d
in Java application. Here's my code:
List<String> commands = new ArrayList<>();
commands.add("zsh");
commands.add("-c");
commands.add("docker-compose up --build");
Process dockerComposeCommand;
ProcessBuilder pb = new ProcessBuilder();
pb.command(commands);
pb.directory(new File(server_config.getDOCKERFILE_PATH()));
String path = System.getenv("PATH");
pb.environment().put("PATH","/usr/bin:"+path);
pb.redirectErrorStream(true);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
List<String> result = new ArrayList<>();
try {
dockerComposeCommand = pb.start();
dockerComposeCommand.waitFor();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(dockerComposeCommand.getInputStream()))) {
String line = reader.readLine();
while (line != null) {
result.add(line);
line = reader.readLine();
System.out.println(line);
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
However, the result was
zsh:1: command not found: docker-compose
I've checked my .zshrc file and add
export PATH="$PATH:/usr/local/bin"
to include docker-compose's path.
Did I miss something or docker-compose just can't simply execute by Java?
Any advice would be helpful!
Related
I am using ProcessBuilder to execute git command inside my java application. The output string is correct when the command is "git status" or "git branch", however the output was empty when the command is "git clone". I wonder how can I get the output string of "git clone". Thanks in advance.
try {
ProcessBuilder pb = new ProcessBuilder("git", "clone", "address");
Process pr = pb.start();
BufferedReader buf = new BufferReader(new InputStreamReader(pr.getInputStream()));
String cmdLine = "";
while ((cmdLine = buf.readLine()) != null) {
System.out.println(cmdLine);
}
} catch (Exception e) {
}
I am trying to use the apktool from a Java program. I'm using this for creating a web service. However this command does not run on the shell from the Java program.
String cmd = "apktool d /home/ridhima/Test.apk" ;
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while((line = reader.readLine()) != null)
{
System.out.print(line + "\n");
}
p.waitFor();
}
catch (IOException | InterruptedException e1) {
e1.printStackTrace();
}
The command works perfectly fine directly from the shell.
Thanks but it works fine now. Since apktool is a wrapper script, it is probably not being recognized through the java program. Extracting the apktool.jar directly works.
try {
ProcessBuilder pb = new ProcessBuilder("/home/ridhima/java/jdk1.8.0/bin/java", "-jar", "apktool.jar","d","test.apk");
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
p.waitFor();
}catch (IOException | InterruptedException e1) {
e1.printStackTrace();
}
You maybe should wait for the process to complete
String cmd = "apktool d /home/ridhima/Test.apk" ;
try {
Process p = Runtime.getRuntime().exec(cmd);
// You maybe should wait for the process to complete
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while((line = reader.readLine()) != null)
{
System.out.print(line + "\n");
}
}
catch (IOException | InterruptedException e1) {
e1.printStackTrace();
}
Or you can use ProcessBuilder for the same task
public class Main {
public static void main(String[] args) throws java.io.IOException, java.lang.InterruptedException {
// Create ProcessBuilder instance for UNIX command ls -l
java.lang.ProcessBuilder processBuilder = new java.lang.ProcessBuilder("ls", "-l");
// Create an environment (shell variables)
java.util.Map env = processBuilder.environment();
env.clear();
env.put("COLUMNS", "3");
processBuilder.directory(new java.io.File("/Users"));
java.lang.Process p = processBuilder.start();
}
}
I am trying to run mdb-export on a file I know exist in that directory. But it does not seem to execute. "ls-l" will so I am sure that the java code is working. The command will execute perfectly from bash.
The failing command is
/usr/bin/mdb-export -Q -d ';' -D '%Y-%m-%d %H:%M:%S' /home/jocke/viking.mdb resultat >> resultat.csv
private void runCommand() {
try {
String workingdirectory=System.getProperty("user.dir");
Runtime runtime = Runtime.getRuntime();
//Process process = runtime.exec("/usr/bin/mdb-export -Q -d ';' -D '%Y-%m-%d %H:%M:%S' /home/jocke/viking.mdb resultat >> resultat.csv");
Process process = runtime.exec("/usr/bin/mdb-export /home/jocke/viking.mdb resultat >> resultat.csv");
//
process.waitFor();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
You cannot use output redirection this way. Use a ProcessBuilder instead:
ProcessBuilder pb = new ProcessBuilder("/usr/bin/mdb-export", "/home/jocke/viking.mdb", "resultat");
File csv = new File("resultat.csv");
pb.redirectOutput(Redirect.appendTo(csv);
Process p = pb.start();
How to call .scpt(applescript) file from java and pass argument into scpt file from java using
Runtime.getRuntime.exec() method.
Process result = Runtime.getRuntime().exec(cmdArray);
String[] args = {"/usr/bin/osascript", "/Users/uname/Library/Preferences/WebApp/Local\\ Store/spawn/Terminal.scpt" "args1", "args2" "args3" "false"};
Process result = Runtime.getRuntime().exec(args);
I cannot check this on Mac but the following answer works on windows:
List<String> list = new LinkedList<String>();
list.add("java");
list.add("-version");
ProcessBuilder pb = new ProcessBuilder(list);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream())));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
} catch (Exception e) {
System.out.println(e);
}
You can also read:
Difference between ProcessBuilder and Runtime.exec()
http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
This question already has answers here:
Executing a Java application in a separate process
(9 answers)
Closed 7 years ago.
Is there a way to run this command line within a Java application?
java -jar map.jar time.rel test.txt debug
I can run it with command but I couldn't do it within Java.
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -jar map.jar time.rel test.txt debug");
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
You can also watch the output like this:
final Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");
new Thread(new Runnable() {
public void run() {
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ((line = input.readLine()) != null)
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
p.waitFor();
And don't forget, if you are running a windows command, you need to put cmd /c in front of your command.
EDIT: And for bonus points, you can also use ProcessBuilder to pass input to a program:
String[] command = new String[] {
"choice",
"/C",
"YN",
"/M",
"\"Press Y if you're cool\""
};
String inputLine = "Y";
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writer.write(inputLine);
writer.newLine();
writer.close();
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
This will run the windows command choice /C YN /M "Press Y if you're cool" and respond with a Y. So, the output will be:
Press Y if you're cool [Y,N]?Y
To avoid the called process to be blocked if it outputs a lot of data on the standard output and/or error, you have to use the solution provided by Craigo. Note also that ProcessBuilder is better than Runtime.getRuntime().exec(). This is for a couple of reasons: it tokenizes better the arguments, and it also takes care of the error standard output (check also here).
ProcessBuilder builder = new ProcessBuilder("cmd", "arg1", ...);
builder.redirectErrorStream(true);
final Process process = builder.start();
// Watch the process
watch(process);
I use a new function "watch" to gather this data in a new thread. This thread will finish in the calling process when the called process ends.
private static void watch(final Process process) {
new Thread() {
public void run() {
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
try {
while ((line = input.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");
import java.io.*;
Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");
Consider the following if you run into any further problems, but I'm guessing that the above will work for you:
Problems with Runtime.exec()
what about
public class CmdExec {
public static Scanner s = null;
public static void main(String[] args) throws InterruptedException, IOException {
s = new Scanner(System.in);
System.out.print("$ ");
String cmd = s.nextLine();
final Process p = Runtime.getRuntime().exec(cmd);
new Thread(new Runnable() {
public void run() {
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ((line = input.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
p.waitFor();
}
}
Have you tried the exec command within the Runtime class?
Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug")
Runtime - Java Documentation
Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");