简体   繁体   中英

execCMD(“cd ~/Desktop/Tmp/Server/ && java -javaagent:lib/Agent.jar -cp ./Dachuang/dachuang.jar:testcase test.Bank”);

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test{

    public static void execCMD(String cmd){
        StringBuilder result = new StringBuilder();
        try {
            Process p = Runtime.getRuntime().exec(cmd);
            BufferedReader bf = new BufferedReader(new
                    InputStreamReader(p.getInputStream()));
            String line = null;
            while((line=  bf.readLine())!=null) {
                result.append(line+"\n");
            }
        }catch(Exception e) {
            result.delete(0,result.length()-1).append("无效命令");
        };
        System.out.println(result.toString());
    }

    public static void main(String[] args) throws IOException {
        execCMD("cd ~/Desktop/Tmp/Server/ && java -javaagent:lib/Agent.jar -cp ./Dachuang/dachuang.jar:testcase test.Bank");
    }
}

Why I get null result on the console? I mean maybe I cannot execute this method right on some complex commands. I can exec("java --version") etc. some simple commands. I don't know how to pass parameter to this method, my command is [ cd ~/Desktop/Tmp/Server/ ] and [ java -javaagent:lib/Agent.jar -cp./Dachuang/dachuang.jar:testcase test.Bank ].

here is my screen cuts: no output

Java is not a shell. You can use ProcessBuilder . Instead of cd use directory(File) . And System.getProperty("user.home") instead of ~ . Something like

public static void main(String[] args) throws IOException, InterruptedException {
    File f = new File(System.getProperty("user.home"), "Desktop/Tmp/Server/");
    ProcessBuilder pb = new ProcessBuilder().directory(f);
    pb.inheritIO();
    pb.command("java", "-javaagent:lib/Agent.jar",
            "-cp", "./Dachuang/dachuang.jar:testcase", "test.Bank");
    Process p = pb.start();
    p.waitFor();
}

As Elliott says: Java is not a shell. And it doesn't understand shell command syntax. When you exec (or similar) a string with whitespace in it, Java crudely splits it into an array of Strings, ignoring any shell escapes or quotes. Then calls exec(String[]) which treats the first word as command name and the rest as the command's arguments.

If you really want to use shell command syntax (eg && ), and shell builtin commands (eg cd ), you can do this:

public static void main(String[] args) throws IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder();
    
    // Uncomment the next line if you want the child shell to inherit
    // this processes stdin, stdout and stderr streams

    // pb.inheritIO();       

    pb.command("/bin/bash", "-c" 
               "cd ~/Desktop/Tmp/Server/ && java -javaagent:lib/Agent.jar " +
               "-cp ./Dachuang/dachuang.jar:testcase test.Bank");
    Process p = pb.start();
    p.waitFor();
}

This is actually running an instance of bash to run a shell command line. It should work for any command line. (You would need to use normal Java string escapes for any backslash or double-quote characters in the command string. That is because this is a Java string literal...)


Elliott's answer is a more elegant solution for your example, but if the shell command line is more complicated, that approach gets too difficult.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM