Correct gesture sharing for calling java. lang. Runtime. exec

  • 2021-12-09 08:54:34
  • OfStack

The correct gesture of directory calling java. lang. Runtime. exec Summary of two methods 1. Notes on Java, Runtime. exec () 1. Runtime. exec () There are four calling methods 2. Get the return value of program execution, 0 is success3. Get the result or error message of program execution 4. Runtime. exec ()

The correct gesture to call java. lang. Runtime. exec

Today, I wrote a program that used compilation and encountered problems.

When calling


runtime.exec("javac HelloWorld.java");

It runs perfectly, that is, it has generated. class.

And here we are


runtime.exec("java HelloWorld >> output.txt");

However, the output cannot be redirected, and even the output. txt file cannot be generated.

Test "echo hello > > 1. txt "is not allowed, it is a headache, so I read the information and found it

One misunderstanding is that str cannot be completely regarded as command executed by command line in exec (str). In particular, redirects cannot be included in str ' < ' ' > 'And pipe symbol'.

So, what should I do if I encounter such instructions? We continue to look down:

Two methods

One is to write the instruction into the script and invoke the script in runtime. exec (). This method avoids using exec (), which is also an idea.

There is another way to call the overloaded method of exec (): Let's focus on this method:

Let's take a look at the official doc [ > link < ] Give us the overloaded method:


public Process exec(String command) throws IOExecption
public Process exec(String command,String [] envp) throws IOExecption
public Process exec(String command,String [] envp,File dir) throws IOExecption
public Process exec(String[] cmdarray) throws IOExecption
public Process exec(String[] cmdarray,String [] envp) throws IOExecption
public Process exec(String[] cmdarray,String [] envp,File dir) throws IOExecption

Looking through its documents, we found that its overloading method 4. exec (String [] cmdarray) is the simplest and most suitable for us. Officially, 4. exec () has the same effect as executing 6. exec (cmdarray, null, null). So 5. exec. (cmdarray, null) is also like 1?

As a result, we can write this:


runtime.exec( new String[]{"/bin/bash", "-c", "java HelloWorld >> output.txt"} );
runtime.exec( new String[]{"/bin/bash", "-c", "java HelloWorld >> output.txt"} ,null );
runtime.exec( new String[]{"/bin/bash", "-c", "java HelloWorld >> output.txt"} ,null,null );

Note, however, that if you use java/home/path/HelloWorld, '/' will be parsed to ".", which will report "Error: The main class.home. path. HelloWorld could not be found or loaded.home".

Therefore, when the full path cannot be used, we need to change the policy under 1 and change the path to the working directory dir, such as:


File dir = new File("/home/path/");

Then, with its sixth overload method, pass dir as the third parameter:


String []cmdarry ={"/bin/bash", "-c", "java HelloWorld >> output.txt"}
runtime.exec(cmdarry,null.dir);

Of course, echo, ls and other commands are not restricted by '/'.

* For the standard use of BTW, exec () to get the return value, see: runtime. exec ()

Summary 1

When the command contains redirect ' < ' ' > The exec (String command) method is not applicable and needs to be executed using exec (String [] cmdArray) or exec (String [] cmdarray, String [] envp, File dir).

For example:


exec("echo hello >> ouput.txt");
exec("history | grep -i mvn");

It should read:


exec( new String[]{"/bin/sh","-c","echo hello >> ouput.txt"});
exec( new String[]{"/bin/bash","-c","history | grep -i mvn"},null);

Java Runtime. exec () Considerations

Runtime.exec() Used to execute external programs or commands

1. Runtime. exec () has four invocation methods


    * public Process exec(String command);
    * public Process exec(String [] cmdArray);
    * public Process exec(String command, String [] envp);
    * public Process exec(String [] cmdArray, String [] envp);

2. Get the return value of program execution, and 0 is success

You need to use the waitFor () function, such as


Process p = Runtime.getRuntime().exec("javac");
( Deal with .....)
int exitVal = p.waitFor();

3. Get the result or error message of program execution

Need to use BufferedInputStream and BufferReader to get it, otherwise the program will use hang

For example, use p. getErrorStream () to get the error message, and then output it:


runtime.exec("java HelloWorld >> output.txt");
0

4.Runtime.exec()

Is not equivalent to directly executing the command line command

Ah, I kind of suffered here. Runtime. exec () is very limited. For some commands, the contents of command line cannot be directly passed to exec () as String parameters.

Such as redirect commands. For example:


runtime.exec("java HelloWorld >> output.txt");
1

In this case, the second overload of exec is used, that is, the input parameter is String []:


runtime.exec("java HelloWorld >> output.txt");
2

Related articles: