这在Windows中相当简单,但在Linux中有点棘手。 我在用
Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "java -classpath /home/4/byz/Orc" });
Orc是具有主函数的类文件。 但没有任何反应。 有没有设置? 难道我做错了什么 ?
我希望Java程序在terminal中运行。
这是解决scheme:
String[] cmdArray = {"gnome-terminal","java -classpath /home/r/byz/ Orchestrator"}; try { Runtime.getRuntime().exec(cmdArray); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
所以基本上,我们必须使用gnome-terminal ..
我相信这已经解决了,但我会发表一个答案:
如何运行:
executeCommand(new String[]{"/bin/bash", "-c", "java -classpath /home/4/byz/Orc"});
方法:
public String executeCommand(String[] cmd) { StringBuffer theRun = null; try { Process process = Runtime.getRuntime().exec(cmd); BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream())); int read; char[] buffer = new char[4096]; StringBuffer output = new StringBuffer(); while ((read = reader.read(buffer)) > 0) { theRun = output.append(buffer, 0, read); } reader.close(); process.waitFor(); } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } return theRun.toString().trim(); }
让我知道这是否有帮助!