有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中的apache exec上执行命令时遇到问题

我想使用ApacheCommonsExec库运行一个文件(a.txt)。但它给了我一个错误:问题是什么

Exception in thread "main" java.io.IOException: Cannot run program "a.txt" (in directory "C:\Users\sinaa\Desktop"): CreateProcess error=2, The system cannot find the file specified

我的代码是:

    public static void main(String[] args) throws IOException
{

    Executor exec = new DefaultExecutor();
    File temp=new File("C:\\Users\\sinaa\\Desktop");
    exec.setWorkingDirectory(temp);
    CommandLine s=new CommandLine("a.txt");
    exec.execute(s);
}

共 (1) 个答案

  1. # 1 楼答案

    根据您的评论:

    i want to open a.txt in notepad window when i compile the code

    要使用默认关联应用程序在Java中打开文件,请使用Desktop类:

    try{
        Desktop.open("a.txt");
    }catch(IOException io){
        io.printStackTrace();
    }