如何运行 jar 中Java class 的主要方法?

How to run the main method of a Java class in a jar?

我已经为两个 java 个文件创建了一个 jar。

    package com.json;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.util.Iterator;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Row;
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;

    public class SecondLevelJsonCreator {
        private static final String CATEGORY_ID2 = "category_id";
        private static final String CHILD_CATEGORY_TAGS = "child_category_tags";
        private static final String PARENT_CATEGORY_TAGS = "parent_category_tags";
        private static final String CATEGORY_NAME = "categoryName";
        private static final String CATEGORY_ID = "categoryId";

        @SuppressWarnings("unchecked")
        public static void main(String[] args)
        {
             try
             {
                  File fileWI = new File("C://Users//intradhakr//Desktop//LEAF//LEAF_2Level.txt");

                    // if file doesnt exists, then create it
                    if (!fileWI.exists()) {
                        fileWI.createNewFile();
                    }

                 FileWriter fw = new FileWriter(fileWI.getAbsoluteFile());
                 BufferedWriter bw = new BufferedWriter(fw);
                 FileInputStream file = new FileInputStream(new File("C://Users//intradhakr//Desktop//SMTC.xls"));

                 //Create Workbook instance holding reference to .xlsx file
                 HSSFWorkbook workbook = new  HSSFWorkbook(file);

                 //Get first/desired sheet from the workbook
                 HSSFSheet sheet = workbook.getSheetAt(3);

                 //Iterate through each rows one by one
                 Iterator<Row> rowIterator = sheet.iterator();
                 String EMPTY ="";
                 JSONArray childCategory = new JSONArray();
    //             JSONArray parentCategory = new JSONArray();
                 JSONObject json = new JSONObject();
                 rowIterator.hasNext();
                 int i=0;
                 do
                 {
                     i++;
                     Row row = rowIterator.next();


                     if(EMPTY.equals(row.getCell(2).getStringCellValue())) {
                         JSONArray childJsonArr = (JSONArray) json.get(CHILD_CATEGORY_TAGS);
                         JSONObject jso =new JSONObject();
                         jso.put(CATEGORY_ID2, row.getCell(4).getStringCellValue());
                         if(!childJsonArr.contains(jso))
                         {
                             childJsonArr.add(jso);
                         }
                         json.put(CHILD_CATEGORY_TAGS, childJsonArr);
                     }
                     else {
                         if(i!=1)
                         {bw.write(json.toString());
                        bw.newLine();}
                     json = new JSONObject();
                     childCategory = new JSONArray();
                     EMPTY = row.getCell(2).getStringCellValue();
                     json.put(CATEGORY_ID   , row.getCell(2).getStringCellValue());
                     json.put(CATEGORY_NAME, row.getCell(3).getStringCellValue());

                     JSONArray parentCategory = new JSONArray();
                     JSONObject cat0 = new JSONObject();
                     cat0.put(CATEGORY_ID, row.getCell(0).getStringCellValue());
                     cat0.put("category_order_id", 0);
                     parentCategory.add(cat0);

                     JSONObject jsO =new JSONObject();
                     jsO.put(CATEGORY_ID2, row.getCell(4).getStringCellValue());
                     childCategory.add(jsO);


                     json.put(PARENT_CATEGORY_TAGS, parentCategory);
                     json.put(CHILD_CATEGORY_TAGS, childCategory);

                     }


                 } while (rowIterator.hasNext());
                 bw.write(json.toString());
                 file.close();
                 bw.close();
             }
             catch (Exception e)
             {
                 e.printStackTrace();
             }
         }

    }

下面还有一个 Java class。我可以通过使用 .class 文件从命令提示符传递参数来 运行 这个文件。但是现在我需要把 classes 做成一个罐子,我需要 运行 从罐子里取出这两个 classes。

    public class SecondLevelJsonCreator {
        private static final String CATEGORY_ID2 = "category_id";
        private static final String CHILD_CATEGORY_TAGS = "child_category_tags";
        private static final String PARENT_CATEGORY_TAGS = "parent_category_tags";
        private static final String CATEGORY_NAME = "categoryName";
        private static final String CATEGORY_ID = "categoryId";
        @SuppressWarnings("unchecked")
        public static void main(String[] args)
        {
             try
             {
                  File fileWI = new File("C://Users//intradhakr//Desktop//LEAF//LEAF_2Level.txt");
                     // if file doesnt exists, then create it
                    if (!fileWI.exists()) {
                        fileWI.createNewFile();
                    }
                 FileWriter fw = new FileWriter(fileWI.getAbsoluteFile());
                 BufferedWriter bw = new BufferedWriter(fw);
                 FileInputStream file = new FileInputStream(new File("C://Users//intradhakr//Desktop//SMTC.xls"));
                 //Create Workbook instance holding reference to .xlsx file
                 HSSFWorkbook workbook = new  HSSFWorkbook(file);
                   //Get first/desired sheet from the workbook
                 HSSFSheet sheet = workbook.getSheetAt(3);
                   //Iterate through each rows one by one
                 Iterator<Row> rowIterator = sheet.iterator();
                 String EMPTY ="";
                 JSONArray childCategory = new JSONArray();
    //             JSONArray parentCategory = new JSONArray();
                 JSONObject json = new JSONObject();
                 rowIterator.hasNext();
                 int i=0;
                 do
                 {
                     i++;
                     Row row = rowIterator.next();
                                                  if(EMPTY.equals(row.getCell(2).getStringCellValue())) {
                         JSONArray childJsonArr = (JSONArray) json.get(CHILD_CATEGORY_TAGS);
                         JSONObject jso =new JSONObject();
                         jso.put(CATEGORY_ID2, row.getCell(4).getStringCellValue());
                         if(!childJsonArr.contains(jso))
                         {
                             childJsonArr.add(jso);
                         }
                         json.put(CHILD_CATEGORY_TAGS, childJsonArr);
                     }
                     else {
                         if(i!=1)
                         {bw.write(json.toString());
                        bw.newLine();}
                     json = new JSONObject();
                     childCategory = new JSONArray();
                     EMPTY = row.getCell(2).getStringCellValue();
                     json.put(CATEGORY_ID   , row.getCell(2).getStringCellValue());
                     json.put(CATEGORY_NAME, row.getCell(3).getStringCellValue());

                     JSONArray parentCategory = new JSONArray();
                     JSONObject cat0 = new JSONObject();
                     cat0.put(CATEGORY_ID, row.getCell(0).getStringCellValue());
                     cat0.put("category_order_id", 0);
                     parentCategory.add(cat0);

                     JSONObject jsO =new JSONObject();
                     jsO.put(CATEGORY_ID2, row.getCell(4).getStringCellValue());
                     childCategory.add(jsO);
                     json.put(PARENT_CATEGORY_TAGS, parentCategory);
                     json.put(CHILD_CATEGORY_TAGS, childCategory);
                    }
                   } while (rowIterator.hasNext());
                 bw.write(json.toString());
                 file.close();
                 bw.close();
             }
             catch (Exception e)
             {
                 e.printStackTrace();
             }
         }
        }

在这两个 classes 中,我硬编码了文件路径和 sheet 数字 3,其中包含数据。现在我需要从命令提示符传递这些参数,并且需要 运行 这两个 class 放在一个 jar 中。

例如:java SecondLevelJsonCreator 3 "C:/path/filename.xls"

你可以这样做

java -cp jarName.jar packageName.ClassName argumentsIfAny
java -cp the\path\to\jarFile.jar SecondLevelJsonCreator 3 "C:/path/filename.xls"

通常应避免将 classes 放入默认包中。将它们放在一个包中,确保目录树与包树匹配,并使用 class:

的完全限定名称
java -cp the\path\to\jarFile.jar com.mycompany.myproject.SecondLevelJsonCreator 3 "C:/path/filename.xls"