LauncherBootstrap

Launcher 组件是一个与平台无关的Java 应用载入器。 Commons-launcher 消除了需要批处理或者Shell脚本来载入Java 类。 
假如你需要在linux/windows上运行一个带有main方法的类,而该类又引用了许多jar和class文件,可以用Launcher。 

Introduction

The Commons-launcher project provides a smart way to start a Java application. In the facts, the CommonsLauncher provides a bootstrap class which starts a ant process.

This ant process :

  • makes the needed pre execution tasks : create the needed classpath, check resources, download resources ..
  • invoke a launcher task which allows to start a java application

Example

We want to start a small Main class :

package org.apache.launcher.example;

import org.apache.commons.logging.LogFactory;

public class Main {
    public static void main(String[] args) {
        LogFactory.getLog(Main.class).info("start");
    }
}
We will name it "our application".

Global distribution

This runtime distribution includes:

  • bin/demo.sh : the unix user script to start our small Main class
  • bin/demo.bat : the windows user script to start our small Main class
  • bin/ : contains the other files used by the script
  • etc/ : contains the configuration file (log4j configuration, ..)
  • lib/launcher-demo.jar : contains the small Main class
  • lib/ : contains the needed jar files to the application
  • lib/launcher/ : contains the needed jar files to the commons launcher

Bin Directory

User script

The demo.(sh/bat) is the bootstrap script used by the user to start the application:

  java -cp . LauncherBootstrap -executablename launcher-demo demo

Launcher ant configuration

Our launcher.xml is as simple as possible :

<project name="Demo Launcher" default="demo" basedir=".">

    <property name="base.dir" value="${basedir}/.."/>

    <property name="etc.dir" value="${base.dir}/etc"/>
    <property name="lib.dir" value="${base.dir}/lib"/>
    <property name="log.dir" value="${base.dir}/log"/>

    <path id="base.class.path">
        <pathelement path="${etc.dir}"/>
        <fileset dir="${lib.dir}" includes="*.jar"/>
    </path>

    <target name="demo">
        <mkdir dir="${log.dir}" />
        <launch classname="org.apache.launcher.example.Main">
            <classpath refid="base.class.path"/>
            <syspropertyset>
                <sysproperty key="log.dir" file="${log.dir}"/>
            </syspropertyset>
        </launch>
    </target>
</project>
It creates the needed classpath, the log directory, sets the log.dir system property for log4j and executes our Main class.

Other files

  • LauncherBootstrap.class, commons-launcher.jar : the neede classes the CommonsLauncher
  • launcher.properties : the bootstrap configuration (used to find the lib/launcher files)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值