如何在Java类中执行cmd命令? [英] How to execute cmd command in Java class?

查看:264
本文介绍了如何在Java类中执行cmd命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Java代码调用cmd命令。我说:

I want to call cmd command in java code. I say:

String str ="C:/uploaded_files/111.txt";
Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c",str});
System.out.println(str);

也不要得到 111.txt 。奇怪的是,当这段代码在 jsp 中时,一切正常。可能是什么问题?

And dont get 111.txt. Its strange because when this code was in jsp all works fine. What can be wrong?

推荐答案

此代码有什么问题。一切正常。打开并显示文件111.txt

what's the problem with this code. It's perfectly working. opens and shows content of the file 111.txt

try {
    String str ="C:/uploaded_files/111.txt";
    Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c",str});
    System.out.println(str);
    } catch (Exception ex) {}

请检查路径是否正确以及是否正确目录和文件不会丢失或拼写

please check whether the path is correct and whether the directories and files are not missed or spelled

这篇关于如何在Java类中执行cmd命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆