免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3448 | 回复: 5
打印 上一主题 下一主题

java如何运行linux中的shell命令! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-13 13:30 |只看该作者 |倒序浏览
java程序如何运行linux中的shell命令!

论坛徽章:
0
2 [报告]
发表于 2006-10-13 13:52 |只看该作者
Runtime.getRuntime().exec()

论坛徽章:
0
3 [报告]
发表于 2006-10-13 13:53 |只看该作者
Process pr=Runtime.getRuntime().exec("/path/to/command");

论坛徽章:
0
4 [报告]
发表于 2006-10-13 13:57 |只看该作者

  1. String lsString;
  2. Process process = Runtime.getRuntime().exec("ls");
  3. BufferedReader bufferedReader  = new BufferedReader( new InputStreamReader(process.getInputStream()));
  4. while ( (lsString=bufferedReader.readLine()) != null)
  5. System.out.println(lsString);  
  6. process.waitfor( );
复制代码

论坛徽章:
0
5 [报告]
发表于 2006-10-14 21:46 |只看该作者
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class test {
        public static void main(String[] args) {
                String lsString = null;
                Process process = null;
                try {
                        process = Runtime.getRuntime().exec("ls");
                        BufferedReader bufferedReader = new BufferedReader(
                                        new InputStreamReader(process.getInputStream()));
                        while ((lsString = bufferedReader.readLine())!= null)
                                System.out.println(lsString);
                        try {
                                process.waitFor();
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }

}



运行时提示错误:Exception in thread "main" java.lang.NoClassDefFoundError: test

请问问什么????

论坛徽章:
0
6 [报告]
发表于 2006-10-14 21:51 |只看该作者
扯远了哦~~java.lang.NoClassDefFoundError应该足够说明问题吧,“无法找到类定义错误”。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP