
Hi,
I have done this code. Can u pls tell me how to create a jar for this in eclipse, this is only a single java file?
package com.dcp.ui;
import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; import java.io.File; import java.io.FilePermission; import java.io.FilenameFilter; import java.io.IOException; import java.sql.Array; //import java.nio.file.Files; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollBar; import javax.swing.SwingUtilities; import javax.swing.filechooser.FileFilter;
import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.biff.WorkspaceInformationRecord; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException;
public class DcpRep //implements ActionListener { JLabel label = new JLabel(); private static void createAndShowGUI() { // make frame.. frame = new JFrame("Select"); frame.setDefaultCloseOperation(JFrame.EXITONCLOSE); frame.setBounds(20,30,700,700); frame.getContentPane().setLayout(null); jb = new JButton("Browse"); jb.setBounds(200, 250, 100, 50); frame.getContentPane().add(jb); jb2=new JButton("Generate"); jb2.setBounds(20, 20, 100, 50); frame.getContentPane().add(jb2); frame.setVisible(true); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
JFileChooser jf = new JFileChooser();
/*FileFilter filt = new FileFilter() {
@Override
public String getDescription() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean accept(File arg0) {
// TODO Auto-generated method stub
return false;
}
};*/
/*FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File arg0, String arg1) {
// TODO Auto-generated method stub
return false;
}
};
filter.accept(file, "xls");*/
//filter.addExtension("xlsx");
//filter.setDescription("xls & xlsx sheets");
//jf.setFileFilter(filt);
//jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jf.setMultiSelectionEnabled(true);
returnVal = jf.showOpenDialog(frame);
File[] file = jf.getSelectedFiles();
// int returnVal2 = jf.showSaveDialog(frame);
// System.out.println(returnVal2);
flength=file.length;
System.out.println(file.length);
for(int f=0;f<file.length;f++)
{
Workbook workbook;
try {
workbook=Workbook.getWorkbook(file[f]);
Sheet sheet=workbook.getSheet(0);
r[f]=sheet.getRows();
c=sheet.getColumns();
//arr = new String[20][c][r[f]];
for(int i=0;i<c;i++)
{
for(int j=0;j<r[f];j++)
{
if(j<500)
{
Cell a=sheet.getCell(i,j);
arr[f][i][j]=a.getContents();
//System.out.println(arr[f][i][j]);
}
else if((j>500)&&(j<1000))
{
Cell b=sheet.getCell(i,j);
brr[f][i][j-500]=b.getContents();
//System.out.println(brr[f][i][j-500]);
}
else if((j>1000)&&(j<1500))
{
Cell c=sheet.getCell(i,j);
crr[f][i][j-1000]=c.getContents();
//System.out.println(brr[f][i][j-500]);
}
}
}
}catch (BiffException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} });
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{if(returnVal == JFileChooser.APPROVE_OPTION)
{
WritableWorkbook generate;
try {
generate=Workbook.createWorkbook(new File("C:/Users/Jai Badri Vishal/Desktop/opt.xls"));
WritableSheet st=generate.createSheet("First Sheet",0);
for(int f=0;f<flength;f++)
{
System.out.println("r is "+r[f]);
//System.out.println("Iteration no "+f);
for(int k=0;k<c;k++)
{
for(int j=y, x=0;j<y+r[f];x++,j++)
{
if(x<500)
{
Label label=new Label(k,j,arr[f][k][x]);
st.addCell(label);
//System.out.println(arr[f][k][j]);
}
else if((x>500)&&(x<1000))
{
Label label=new Label(k,j,brr[f][k][x-500]);
st.addCell(label);
//System.out.println(brr[f][k][j-500]);
}
else if((x>1000)&&(x<1500))
{
Label label=new Label(k,j,crr[f][k][x-1000]);
st.addCell(label);
//System.out.println(brr[f][k][j-500]);
}
}
}y=y+r[f];
}
generate.write();
generate.close();
} catch (WriteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
} }
});
}
public static void main(String[] args) {
// start off..
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
static JButton jb,jb2;
static JFrame frame;
static int c,y,flength;
static int r[]=new int[20];
static String arr[][][]=new String[100][500][500];
static String brr[][][]=new String[100][500][500];
static String crr[][][]=new String[100][500][500];
static File file,p;
static int returnVal;
}

A Jar file combines several classes into a single archive file. Basically,library classes are stored in the jar file. Through the given code you can create your own jar file.
import java.io.*;
import java.util.jar.*;
public class CreateJar {
public static int buffer = 10240;
protected void createJarArchive(File jarFile, File f) {
try {
byte b[] = new byte[buffer];
FileOutputStream fout = new FileOutputStream(jarFile);
JarOutputStream out = new JarOutputStream(fout, new Manifest());
JarEntry addFiles = new JarEntry(f.getName());
addFiles.setTime(f.lastModified());
out.putNextEntry(addFiles);
FileInputStream fin = new FileInputStream(f);
while (true) {
int len = fin.read(b, 0, b.length);
if (len <= 0)
break;
out.write(b, 0, len);
}
fin.close();
out.close();
fout.close();
System.out.println("Jar File is created successfully.");
} catch (Exception ex) {}
}
public static void main(String[]args){
CreateJar jar=new CreateJar();
File folder = new File("c:/");
File files = new File("c:/DcpRep.java");
File file=new File("c:/Example.jar");
jar.createJarArchive(file, files);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.