我想從Java執行bash命令(在Linux系統上)。 我想執行的命令是:runtime.exec參數中有空格
/usr/bin/convert -pointsize 24 label:'Just a test.' ./dummy.png
我第一次使用:
runtime.exec("/usr/bin/convert -pointsize 24 label:'Just a test.' ./dummy.png");
這個工作,但使用的「公正」作爲一個標籤,而不是:「只是一個測試」。我現在用的:
runtime.exec(new String []{"/usr/bin/convert", "-pointsize 24", "label:'Just a test.'", "./dummy.png"});
這個不給出錯誤,但是沒有生成dummy.png。
我怎麼能得到這個工作?
- 編輯:
隨着MadProgrammer的幫助我解決了這個問題。我認爲這是個好主意,以顯示工作示例:
import java.util.Scanner;
import java.io.*;
public class SystemCommand {
// publiC#########################
public static void main(String[] args) {
try {
doCommand(new String[] {"/usr/bin/convert",
"-background", BACKGROUND,
"-fill", FILL,
"-font", FONT,
"-pointsize", FONTSIZE,
"label:" + CITATION,
FILENAME});
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
// private ########################
final static String INDENT = " ";
final static String AUTHOR = "Bruce Lee";
final static String BACKGROUND = "NavyBlue";
final static String CITATION =
"\n" +
INDENT + "We all have time to either spend or waste" + INDENT + "\n" +
INDENT + "and it is our decision what to do with it." + INDENT + "\n" +
INDENT + "But once passed, it is gone forever." + INDENT + "\n" +
"\n" +
INDENT + AUTHOR + INDENT + "\n";
final static String FILENAME = "citation.png";
final static String FILL = "Yellow";
final static String FONT = "Bookman-DemiItalic";
final static String FONTSIZE = "24";
static Runtime runtime = Runtime.getRuntime();
private static void doCommand(final String[] cmd) throws IOException {
int i;
Process p;
Scanner sc;
p = runtime.exec(cmd);
sc = new Scanner(p.getInputStream());
while (sc.hasNext()) {
System.out.println(sc.nextLine());
}
}
}
要在Linux(難懂的已安裝)測試:
javac SystemCommand.java && java SystemCommand && display citation.png
當我的ProcessBuilder做一個例子,我將增加它也在這裏。
- 編輯
ProcessBuilder變種。在這種情況下,它不是非常有用,但是當你做更復雜的事情時,這將是一個更好的解決方案。
/*
I needed to write some code that executes something in the Bash shell.
This is not to difficult, but when there are spaces, things can become difficult.
But if you know how it works, then there is no problem.
It can be done Runtime.getRuntime().exec. In this example that would be good enough.
(See SystemCommand.java) But when you have a little bit more difficult things to do,
it is better to use ProcessBuilder. That I do in this example.
The solution is: create a String array in which the first entry is the command
and every parameter has his own entry (without quoutes).
This program is the equavelent of:
/usr/bin/convert -background "NavyBlue" -fill "Yellow" -font "Bookman-DemiItalic" -pointsize 24 label:"
I hope I shall always possess
firmness and virtue enough
to maintain
what I consider the most
enviable of all titles,
the character of an honest man.
George Washington" citation.png
To test it:
javac SystemCommandWithProcessBuilder.java && \
java SystemCommandWithProcessBuilder && \
display citation.png
To use this you need of-course Java, but besides that ImageMagick needs to be installed.
It is written for Linux.
*/
import java.io.*;
public class SystemCommandWithProcessBuilder {
// publiC#########################
public static void main(String[] args) {
try {
doCommand(new String[] {"/usr/bin/convert",
"-background", BACKGROUND,
"-fill", FILL,
"-font", FONT,
"-pointsize", FONTSIZE,
"label:" + CITATION,
FILENAME});
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
// private ########################
final static String INDENT = " ";
final static String AUTHOR = "George Washington";
final static String BACKGROUND = "NavyBlue";
final static String CITATION =
"\n" +
INDENT + "I hope I shall always possess" + INDENT + "\n" +
INDENT + "firmness and virtue enough" + INDENT + "\n" +
INDENT + "to maintain" + INDENT + "\n" +
INDENT + "what I consider the most" + INDENT + "\n" +
INDENT + "enviable of all titles," + INDENT + "\n" +
INDENT + "the character of an honest man." + INDENT + "\n" +
"\n" +
INDENT + AUTHOR + INDENT + "\n";
final static String FILENAME = "citation.png";
final static String FILL = "Yellow";
final static String FONT = "Bookman-DemiItalic";
final static String FONTSIZE = "24";
private static void doCommand(final String[] cmd) throws IOException {
BufferedReader br;
String line;
Process p;
p = new ProcessBuilder(cmd).start();
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
要在Linux測試(難懂的已安裝):
javac SystemCommandWithProcessBuilder.java && \
java SystemCommandWithProcessBuilder && \
display citation.png
我在發表兩個:https://github.com/CecilWesterhof/JavaExamples
閱讀(實施)*所有*的建議[當的Runtime.exec()不會(http://www.javaworld.com /jw-12-2000/jw-1229-traps.html)。這可能會解決問題。如果不是,它應該提供更多關於失敗原因的信息。然後忽略它引用'exec'並使用'ProcessBuilder'構建'Process'。還要將'String arg'分解爲'String [] args'來解釋其本身包含空格的參數。 –