ProcessBuilder vs Runtime.exec ()
Ich versuche, eine Frontend-App in Java zu erstellen, um Stapel-SVG-Konvertierungen mithilfe der Befehlszeilenfunktion von Inkscape durchzuführen. Ich nehme und aktualisiere den Code vonhttps: //sourceforge.net/projects/conversionsvg. Wie der ursprüngliche Entwickler Inkscape von @ aufgerufen h Runtime.getRuntime (). Exec (String). Das Problem, auf das ich stoße, sind einige Inkonsistenzen zwischen der Verwendung von MethodeA und MethodeB. Ich habe ein einfaches Java-Testprojekt erstellt, um die verschiedenen ausgeführten Aktionen zu demonstrieren.
CallerTest.java
package conversion;
import java.io.IOException;
public class CallerTest {
static String pathToInkscape = "\"C:\\Program Files\\Inkscape\\inkscape.exe\"";
public static void main(String[] args) {
ProcessBuilderCaller processBuilder = new ProcessBuilderCaller();
RuntimeExecCaller runtimeExec = new RuntimeExecCaller();
// methodA() uses one long command line string
try {
String oneLongString_ProcessBuilder = pathToInkscape + " -f \"C:\\test.svg\" -D -w 100 -h 100 -e \"C:\\ProcessBuilder-methodB.png\"";
String oneLongString_RuntimeExec = pathToInkscape + " -f \"C:\\test.svg\" -D -w 100 -h 100 -e \"C:\\RuntimeExec-methodA.png\"";
// processBuilder.methodA(oneLongString_ProcessBuilder);
runtimeExec.methodA(oneLongString_RuntimeExec);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// methodB() uses an array containing the command and the options to pass to the command
try {
String[] commandAndOptions_ProcessBuilder = {pathToInkscape, " -f \"C:/test.svg\" -D -w 100 -h 100 -e \"C:\\ProcessBuilder-methodB.png\""};
String[] commandAndOptions_RuntimeExec = {pathToInkscape, " -f \"C:/test.svg\" -D -w 100 -h 100 -e \"C:\\RuntimeExec-methodB.png\""};
processBuilder.methodB(commandAndOptions_ProcessBuilder);
// runtimeExec.methodB(commandAndOptions_RuntimeExec);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
RuntimeExecCaller.java
package conversion;
import java.io.IOException;
public class RuntimeExecCaller {
Process process;
// use one string
public void methodA(String oneLongString) throws IOException {
process = Runtime.getRuntime().exec(oneLongString);
}
, // use the array
public void methodB(String[] commandAndOptions) throws IOException {
process = Runtime.getRuntime().exec(commandAndOptions);
}
}
ProcessBuilderCaller.java
package conversion;
import java.io.IOException;
public class ProcessBuilderCaller {
Process process;
// use one string
public void methodA(String oneLongString) throws IOException {
process = new ProcessBuilder(oneLongString).start();
}
// use the array
public void methodB(String[] commandAndOptions) throws IOException {
process = new ProcessBuilder(commandAndOptions).start();
}
}
Ergebni
BeidemethodA (String) Anrufe funktionieren, aber wennmethodB (String []) heißt Inkscape wird gestartet und die Argumente werden falsch übergeben. NachmethodB (String []) wird ausgeführt Ich erhalte einen Inkscape-Fehlerdialog für jedes Sprichwort
Die angeforderte Datei konnte nicht geladen werden -f C: /test.svg -D -w 100 -h 100 -e C: \ RuntimeExec-methodB.png
Die angeforderte Datei konnte nicht geladen werden -f C: /test.svg -D -w 100 -h 100 -e C: \ ProcessBuilder-methodB.png
und wenn ich im Dialogfeld auf Schließen klicke, wird Inkscape mit einem neuen leeren Dokument angezeigt. Also, ich schätze, ich habe ein paar Fragen:
Was ist der Unterschied zwischen Runtime.getRuntime (). Exec (String) und Runtime.getRuntime (). Exec (String [])?
JavaDoc sagt, dass Runtime.exec (String) Anrufe Runtime.exec (Befehl, null) (welches ist Runtime.exec (String cmd, String [] envp)) was wiederum @ aufru Runtime.exec (cmdarray, envp) (welches ist Runtime.exec (String [] cmdarray, String [] envp)). Also, wenn Runtime.getRuntime (). Exec (String) ruft an Runtime.exec (String []) Wie auch immer, warum erhalte ich unterschiedliche Ergebnisse, wenn ich unterschiedliche Methoden verwende?
Passiert hinter den Kulissen etwas, bei dem Java die Umgebung abhängig von der aufgerufenen Methode unterschiedlich einrichtet?