我为两个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
文件从命令提示符传递参数来运行此文件。但现在我需要将两个类都放在一个jar中,我需要从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();
}
}
}
在这两个类中,我已经硬编码了包含数据的文件路径和工作表编号3。现在我需要从命令提示符传递这些参数,并且需要在一个jar中运行这两个类。
例如:java SecondLevelJsonCreator 3 "C:/path/filename.xls"
答案 0 :(得分:2)
你可以做这样的事情
java -cp jarName.jar packageName.ClassName argumentsIfAny
答案 1 :(得分:1)
java -cp the\path\to\jarFile.jar SecondLevelJsonCreator 3 "C:/path/filename.xls"
通常应该避免将类放在默认包中。将它们放在一个包中,确保目录树与包树匹配,并使用该类的完全限定名称:
java -cp the\path\to\jarFile.jar com.mycompany.myproject.SecondLevelJsonCreator 3 "C:/path/filename.xls"