Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用参数查找批处理文件_Java_Batch File - Fatal编程技术网

在java中使用参数查找批处理文件

在java中使用参数查找批处理文件,java,batch-file,Java,Batch File,我有一个批处理文件(test.bat),其中包含命令copy num test.txt。我有一个java程序,当我运行它时,当我在web浏览器中输入URL时,例如http://localhost:8080/runbatchfileparam,我得到的结果是{“result”:true}或{“result”:false}。True表示java应用程序已正确执行批处理文件(test.txt在目录下创建) 我现在想做的是,我希望java程序能够接受参数。例如,用户应能够输入http://localho

我有一个批处理文件(test.bat),其中包含命令
copy num test.txt
。我有一个java程序,当我运行它时,当我在web浏览器中输入URL时,例如
http://localhost:8080/runbatchfileparam
,我得到的结果是
{“result”:true}
{“result”:false}
。True表示java应用程序已正确执行批处理文件(test.txt在目录下创建)

我现在想做的是,我希望java程序能够接受参数。例如,用户应能够输入
http://localhost:8080/runbatchfileparam/testabc.bat
作为web浏览器中的URL,如果找到并执行了testabc.bat文件(在桌面下),则结果应为
{“result”:true}
;如果未找到并执行testabc.bat文件,则结果应为
{“result”:false}
。(注意:所有批处理文件都是在桌面文件路径下创建的:
C:/Users/attsuap1/desktop

我已经编辑了我的控制器以接受一个参数,并完成了@PathVariable。在我的代码中,
fileName
变量指的是我在test.bat中创建的批处理文件名(test.bat,test123.bat)命令:
copy NUL test.txt
test123.bat中的命令:
copy NUL test123.txt
。但是,我一直将结果作为{“result”:false}。这意味着java程序无法找到批处理文件并执行它

这是我的密码:

RunBatchFile.java

public ResultFormat runBatch(String fileName) {

    String var = fileName;
    String filePath = "C:/Users/attsuap1/Desktop" + var;
    try {
        Process p = Runtime.getRuntime().exec(filePath);

        int exitVal = p.waitFor();

        return new ResultFormat(exitVal == 0);

    } catch (Exception e) {
        e.printStackTrace();
        return new ResultFormat(false);
    }
}
private boolean result;

public ResultFormat(boolean result) {
    this.result = result;
}

public boolean getResult() {
    return result;
}
public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
ResultFormat.java

public ResultFormat runBatch(String fileName) {

    String var = fileName;
    String filePath = "C:/Users/attsuap1/Desktop" + var;
    try {
        Process p = Runtime.getRuntime().exec(filePath);

        int exitVal = p.waitFor();

        return new ResultFormat(exitVal == 0);

    } catch (Exception e) {
        e.printStackTrace();
        return new ResultFormat(false);
    }
}
private boolean result;

public ResultFormat(boolean result) {
    this.result = result;
}

public boolean getResult() {
    return result;
}
public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
BatchFileController

private static final String template = "Sum, %s!";  

@RequestMapping("/runbatchfileparam/{param}")
public ResultFormat runbatchFile(@PathVariable("param") String fileName ) {
    RunBatchFile rbf = new RunBatchFile();
    return rbf.runBatch(fileName);
}
Application.java

public ResultFormat runBatch(String fileName) {

    String var = fileName;
    String filePath = "C:/Users/attsuap1/Desktop" + var;
    try {
        Process p = Runtime.getRuntime().exec(filePath);

        int exitVal = p.waitFor();

        return new ResultFormat(exitVal == 0);

    } catch (Exception e) {
        e.printStackTrace();
        return new ResultFormat(false);
    }
}
private boolean result;

public ResultFormat(boolean result) {
    this.result = result;
}

public boolean getResult() {
    return result;
}
public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
我需要编辑什么,或者我应该向代码中添加什么来实现我的目标?

在这一行之后:

String filePath = "C:/Users/attsuap1/Desktop" + var;
尝试打印
文件路径的内容
,我怀疑您会想出类似的方法:

C:/Users/attsuap1/Desktoptestabc.bat


嗨,谢谢你的帮助。你能再解释一下吗?我对这个很陌生,所以我觉得有点困惑