1

I have created a jar for two java files.

    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();
             }
         }

    }

And I have another Java class given below. I can run this file by passing the arguments from command prompt by using the .class files of this. But now I need to make both the classes one jar and I need to run these two classes from the jar.

    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();
             }
         }
        }

In these two classes I have hardcoded the file path and sheet number 3, which contains the data. Now I need to pass these parameters from the command prompt and need to run these two classes in a single jar.

For example: java SecondLevelJsonCreator 3 "C:/path/filename.xls"

Raceimaztion
  • 9,494
  • 4
  • 26
  • 41
kr thrinod
  • 23
  • 1
  • 1
  • 3

2 Answers2

8

you can do something like this

java -cp jarName.jar packageName.ClassName argumentsIfAny
Vihar
  • 3,626
  • 2
  • 24
  • 47
1
java -cp the\path\to\jarFile.jar SecondLevelJsonCreator 3 "C:/path/filename.xls"

You should generally avoid putting classes in the default package. Put them in a package, make sure the directory tree matches the package tree, and use the fully qualified name of the class:

java -cp the\path\to\jarFile.jar com.mycompany.myproject.SecondLevelJsonCreator 3 "C:/path/filename.xls"
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Error: Could not find or load main class com\json\FirstLevelJsonCreator I am getting this error when i run java -cp com.json-0.0.1-SNAPSHOT.jar com/json/FirstLevelJsonCreator can u tell me the answer for this – kr thrinod Jan 23 '15 at 08:54
  • you should be doing `java -cp com.json-0.0.1-SNAPSHOT.jar com.json.FirstLevelJsonCreator` – Vihar Jan 23 '15 at 09:08
  • The fully qualified class name, as written in my answer, is separated by dots: `com.mycompany.myproject.SecondLevelJsonCreator`. Not by slashes as in your attempt: `com/json/FirstLevelJsonCreator`. – JB Nizet Jan 23 '15 at 09:13