Java tips: Use runtime.exe C to redirect local programs to call

Source: Internet
Author: User
Http://www.csip.com.cn/26/n-18126.html

Java tips: Use runtime.exe C to redirect local program calls

Java has the ability to use runtime.exe C to redirect local program calls. However, an error occurs when calling commands using redirection or pipelines. To solve this problem, run the command through shell. Calling a local program in Java will undermine the independence rules of the platform, but this is often required.

The following is an example of a simple class that shows how to run the LS command in UNIX:

Import java. Io. bufferedinputstream;

Import java. Io. ioexception;

Public class execls {

Static public void main (string [] ARGs ){

String cmd = "ls"

Try {

Process PS = runtime.getruntime(cmd.exe C (cmds );

System. Out. Print (loadstream (PS. getinputstream ()));

System. Err. Print (loadstream (PS. geterrorstream ()));

} Catch (ioexception IOE ){

IOE. printstacktrace ();

}

}

// Read an input-stream into a string

Static string loadstream (inputstream in) throws ioexception {

Int PTR = 0;

In = new bufferedinputstream (in );

Stringbuffer buffer = new stringbuffer ();

While (PTR = in. Read ())! =-1 ){

Buffer. append (char) PTR );

}

Return buffer. tostring ();

}

}

The important part of the above Code is the exec method and the command string ls. This program will output the list details under the running directory.

So what if you want to redirect these details to the file? The command line input should be written as ls> file, but when you change the CMD variable to this, an error will occur during running, as shown below:

/Bin/LS:>: no such file or directory

/Bin/LS: file: no such file or directory

The cause of the error is that the additional parameters are directly transferred to the LS command rather than to the actual command line. To solve this problem, create a cmd string into a string array and send the program you want to run to the command shell.

Therefore, change the cmd line to the following:

String [] cmd = {"sh", "-c", "ls> file "};

You will get a file named file, which contains the directory list. -The C parameter tells it to read the subsequent string, and the final parameter is the script you want to run.

In this case, the pipeline runs well, so you can change the command to the following method:

String [] cmd = {"/bin/sh", "-c", "/bin/ls | grep D> file "};

In this form, you will be given a file named file, which contains the LS entry containing D. Providing the full path of SH and LS is conducive to providing the security of your program.

Although using runtime.exe C is not the best way to create a platform-independent Java, it is sometimes necessary. This type of redirection technology can be used to limit runtime.exe C.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.