Start a Java program with a script

Source: Internet
Author: User

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.

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.