Use scripts to start Java programs
At work today, you need to write a shell script to start a socket program to simulate the SMS gateway. I checked some information and finally got it done. Now I want to record it for your convenience.
To illustrate the usage, we use the simplest program. For example, we need to run the main method of the following class.
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 compress the project where this class is located into a jar package, for example, runtime demo. jar. jar and all jar packages required by the project are put together. For example, my directory is: D:/cmddemo/lib.
The following is a script file, which is divided into two parts: Windows and Linux.
I. Windows
In Windows, you need to write familiar BAT files. For example, I create a start. BAT file under D:/Alibaba demo/lib. The content is as follows:
Java-classpath D:/release demo/lib/log4j-1.2.14.jar; D:/release demo/lib/release demo. Jar CHB. cmd. Test. testclass D:/release demo/test.txt
Double-click the BAT file and you will find multiple test.txt files under the D:/cmddemodirectory.
Ii. Linux
In Linux, you need to create a new SH script, which is basically the same as in windows,The only difference is that the delimiter between jar packages in Windows is ";", while the delimiter between jar packages in Linux is ":".
Java-classpath/root/export demo/lib/log4j-1.2.14.jar:/root/export demo/lib/export demo. Jar CHB. cmd. Test. testclass/root/export demo/test.txt
Then run the script file. If you do not have the permission, remember to use
Chmod + x start. Sh
Well, it's very simple, but I still hope to help you.