Je travaille sur une application et j'ai un problème avec la commande shell exécutée depuis l'application java. voici le code:Exécuter la commande shell à partir de Java
public String execRuntime(String cmd) {
Process proc = null;
int inBuffer, errBuffer;
int result = 0;
StringBuffer outputReport = new StringBuffer();
StringBuffer errorBuffer = new StringBuffer();
try {
proc = Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
return "";
}
try {
response.status = 1;
result = proc.waitFor();
} catch (InterruptedException e) {
return "";
}
if (proc != null && null != proc.getInputStream()) {
InputStream is = proc.getInputStream();
InputStream es = proc.getErrorStream();
OutputStream os = proc.getOutputStream();
try {
while ((inBuffer = is.read()) != -1) {
outputReport.append((char) inBuffer);
}
while ((errBuffer = es.read()) != -1) {
errorBuffer.append((char) errBuffer);
}
} catch (IOException e) {
return "";
}
try {
is.close();
is = null;
es.close();
es = null;
os.close();
os = null;
} catch (IOException e) {
return "";
}
proc.destroy();
proc = null;
}
if (errorBuffer.length() > 0) {
logger
.error("could not finish execution because of error(s).");
logger.error("*** Error : " + errorBuffer.toString());
return "";
}
return outputReport.toString();
}
mais lorsque je tente de exec commande comme:
/export/home/test/myapp -T "some argument"
monapp lit "some argument"
que deux separes arguments.but Je veux lire "some argument"
comme seul argument. lorsque j'exécute directement cette commande depuis le terminal, elle est exécutée avec succès. J'ai essayé '"some argument"'
, ""some argument""
, "some\ argument"
mais cela n'a pas fonctionné pour moi. comment puis-je lire cet argument comme un argument.
thx @midhat. Cela fonctionne pour moi. – Aykut
[Quand Runtime.exec() 'ne va pas: Naviguer autour des pièges liés à la méthode' Runtime.exec() '] (http://www.javaworld.com/article/2071275/core-java/fr -runtime-exec --- won-t.html) ou peut-être un avenir [requête de moteur de recherche] (https://duckduckgo.com/?q=When+Runtime.exec+wont+Navigate+yourself+around+ pièges + liés + à + la méthode + Runtime.exec +). –