当前位置:网站首页>Jar batch management gadget

Jar batch management gadget

2022-07-04 23:23:00 lijiamin-

Preface

One Jar Package management gadget , At present, it has the following functions , Later, it can be expanded slowly

1. Search all under the current project path jar And copy to a certain path

2. Execute jar Package batch run start

3.Kill all java process

One 、 The code structure

These functions can be extended slowly by themselves , At present, I have only done what I need , First, let's look at file processing

JarManagerApplication

package com.ljm;


import com.ljm.play.One;
import com.ljm.play.Two;
import java.io.IOException;
import java.util.Scanner;

/** *  One JAR Manage small scripts  * 1. Search all under the current project path jar And copy to a certain path  * 2. Execute jar Package batch run start  * 3.kill all java process  * @author  Li Jiamin  */
public class JarManagerApplication {
    

    public static void main(String[] args) {
    
        menu();
        String InputStr = null;
        //  Create a scanner object   For receiving keyboard data 
        Scanner scannerShow = new Scanner(System.in);
        while (!scannerShow.hasNext() == true) {
    
            //  Waiting for user input 
        }
        InputStr = scannerShow.next();
        System.out.println(" The function item you entered is :" + InputStr);
        switch (InputStr) {
    
            case "1":
                // 1. Search all under the current project path jar And copy to a certain path 
                One.selectMethod();
                One.copyMethod();
                break;
            case "2":
                // 2. Execute jar Package batch run start 
                Two.runJar();
                break;
            case "3":
                // 3.kill all java process 
                break;
            default:
                System.out.println("=== Parameter error ===");
                break;
        }
        //  Program exit 
        System.exit(0);
    }

    /** *  One JAR Manage small scripts  * 1. Search all under the current project path jar And copy to a certain path  * 2. Execute jar Package batch run start  * 3.kill all java process  */
    public static void menu() {
    
        System.out.println(" One JAR Manage small scripts -----");
        System.out.println(" Please enter the function item ( Numbers ):");
        System.out.println("1. Search all under the current project path jar And copy to a certain path ");
        System.out.println("2. Execute jar Package batch run start ");
        System.out.println("3.kill all java process ");
        System.out.println();
    }
}

One

package com.ljm.play;

import com.ljm.handle.FileHandler;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

public class One {
    

    /** *  Query search  */
    public static void selectMethod() {
    
        // E:\44_JAVA_Protect\ Hangzhou metro \hangzhou-metro-server-new
        String pathInputStr = "";
        //  Create a scanner object   For receiving keyboard data 
        Scanner scanner = new Scanner(System.in);
        System.out.println(" Please enter the project path :");
        //  Determine whether the user has input string 
        if (scanner.hasNext() == true) {
    
            pathInputStr = scanner.next();
            System.out.println(" The path you entered is :" + pathInputStr);
        }
        //  Search for 
        FileHandler.getBladeJar(pathInputStr);
    }

    /** *  Copy to a path  */
    public static void copyMethod() {
    
        //  Directory location 
        String pathInputStr = "";
        //  Create a scanner object   For receiving keyboard data 
        Scanner scanner = new Scanner(System.in);
        System.out.println(" Please enter the path to copy :");
        //  Determine whether the user has input string 
        if (scanner.hasNext() == true) {
    
            pathInputStr = scanner.next();
            System.out.println(" The path you entered is :" + pathInputStr);
        }

        List<String> bladeJarPathList = FileHandler.getBladeJarPathList();
        List<String> bladeJarNameList = FileHandler.getBladeJarNameList();
        System.out.println(" Start copying .......");
        for (int i = 0; i < bladeJarPathList.size(); i++) {
    
            String sPath = bladeJarPathList.get(i);
            String sName = pathInputStr + "\\" + bladeJarNameList.get(i);
            try {
    
                FileHandler.copy(sPath, sName);
                System.out.println(" Copied file :" + (i + 1) + "  Total quantity :" + bladeJarPathList.size());
            } catch (IOException e) {
    
                throw new RuntimeException(e);
            }
        }
    }

}

FileHandler

package com.ljm.handle;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/** *  File processor  * @author  Li Jiamin  */
public class FileHandler {
    


    private static List<String> jarPathList = new ArrayList<>();
    private static List<String> jarNameList = new ArrayList<>();

    /** JAR Package path  */
    public static List<String> getBladeJarPathList() {
    
        return jarPathList;
    }

    /** JAR Package name  */
    public static List<String> getBladeJarNameList() {
    
        return jarNameList;
    }

    /** *  Find all BLADE-JAR Postfix file  * @param path */
    public static void getBladeJar(String path) {
    
        File file = new File(path);
        if (file.exists()) {
    
            File[] files = file.listFiles();
            if (null != files) {
    
                for (File file2 : files) {
    
                    if (file2.isDirectory()) {
    
                        getBladeJar(file2.getAbsolutePath());
                    } else {
    
                        String file2AbsolutePath = file2.getAbsolutePath();
                        int length = file2AbsolutePath.length();
                        int lastIndexOf = file2AbsolutePath.lastIndexOf(".");
                        if (lastIndexOf <= 0) {
    
                            continue;
                        }
                        if (file2AbsolutePath.substring(lastIndexOf, length).equals(".jar")) {
    
                            if (!file2AbsolutePath.contains("common") && !file2AbsolutePath.contains("api") && !file2AbsolutePath.contains("lib")) {
    
                                jarPathList.add(file2AbsolutePath);
                                jarNameList.add(file2.getName());
                            }
                        }
                    }
                }
            }
        }
    }

    /** *  from srcPathName Copy the contents of the file to destPathName In file  * @param srcPathName  Input file  * @param destPathName  The output file  * @throws IOException */
    public static void copy(String srcPathName, String destPathName) throws IOException {
    
        //  establish IO flow 
        // FileInputStream and FileOutputStream
        FileInputStream fis = new FileInputStream(srcPathName);
        FileOutputStream fos = new FileOutputStream(destPathName);
        //  from fis Read data in stream , writes fos Streaming 
        byte[] data = new byte[10];
        int len;
        while ((len = fis.read(data)) != -1) {
    
            fos.write(data, 0, len);
        }
        //  close 
        fis.close();
        fos.close();
    }
}

The above code summarizes several function points

  1. Search for files with a suffix under a certain path
  2. Copy file
  3. IO flow

Then look at the code started by the batch execution script

package com.ljm.play;

import com.ljm.handle.FileHandler;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Two {
    

    /** *  Run all Jar */
    public static void runJar() {
    
        String pathInputStr = "";
        //  Create a scanner object   For receiving keyboard data 
        Scanner scanner = new Scanner(System.in);
        System.out.println(" Please enter JAR route :");
        //  Determine whether the user has input string 
        if (scanner.hasNext() == true) {
    
            pathInputStr = scanner.next();
            System.out.println(" The path you entered is :" + pathInputStr);
        }
        //  Search for 
        FileHandler.getBladeJar(pathInputStr);
        List<String> jarPathList = FileHandler.getBladeJarPathList();
        // cmd
        try {
    
            FileWriter fileWriter = new FileWriter(pathInputStr + "\\" + "start.bat");
            fileWriter.append("@echo off \n");
            fileWriter.append("\n");
            for (String strP : jarPathList) {
    
                fileWriter.append("start java -d64 -server -Xms512m -Xmx2048m -jar " + strP + "\n");
                fileWriter.append("choice /t 10 /d y /n > null " + "\n");
                fileWriter.append("\n");
            }
            fileWriter.flush();
            fileWriter.close();
            Process p = Runtime.getRuntime().exec("cmd /c " + pathInputStr + "\\" + "start.bat");
        } catch (Exception e) {
    
            throw new RuntimeException(e);
        }
    }
}

It's delicious , The disadvantage is that the path should be in English , This has not been solved , A bit late .

Two 、 Open source warehouse address

Address
The official is still reviewing , undetermined

summary

Tips : Here is a summary of the article :
for example : That's what we're going to talk about today .

原网站

版权声明
本文为[lijiamin-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042301132274.html