Java Execution Shell command

Source: Internet
Author: User

Package com.pms.util;

Import Java.io.File;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.ArrayList;
Import java.util.List;

Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;

/**
* Execute Linux command
*
* @author Mars
*
*/
public class Executelinux {

log4j Instance Name
Private static final Log log = Logfactory.getlog (executelinux.class
. Getcanonicalname ());

/**
* Execute the Linux command and output the results
*
* @param cmd
* @throws RuntimeException
* @throws IOException
*/
public static void Execute (string[] cmd) throws Exception {
InputStream errorstream = null;
InputStream inputstream = null;
Process PS = null;
try {
string[] cmd = new string[] {"/bin/sh", "-C", "LS"};
PS = Runtime.getruntime (). exec (CMD);
Log.debug ("Start executing local thread");

Errorstream = Ps.geterrorstream (); Stream of correct results
InputStream = Ps.getinputstream (); Stream of error results
Printerror (Errorstream);
Printinfo (InputStream);

} catch (Exception e) {
Log.error ("Execute Linux Command error ...", e);
throw new Exception ("Execute Linux Command error ...");
}finally{
if (errorstream! = null) {
Errorstream.close ();
}
if (InputStream! = null) {
Inputstream.close ();
}

Ps.destroy ();
}

}

/**
* Print correct information
* @param errorstream
* @throws IOException
*/
private static void Printinfo (InputStream inputstream) throws ioexception{

StringBuffer sb = new StringBuffer ();
int line=0;
while (line = Inputstream.read (new byte[1024]))! =-1) {
Sb.append ((char) line). Append ("\ n");
}
String result = Sb.tostring ();
System.out.println ("# # # #正确信息 ######:" +result);
Log.debug ("# # # #正确信息LOG ######:" +result);
}

/**
* Print error message
* @param errorstream
* @throws IOException
*/
private static void Printerror (InputStream errorstream) throws ioexception{

StringBuffer sb = new StringBuffer ();
int line=0;
while (line = Errorstream.read (new byte[1024]))! =-1) {
Sb.append ((char) line). Append ("\ n");
}
String result = Sb.tostring ();
System.out.println ("# # # #错误信息 ######:" +result);
Log.debug ("# # # #错误信息LOG ######:" +result);
}

/**
* Get WGRIB2-cut Linux commands to cut grib2 into binary bin files
* @param pathName file path name
* @param filename
* @param savepath the path name to save to
* @param lists condition set (must not be empty because the foreground check is not empty when the database is saved from the database)
* @return
*/
public static string[] Getlinuxcommand (String pathName, String fileName, String savepath, list<string> lists) {

"/bin/sh", "-C",
string[] cmd = new string[3];
Cmd[0] = "/bin/sh";
CMD[1] = "-C";

StringBuffer CONDITION_BF = new StringBuffer ();
for (String str:lists) {
Condition_bf.append (str+ "mb|");
}
Cut off the last ' | ' symbol
String condition_str = condition_bf.tostring (). substring (0, condition_bf.tostring (). Length ()-1);
Wgrib2-s Z_nafp_c_babj_20120508000000_p_cnpc-t639-gmfs-hnehe-00300.grib2 | Grep-e "(\bhgt:20\b) | (\bhgt:30\b) "| Wgrib2-i Z_nafp_c_babj_20120508000000_p_cnpc-t639-gmfs-hnehe-00300.grib2-no_header-bin Data5.bin
StringBuffer bf = new StringBuffer ();
Bf.append ("Wgrib2-s");
Bf.append (pathname+ "/" +filename);
Bf.append ("| Grep-e ");
Bf.append ("\" "+condition_str+" \ "");
Bf.append ("| Wgrib2-i ");
Bf.append (pathname+ "/" +filename);
Bf.append ("-no_header-bin");
String newName = filename.substring (0, Filename.indexof (".")); Remove the file name of the. Grib2
Bf.append (savepath+ "/" +newname+ ". Bin");
CMD[2] = bf.tostring ();

return cmd;
}

public static void Main (string[] args) {
String str = "NEW.GRIB2";
System.out.println (str.substring (0, Str.indexof (".")));
//
String pathName = "/home/ftp/cwfs";
String fileName = "New_003.grib2";
String Savepath = "/home/ftp/cwfs";
//
list<string> lists = new arraylist<string> ();
Lists.add ("het:20");
string[] Command = Getlinuxcommand (PathName, FileName, Savepath, lists);
//
System.out.println (command[2]);

File File = new file ("E:temp");
System.out.println (File.getparent ());
file[] Listfiles = File.listfiles ();
if (listfiles! = null && listfiles.length > 0) {
for (File f:listfiles) {
F.delete ();
//   }
//  }

file[] list = File.listfiles ();
for (File s:list) {
System.out.println (S.getpath ());
//  }

}
}

Java Execution Shell command

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.