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"