在java程序的不同目录中运行Bash命令 [英] Running a Bash Command in different directory from a java program

查看:149
本文介绍了在java程序的不同目录中运行Bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序说明:

我正在编写一个Java程序,其初始当前目录是/ home / user / Desktop。我想在location / home / user / project /中运行一个bash命令du -s来查找该文件夹的大小,以便我可以在项目中使用该文件夹的大小。我无法发布整个代码,因为它有一些敏感数据。我只是发布了所需的代码。

I am writing a Java program which initial current directory is /home/user/Desktop. I want to run a bash command "du -s" in "location /home/user/project/" for finding the size of that folder so that I can to use the size of the folder in my project. I cannot post the entire code as it is having some sensitive data. I am just posting the code which is needed.

这就是我所做的: -

import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;

public class Exec_in_cur_dir {
    public static void main(String[] args) {
        try {
            StringBuffer output = new StringBuffer();
            String Command ="cd /home/user/project";   //Bash Command

            // create a process and execute 
            Process p = Runtime.getRuntime().exec(Command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";           

            while ((line = reader.readLine())!= null) {
                output.append(line + "\n");  
            } 

            System.out.println(output.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

所以如果我执行程序,输出是

So if I execute the program, the output is


无法运行程序cd:error = 2.

Cannot run program "cd": error=2.

但它正在处理其他所有命令,例如

But it is working every other commands like


  • ls

  • df -h

  • 等。

我的问题:

从上面的分析中推断出我的推断是我的java程序无法更改目录。那么如何更改目录路径并执行bash命令。

From the above analysis what I have inferred is my java program cannot be able to change directory. So how can I change the directory path and execute a bash command.

推荐答案

只是为了清楚地说明为什么 cd 命令不是像ar ls 这样的程序DF cd 由shell直接解释(并且shell会更改自己的工作目录,但是对于程序 shell执行 fork + exec 以将程序作为新进程执行。

Just to clearly state the why you cannot do that, the cd command is not a program like ar ls or df. cd is directly interpreted by the shell (and the shell changes its own working directory what will be inherited but child commands), while for programs, the shell executes a fork + exec to execute the program as a new process.

当您使用<$ c $时c> runtime.exex(),你启动一个新进程来执行一个程序(并且已经说过 cd 不是程序)。执行脚本(它们也不是程序)的常用解决方法是使用 bash -c命令。但它几乎不会用于你,因为你只会改变子进程的工作目录,而下一步 exec 仍然会有java程序的工作目录。

When you use runtime.exex(), you start an new process to execute a program (and as already said cd is not a program). A common workaround to execute scripts (they too are not programs), is to use bash -c command. But it would be almost not use for you, because you would change the working directory of the child process only, and next exec would still have the working directory of the java program.

使用 cd 命令实现此目的的唯一方法是更改​​shell中的工作目录,并拥有此shell执行命令。类似于:

The only way to achieve that using cd command would be to change working directory in a shell, and have this shell execute the command. Something like :

    String Command ="bash -c (cd /home/user/project; du -s )";   //Bash Command
    // create a process and execute 
    Process p = Runtime.getRuntime().exec(Command);

当然,正确的方法是更改​​ exec中的工作目录命令本身,避免启动中间shell:

But of course, the correct way it change working directory in exec command itself, avoiding to start an intermediary shell :

    String Command ="du -s";   //Bash Command
    // create a process and execute 
    Process p = Runtime.getRuntime().exec(Command, null, new File("/home/user/project");

这篇关于在java程序的不同目录中运行Bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆