PocessBuilder 执行 glassfish 命令
PocessBuilder execute glassfish command
我尝试获取集群的实例列表,为此我使用 ProcessBuilder 执行命令:
asadmin list-instances --long=true
当我从 java 在 windows 中执行此命令时,它工作正常,但是当我在 [=31] 中的真实服务器中部署我的应用程序时=] 我得到一个例外:
Exception = java.io.IOException: Cannot run program "/bin/sh -c
'asadmin list-instances --long=true'": error=2, Aucun fichier ou
dossier de ce type
当我直接在服务器上执行这个命令时它工作正常吗?
我真的不明白这是什么问题
这是我的 Java 代码:
public String executerCommande() {
String line, line2 = "";
try {
//Windows command
//ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", command);
//I use this with quots and without quotes
//ProcessBuilder builder = new ProcessBuilder("/bin/sh -c asadmin list-instances --long=true");
ProcessBuilder builder = new ProcessBuilder("/bin/sh -c 'asadmin list-instances --long=true'");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (true) {
line = r.readLine();
if (line == null) {
break;
}
line2 += r.readLine() + "\n";
}
} catch (Exception e) {
System.out.println("Exception = " + e);
}
return line2;
}
谢谢。
使用运行时可能会有帮助
try {
String command = "asadmin list-instances --long=true";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
for (String s = br.readLine(); s != null; s = br.readLine()) {
System.out.println("line: " + s);
}
br.close();
p.waitFor();
System.out.println("command executed exited: " + p.exitValue());
p.destroy();
} catch (Exception e) {
System.out.println("Exception" + e);
}
我尝试获取集群的实例列表,为此我使用 ProcessBuilder 执行命令:
asadmin list-instances --long=true
当我从 java 在 windows 中执行此命令时,它工作正常,但是当我在 [=31] 中的真实服务器中部署我的应用程序时=] 我得到一个例外:
Exception = java.io.IOException: Cannot run program "/bin/sh -c 'asadmin list-instances --long=true'": error=2, Aucun fichier ou dossier de ce type
当我直接在服务器上执行这个命令时它工作正常吗? 我真的不明白这是什么问题
这是我的 Java 代码:
public String executerCommande() {
String line, line2 = "";
try {
//Windows command
//ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", command);
//I use this with quots and without quotes
//ProcessBuilder builder = new ProcessBuilder("/bin/sh -c asadmin list-instances --long=true");
ProcessBuilder builder = new ProcessBuilder("/bin/sh -c 'asadmin list-instances --long=true'");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (true) {
line = r.readLine();
if (line == null) {
break;
}
line2 += r.readLine() + "\n";
}
} catch (Exception e) {
System.out.println("Exception = " + e);
}
return line2;
}
谢谢。
使用运行时可能会有帮助
try {
String command = "asadmin list-instances --long=true";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
for (String s = br.readLine(); s != null; s = br.readLine()) {
System.out.println("line: " + s);
}
br.close();
p.waitFor();
System.out.println("command executed exited: " + p.exitValue());
p.destroy();
} catch (Exception e) {
System.out.println("Exception" + e);
}