2013/01/15

Batch Shell 產生Java jar

為了自己方便,只有在W7 企業版 64bit上面測試過
第一次寫Batch請多多包含

要編譯的java程式碼如下圖,實體Code會貼在Batch Shell的後面




先把Code存成java4.java,並且javac java4.java後會出現以下幾個Class


執行Create jar.bat,請依序輸入no java4 ChengYouFang,輸入完畢後匯出編程需要的manifest.mf(有在編程的人應該不陌生吧XDDDD又愛又恨的東西),並且執行打包程序

依序輸入的為是否有Package、主類別(就是執行整個專案的類別檔名稱)以及輸出的jar檔案名稱




為了證明打包不是作假的,所以下圖分別使用java -jar ChengYouFang.jar以及java java4的方式去執行



如果對jar編程有問題的人建議參考這篇『[Java]如何將編譯好的.class檔案打包成JAR?』,我也是參照這篇的作法製作出來的

懶得自己重新複製貼上點我下載

Create jar.bat
@ECHO off
REM 20130115 By Cheng You Fang publish
REM http://writecodepeople.blogspot.tw/
SET /p isPackage=The project if a Package then input yes, else input no:
IF NOT %isPackage%==yes CALL:NO

:YES
    SET /p PackageName=Please input your Package Name:
    SET /p MainClass=Please input your Main Class Name:
    ECHO Main-Class:%PackageName%/%MainClass% > ./manifest.mf
    SET /p JarName=Please input your jar Name:
    jar cvfm %JarName%.jar manifest.mf *.class
    CALL:END

:NO
    SET /p MainClass=Please input your Class Name:
    ECHO Main-Class: %MainClass% > ./manifest.mf
    SET /p JarName=Please input your jar Name:
    jar cvfm %JarName%.jar manifest.mf *.class
    CALL:END

:END
    PAUSE
    EXIT

Sample Code
class Tortoise extends Thread {
    static int i = 1;

    @Override
    public void run() {
        try {

            for (; i <= 10; i++) {
                System.out.println("第" + i + "秒,烏龜跑了" + i + "公尺");
                sleep(1000);
            }
            System.out.println("烏龜花了" + (i - 1) + "秒");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        super.run();
    }
}

class Rbbit extends Thread {
    static int i = 1;

    @Override
    public void run() {
        try {
            for (; i <= 5; i++) {
                System.out.println("第" + i + "秒,兔子跑了" + (i * 2) + "公尺");
                sleep(1000);
            }
            System.out.println("兔子花了" + (i - 1) + "秒");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        super.run();
    }
}

public class java4 {
    public static void main(String argv[]) {
        Rbbit rbbit = new Rbbit();
        Tortoise tortoise = new Tortoise();
        rbbit.start();
        tortoise.start();

    }
}



參考資料:
http://it-easy.tw/java-jar/
http://www.petri.co.il/quickly_create_txt_file_from_cmd.htm
http://www.robvanderwoude.com/if.php
http://blog.miniasp.com/post/2009/06/24/Sleep-command-in-Batch.aspx
http://www.computerhope.com/pausehlp.htm
http://stackoverflow.com/questions/7879791/how-to-read-input-from-console-in-a-batch-file
http://blog.miniasp.com/post/2010/09/24/How-to-parse-text-from-file-or-command-using-Batch.aspx
http://www.robvanderwoude.com/goto.php
http://33tsai.blogspot.tw/2008/04/bat.html
http://www.jb51.net/article/14986.htm
http://stackoverflow.com/questions/132799/how-can-you-echo-a-newline-in-batch-files
http://www.robvanderwoude.com/call.php
http://bbs.csdn.net/topics/260067601
http://yukinaco.pixnet.net/blog/post/22737349