通过java运行Shell命令

通过java运行Shell命令,java,process,exec,Java,Process,Exec,我试图通过java运行cmd命令。“资源管理器”、“记事本”等命令正在运行,但“目录”、“路径”等命令不起作用。它引发异常,输出显示:- 执行命令java.io.IOException时出现问题:无法运行程序“path”:CreateProcess error=2,系统找不到指定的文件 boolean exc(String command){ Process p;String pingResult=""; try{ p=Runtime.ge

我试图通过java运行cmd命令。“资源管理器”、“记事本”等命令正在运行,但“目录”、“路径”等命令不起作用。它引发异常,输出显示:-

执行命令java.io.IOException时出现问题:无法运行程序“path”:CreateProcess error=2,系统找不到指定的文件

boolean exc(String command){
        Process p;String pingResult="";
        try{
            p=Runtime.getRuntime().exec(command);
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                pingResult += inputLine;
            }
            in.close();

            return true;
        }catch(Exception e){
            System.out.println("Problem in Executing Command "+e.toString());
            return false;
        }
    }

请参阅,如果我的代码中有任何问题。

程序用于列出指定路径中的目录
你可以使用下面的代码,你应该得到你想要的输出

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package checkapplicationisopen;

import java.io.*;
public class TestExec {
    public static void main(String[] args) {
        TestExec testExec=new TestExec();
        System.out.println(testExec.getDirectoryList("C:\\Documents"));
    }

    /**
     * 
     * @param path directory path which you want to retrieve the directory and files
     * @return the list of directories and Files with the size
     * 
     */
    public String getDirectoryList(String path)
    {
        String dirList="";
        try {
            Process p = Runtime.getRuntime().exec("cmd /C dir "+path);
            BufferedReader in = new BufferedReader(
                                new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                dirList+=line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dirList;
    }
}

用于列出指定路径中目录的程序
你可以使用下面的代码,你应该得到你想要的输出

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package checkapplicationisopen;

import java.io.*;
public class TestExec {
    public static void main(String[] args) {
        TestExec testExec=new TestExec();
        System.out.println(testExec.getDirectoryList("C:\\Documents"));
    }

    /**
     * 
     * @param path directory path which you want to retrieve the directory and files
     * @return the list of directories and Files with the size
     * 
     */
    public String getDirectoryList(String path)
    {
        String dirList="";
        try {
            Process p = Runtime.getRuntime().exec("cmd /C dir "+path);
            BufferedReader in = new BufferedReader(
                                new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                dirList+=line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dirList;
    }
}

命令的值是多少?
?我以“dir”的形式发出命令。请发布一个完整的工作示例,演示该问题。例外是关于“路径”,而不是“目录”。是的。。。。但是dir也不起作用。没有dir命令,dir/cd/pwd是cmd的内置函数,
命令的值是多少?
我给出的命令是“dir”,请发布一个完整的工作示例来演示这个问题。例外是关于“路径”,而不是“目录”。是的。。。。但是dir也不起作用。没有dir命令,dir/cd/pwd是cmd的内置函数