Various problems encountered by Java execution shell

Source: Internet
Author: User
Tags readline

1, to determine whether the child process execution end

Sometimes after we invoke the shell in Java, the subsequent operation can only continue if the process subprocess ends normally, so we need to determine when the process processes are terminated.

The Process class provides a waitfor () method. This method causes the current thread to wait until the process thread terminates.

Process.waitfor () has an int type return value, and when the return value is 0, the table process processes terminate normally. Otherwise, it's usually the case that the script executes (which is usually what I'm experiencing).

2, Process.waitfor () causes the current thread to block.

Sometimes we find that when the WAITFOR () method is invoked, the Java main thread blocks at WAITFOR () and the reason for the blocking is. Analyze:

After Java executes runtime.getruntime (). EXEC (Jyname), Linux creates a process that establishes three pipe connections to the JVM process, standard input streams, standard output streams, and standard error streams, assuming that the Linux process is constantly

Writes data to standard output streams and standard errors, while the JVM does not read, the data is stored in a Linux buffer, and when the buffer is full, the process cannot continue to write data, zombie, causing the Java process to die at waitfor ().

Could never end.

WORKAROUND: The Java process reads the standard output stream and the standard error stream continuously before waitfor ():

		Jyname Extract Script path String filename=filelist.get (0). ToString (). substring (filelist.get (0). ToString (). LastIndexOf (
		File.separator) +1);
		String jyname= "/etc/zxvf.sh" +filename;
			try {Process p0 = Runtime.getruntime (). exec (Jyname);
			Read standard output stream BufferedReader BufferedReader =new bufferedreader (New InputStreamReader (P0.getinputstream ()));
			String Line;
			while ((Line=bufferedreader.readline ())!= null) {System.out.println (line);
			//Read standard error stream BufferedReader Brerror = new BufferedReader (New InputStreamReader (P0.geterrorstream (), "gb2312"));
			String errline = null;
			while ((Errline = Brerror.readline ())!= null) {System.out.println (errline); //waitfor () Determines whether process processes are terminated and whether normal termination is determined by the return value.
			0 represents the normal termination of int c=p0.waitfor ();
				if (c!=0) {baseres.put ("desc", "Software upgrade failed: Perform zxvf.sh abnormal termination");
				Baseres.setreturnflag (FALSE);
			return baseres;
			The catch (IOException E1) {baseres.put ("desc", "Software upgrade failed: File decompression failed");
			Baseres.setreturnflag (FALSE); Return baseRes;
			catch (Interruptedexception E1) {baseres.put ("desc", "Software upgrade failed: File decompression failed");
			Baseres.setreturnflag (FALSE);
		return baseres; }

You can also start two additional threads after executing runtime.getruntime (). EXEC (jyname) Read standard error streams and standard output streams separately
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;

Import Java.io.InputStreamReader;

	public class Excutethread extends Thread {private String name;
	Public Excutethread (String name) {this.name = name;
			@Override public void Run () {try {Process P = runtime.getruntime (). exec (name);
			InputStream FIS = P.getinputstream ();
			Final BufferedReader brerror = new BufferedReader (New InputStreamReader (P.geterrorstream (), "gb2312"));
			InputStreamReader ISR = new InputStreamReader (FIS, "gb2312");
			Final BufferedReader br = new BufferedReader (ISR);
					thread T1 = new Thread () {public void run () {String line = null;
						try {while (line = Brerror.readline ())!= null) {//System.out.println (line);
					} catch (IOException e) {e.printstacktrace ();
						Finally {try {if (brerror!= null) brerror.close ();
						catch (IOException e) {e.printstacktrace ();}
					}
				}
			};
					Thread t2 = new Thread () {public void run () {String line = null;
						try {while (line = Br.readline ())!= null) {//System.out.println (line);
					} catch (IOException e) {e.printstacktrace ();
						Finally {try {if (br!= null) br.close ();
						catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
			}
					}
				}
			};
			T1.start ();

		T2.start ();
		catch (IOException E1) {//TODO auto-generated catch block E1.printstacktrace ();
 } finally {}}}

3, the shell script has associated script, note the path

The shell script is also to execute other scripts, this time is to pay attention to a path of the problem, which I have been looking for a long time a problem.

Process p=runtime.getruntime (). EXEC ("/etc/a.sh")

The Test.java class invokes the a.sh script in the ETC directory, a.sh script executes the b.sh script in the ETC directory, originally I wrote in the a.sh script./b.sh. In fact, Linux is not found b.sh, because we execute is in

Test.class directory down-/etc/a.sh so when a.sh executes./b.sh He will be in the Test.class directory to find, so can not find, so a.sh to write/etc/b.sh

4, Java continuous invocation of multiple scripts

		string[] cmd = {"/bin/sh", "C", "rm-rf/installation/upgrade/"; mkdir/installation/upgrade/"};
		Process p = runtime.getruntime (). exec (cmd);
		P.waitfor ();

Is the way of this array.


5, Java execution. sh script file when the direct write directory, for example: Runtime.getruntime (). EXEC ("/etc/a.sh")

Java needs to add "/bin/sh" for example when executing statements directly:

			String name= "/bin/sh cd/installation/upgrade/ip89_install_packet";
			Process p = runtime.getruntime (). exec (name);







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.