Tuesday 6 December 2016

How to convert Java source file to Jar and working with function inside java file.




Type of JAR file

1) Executable JAR
2) Non-Executable JAR


Follow the below steps for convert JAVA source file to Non-Executable JAR file.
------------------------------------------------------------------------------

1) Create java file by using below code


public class SampleCode{



// create new Excel sheet in workbook

public static void createExcelSheet(Workbook wb, String SheetName, String ExcelPath) throws IOException
{
wb.createSheet(SheetName);
FileOutputStream output_file =new FileOutputStream(new File(ExcelPath));
wb.write(output_file);
output_file.flush();
output_file.close();

}


// verify Excel version

public static Workbook verifyExcelVersion(String FilePath) throws FileNotFoundException, IOException
{
Workbook book1=null;
String fileExtn=FilenameUtils.getExtension(FilePath);

if(fileExtn.equals("xls"))
book1=new HSSFWorkbook(new FileInputStream(FilePath));
else if(fileExtn.equals("xlsx"))
book1=new XSSFWorkbook(new FileInputStream(FilePath));
else if(fileExtn.equals("xlsm"))
book1=new XSSFWorkbook(new FileInputStream(FilePath));
return book1;
}




}


2) Save above source file under one package.
Example (Package) -> SampleCode.java

3) Right on  Example (Package) -> Export -> Java -> JAR file -> Click Next

4) In JAR File Specification Window

i)   verify expected Package selected or not.
ii)  select "Export Generated class files and resources".
iii) select "compress the contents of the JAR file".
iv)  provide the path in "Select the export destination"
v)   click Next.

5) In JAR Packaging Options window

i) select "Export class files with compile Errors"
ii) Select "Export class files with compile warnings"
ii) Select "Save the descriptions of this JAR in workspace"
iv) provide the path in "Description file".
v)  click Next

6) In JAR Manifest Specification window

i)   click "Generate the Manifest file".
ii)  click "seal some packages".
iii) click finish

7) Once we cick finsh two files withh get genrated in mentioned path

i) FileName.jar
ii) FileName.jardesc

8) The above JAR is ready to some other project to use existion code. This JAR is called Non Executable JAR file.




How to use and execute function with in JAR file to another package.
-------------------------------------------------------------------

1) create the project

2) Add FileName.jar file in to configure Build path.

3) Then we can use existing functions which available in FileName.jar file.



public static void main(String agrs[])
{

SampleCode obj1=new SampleCode();
Workbook book2=obj1.verifyExcelVersion(ToFilePath);
Sheet sheet2=null;


obj1.createExcelSheet(wb,"Sheet2",Download_FilePath);

}

4) By using above method we can use functions which available in FileName.jar file.

No comments:

Post a Comment