在程序中调用Win/Lin命令

博客展示了Java代码实现Shell命令执行的过程,定义了ShellUtils类,包含execute方法用于执行命令并返回结果。同时指出执行命令时可能出现的异常,如Cannot run program \cd\,原因是执行Linux和Windows命令有区别,并给出了不同系统的执行方式。

 

public class ShellUtils {

     public static void main(String[] args) {

           String str = ShellUtils.execute("cd");

           

           //String str =  ShellUtils.execute("/myfolder/firstShell.sh 10");

           System.out.println(str);

     }

     

     public static String execute(String command){

           Scanner input = null;

           String result = "";

           Process process = null;

           try {

                process = Runtime.getRuntime().exec(command);

                //等待命令执行完成

                process.waitFor();

                InputStream in = process.getInputStream();

                input = new Scanner(in);

                while(input.hasNext()){

                     result += input.nextLine() +"\n";

                }

                result = command + "\n" + result;

                

           } catch (Exception e) {

                e.printStackTrace();

           }finally {

                if(input != null){

                     input.close();

                }

                if(process != null){

                     process.destroy();

                }

           }

           return result;

     }

}

 

异常/错误:

 1.Cannot run program "cd": CreateProcess error=2,

原因:执行Linux命令和Windows命令有区别

执行Windows:.exec(cmd /c comand);

执行Linux命令:.exec(command);

 

****** Vitis HLS - High-Level Synthesis from C, C++ and OpenCL v2024.1 (64-bit) **** SW Build 5069499 on May 21 2024 **** IP Build 5075265 on Wed May 22 21:45:21 MDT 2024 **** SharedData Build 5076995 on Wed May 22 18:29:18 MDT 2024 **** Start of session at: Sun Mar 9 17:45:30 2025 ** Copyright 1986-2022 Xilinx, Inc. All Rights Reserved. ** Copyright 2022-2024 Advanced Micro Devices, Inc. All Rights Reserved. source F:/Xilinx_Vitis/Vitis_HLS/2024.1/scripts/vitis_hls/hls.tcl -notrace INFO: [HLS 200-10] For user '13578' on host 'garfieldgu' (Windows NT_amd64 version 10.0) on Sun Mar 09 17:45:33 +0800 2025 INFO: [HLS 200-10] In directory 'C:/Users/13578/AppData/Roaming/Xilinx/Vitis' WARNING: [HLS 200-2053] The vitis_hls executable is deprecated. Consider using vitis INFO: [HLS 200-10] Bringing up Vitis Unified IDE: F:/Xilinx_Vitis/Vitis/2024.1/bin/vitis.bat '\gnu\microblaze\lin\bin\' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 '\gnu\microblaze\nt\bin\' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 '\gnuwin\bin\' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 'cmd' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 '\gnu\microblaze\lin\bin\' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 '\gnu\microblaze\nt\bin\' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 '\gnuwin\bin\' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
03-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值