[英]How to exit the windows command prompt in which a java application got started
[英]Java Command Prompt Exit
我有一個關於Java中的Runtime.getRuntime.exec(“”)命令的簡短問題。
我正在嘗試通過SSH建立通往網關計算機的隧道:
String tunnel = "C:\\Program Files\\PuTTY\\putty.exe -ssh -X -P " + localPort + " " + tempUsername + "@" + localIP
+ " -pw " + tempPassword + " -L " + tunnelPort + ":" + gatewayName + ":"+gatewayPort;
Runtime.getRuntime().exec(tunnel);
除了令人討厭的事實,即命令提示符出現之外,這段代碼可以正常工作。
現在,我嘗試在執行代碼后退出提示:
String tunnel = "C:\\Program Files\\PuTTY\\putty.exe -ssh -X -P " + localPort + " " + tempUsername + "@" + localIP
+ " -pw " + tempPassword + " -L " + tunnelPort + ":" + gatewayName + ":"+gatewayPort;
String cmdCommands [] = {tunnel, "exit"};
Runtime.getRuntime().exec(cmdCommands);
是否可以像我一樣關閉命令提示符,或者有更好的方法嗎? (此代碼無效)
你需要要么使用一個實際的SSH庫,而不是直接膩子,如評論,或捕捉的IO流exec
Process p = Runtime.getRuntime().exec(cmdCommands);
InputStream is = p.getInputStream();
OutputStream os = p.getOutputStream();
os.write("exit\n");
出於平台原因,對硬代碼進行\\ n通常不是一個好主意,但是您知道了。 另外,您將需要選擇正確的OutputStream。 有幾個子類(緩沖等)可能有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.