Today at work, you need to write a shell script that launches a socket program that simulates the SMS gateway. Looked up some information, finally took care of, now record, convenient for everyone to check.
In order to illustrate the use of the method, we use the simplest program, for example, we want to run the following class of the Main method
Package chb.cmd.test;
Import Java.io.File;
Import java.io.IOException;
Import Org.apache.log4j.Logger;
public class TestClass {
static Logger log = Logger.getlogger (Testclass.class);
/**
* @param args
*/
public static void main (String[] args) {
log.debug (args[0]);
file file = new file (Args[0]);
try {
file.createnewfile ();
} catch (ioexception e) {
e.printstacktrace ( );
}
}
}
We need to make the project of this class into a jar package, for example called: Cmddemo.jar, and then put Cmddemo.jar together with all the jar packages needed for the project, such as my directory: D:/cmddemo/lib.
To write the script file below, we are divided into two parts, Windows and Linux.
A. Windows
Under Windows is to write the familiar bat file, for example, I create a new start.bat under D:/cmddemo/lib, the content is as follows:
Java-classpath D:/cmddemo/lib/log4j-1.2.14.jar;d:/cmddemo/lib/cmddemo.jar Chb.cmd.test.TestClass d:/cmddemo/ Test.txt
Then double-click on the bat file and you will find a Test.txt file in the D:/cmddemo directory.
Two. Linux
Under Linux, you need to create a new SH script, which is basically the same as under Windows, except that the delimiter between each jar package under Windows is ";" and the delimiter between each jar package under Linux is ":".
Java-classpath/root/cmddemo/lib/log4j-1.2.14.jar:/root/cmddemo/lib/cmddemo.jar chb.cmd.test.testclass/root/ Cmddemo/test.txt
Then run the script file is OK, if you do not have permission, remember to use
chmod +x start.sh
Yes, well, it's simple, but I hope we can help.