Use scripts to start Java programs

Source: Internet
Author: User
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.

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.