求助:java调用cmd并输入任意数

xjx09190 2017-03-15 03:41:31
我想用java调用打开cmd并输入任意想要输入的数,比如我调用cmd并输入hello world

求助各位大神
...全文
123 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
逗泥丸的平方 2017-03-16
  • 打赏
  • 举报
回复
想调cmd命令直说就好了,干嘛装的这么委婉
xjx09190 2017-03-15
  • 打赏
  • 举报
回复
引用 1 楼 qq_33350186 的回复:
import java.io.BufferedReader; import java.io.InputStreamReader; public class cmd { public static void main(String[] args) { BufferedReader br = null; try { Process p = Runtime.getRuntime().exec("cmd.exe /c ipconfig /all"); br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (Exception e) { e.printStackTrace(); } } } } }
可以啦 谢谢
李德胜1995 2017-03-15
  • 打赏
  • 举报
回复
Runtime run = Runtime.getRuntime(); run.exec("cmd命令");
xjx09190 2017-03-15
  • 打赏
  • 举报
回复
引用 1 楼 qq_33350186 的回复:
import java.io.BufferedReader; import java.io.InputStreamReader; public class cmd { public static void main(String[] args) { BufferedReader br = null; try { Process p = Runtime.getRuntime().exec("cmd.exe /c ipconfig /all"); br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (Exception e) { e.printStackTrace(); } } } } }
我如果要输入:netsh wlan connect name="503" 呢?ipconfig好像是cmd命令可以直接这么运行,但是我改成我想要输入的数就不行了!
追寻free 2017-03-15
  • 打赏
  • 举报
回复
import java.io.BufferedReader; import java.io.InputStreamReader; public class cmd { public static void main(String[] args) { BufferedReader br = null; try { Process p = Runtime.getRuntime().exec("cmd.exe /c ipconfig /all"); br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (Exception e) { e.printStackTrace(); } } } } }

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧