Apache commandline java.io.IOException

1. 报错代码

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.PumpStreamHandler;

import java.io.*;

public class ApacheCommandLineTest {
    public static void main(String[] args) {
        String cmd = "source /opt/app/app.cfg";
        CommandLine commandline = new CommandLine(cmd);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("test.log", true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        PumpStreamHandler streamHandler = new PumpStreamHandler(fileOutputStream, fileOutputStream, null);

        DefaultExecutor exec = new DefaultExecutor();
        exec.setExitValues(null);
        exec.setStreamHandler(streamHandler);
        try {
            int exitValue = exec.execute(commandline);  // exit code: 0=success, 1=error
            System.out.println(exitValue);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2. 异常信息

java.io.IOException: Cannot run program "source \opt\app\app.cfg" (in directory "."): CreateProcess error=2, The system cannot find the file specified
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
	at java.lang.Runtime.exec(Runtime.java:620)
	at org.apache.commons.exec.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:61)
	at org.apache.commons.exec.DefaultExecutor.launch(DefaultExecutor.java:279)
	at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:336)
	at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
	at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153)
	at com.xxl.job.core.handler.impl.ApacheCommandLineTest.main(ApacheCommandLineTest.java:36)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
	at java.lang.ProcessImpl.create(Native Method)
	at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
	at java.lang.ProcessImpl.start(ProcessImpl.java:137)
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
	... 7 more

3. 改为执行脚本文件,新建脚本文件test.sh

#!/bin/bash
source /opt/app/app.cfg
/opt/venv/bin/python /opt/app/test.py
echo "End!"
    public static void main(String[] args) {
        CommandLine commandline1 = CommandLine.parse("/bin/bash " + "test.sh");
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("test.log", true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        PumpStreamHandler streamHandler = new PumpStreamHandler(fileOutputStream, fileOutputStream, null);

        // exec
        DefaultExecutor exec = new DefaultExecutor();
        exec.setExitValues(null);
        exec.setStreamHandler(streamHandler);
        try {
            int exitValue1 = exec.execute(commandline1);  // exit code: 0=success, 1=error
            System.out.println(exitValue1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

4. 参考(https://www.programcreek.com/java-api-examples/?code=kinow/commons-sandbox/commons-sandbox-master/src/main/java/br/eti/kinoshita/commons/exec/Tests.java

 

(使用命令行方式执行无论怎么修改,一直遇到这个错误,无奈使用执行脚本的方式)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值