The example of this article tells the method of calling the shell command of Java. Share it for everyone for your reference. The specifics are as follows:
Recently, there is such a demand in the project: After the scheduling of foreign currency funds in the system is completed, the scheduling information should be generated to generate a TXT file, and then this TXT file is sent to another system (Kondor). The generating file naturally uses the OutPutstreamwirter. There are two ways to send files. One is to write a program similar to the FTP function, and the other is to use Java to call the shell and complete the sending of the file in the shell. We choose the latter, that is, after the scheduling of foreign currency funds, use Java's outputStreamWriter to generate a TXT file, and then use Java to call the shell script and complete the work from the FTP file to the Kondor system in the shell script.
The following is Java Program Javashellutil.java:
Import java.io.BuffRedReader; Import Java.iO.File; Import Java.fileoutPutstream; Import java.io.ioException; Import java.inputStreamReader; Import java.io.outputstream; Import Java.io.outPutstreamwriter; Import java.text.dateformat; Import java.text.simpleDateFormat; Import java.util.date; Public Class Javashellutil {// Basic Path Private Static Streat ATH = "/TMP/"; // Log files in the execution of shell Position (absolute path) Private Static Final String ExecuteShellLogFile = Basepath + "ExecuteShell.log"; // Send files to the shell system of the Kondor system (absolute path) Private Static String Sen Sen dkondorshellName = Basepath + "Sendkondorfile.sh"; Public INTETECUTESHELL (String Shellcommand) Throws IOEXCEPTION {int SUCCESS = 0; StringBuffer StringBuffer = New StringBuffer (); = Null; // Formatting date time, use the dateFormat dateFormat = New SimpleDateFormat ("Yyyy-MM-DD Hh: mm: ss "); Try {stringbuffer.append (dateformat.Format (new date ())). Append (" Prepare to execute the shell command ") .appnd (shellcommand) .appnd (" /r /n "); Process pid = null; string [] cmd = {"/bin/sh", "-c", shellcommand}; // execute the shell command pid = runtime.getruntime (). EXEC (cmd); if (pid! = Nulll ) {stringbuffer.append ("Process number:"). Append (pid.tostring ()). Append ("/r/n"); // bufferedReader is used to read the output content of shell bufferedReader = new bufferedReader (New Inputer StreamReader (pid.getInpputStream ()), 1024); pid.waitfor ();} Else {stringbuffer.append ("No PID/R/N");} StringBuffer.append (DateFormat.Format (new date ())). APPEND ("Shell command executes/r/n execute results:/r/n"); string line = null; // Read the output content of shell and add to the whole (bUFFEREDREADER! = NUFFEREDREADER! = NULL && (LINE = bufferedReader.readline ())! = Null) {stringbuffer.append (line) .appnd ("/r/n");}} Catch (Exception IOE) {stringbuffer.append ("" ::// r/n "). Append (IOE.GetMessage ()). Append ("/r/n ");} Finally {if (buffredReader! = Null) {OutputStream OutputStreamwriter = NULL; TRY FFEREDRADER.Close ();/ /Output the shell's execution to the OutputStream OutputStream = New FileoutPutstream (ExecuteShellLLOGFile); OutputStreamWriter = New OutputStream (OutputStream, "Utf-8"); outputStreamWrite.write (stringbuffer.tostring ());} Catch (Exception E ) {e.printstacktrace ();} Finally {outputStreamWriter.Close ();}} Success = 1;} Return Success;}}}}
The following is the shell script Sendkondorfile.SH.
#!/bin/sh#Logfile = "/OPT/FMS2_KONDOR/Sendkondorfile.log" #Kondor system, the generated file generates to this address Kondor_ip = 192.168.1.200#FTP_UserName = Kondor#ftp password ftp_password = Kondor#The absolute path of the file to be sent, the absolute path of the file to be sent, filepath = "# The second parameter is given to FILENAMEIF [$# -Ge "1"] TheNFilepath = $ 1LSEECHO "No file path" echo "without file path/n" >> $ logfileReturnFiif [$# -e "2"] thenfilenamecho "No file name" echo "without file name/n" >> $ logfileReturnFiecho "The file to be sent is $ {filepath}/$ {filename}" CD $ {filepath} ls $ filenameif (test $? -Eq 0) thencho "Ready to send files: $ {filepath}/$ {filename}" elseecho "file $ {filepath}/$ {fieldame} does not exist" echo "file $ {filepath}/$ {filename} does not exist/n" >> $ LogfileReturnPip -n $ {Kondor_ip} << _ Enduser $ {ftp_username} $ {ftp_password} ASCPROMPTPUT $ FILENAMECHO_ENDECHO "Date +%Y-%M-%D ''%H:%m:%s` sent a file $ {filepath }/$ {FILENAME} "Echo" Date +%Y-%M-%D '%H:%M:%S` Send file $ {Filepath}/$ {FILENAME}/n ">> $ Logfile
The call method is:
Javashellutil javashellutil = new javashellutil (); // The parameter is the shell command to be executed, that is, the tmp.pdf file in the/TEMP directory is sent to 192.168.1.200 by calling the shell script Sendkondorfile.sh. uccess = javashellutil.executeshell ("" sh /tmp/sendkondorfile.sh /Temp TMP.pdf ");
It is hoped that this article is helpful to everyone's Java program design.