JAR Generation

JAR Generation

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;

}

View Answers

May 7, 2012 at 11:35 AM

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









Related Tutorials/Questions & Answers:
JAR Generation
JAR Generation  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
JAR Generation
JAR Generation  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
Advertisements
pdf generation.
pdf generation.  i want to generate the data which is stored in mysql data base in pdf format with php. how i will do
Barcode generation
Barcode generation  hi, I am working on a project where there is a alias no. with symbology EAN-13 for generation of barcode... now i have to write the logic for one item no if their are two or more quantities it should
report generation using jsp
report generation using jsp  report generation coding using jsp
generation - Maven
generation  I have 2 config files to a file "monappli.properties" and a file "log4j.xml" the problem is that I was asked to generate the JAR files and two outside the JAR! I do not know how outsourced both files Help
report generation - EJB
report generation   how to create the report in java
PDF Generation for unicode characters
PDF Generation for unicode characters  Hi, How a PDF file having unicode characters is generated in JSP
jar file
jar file  steps to create jar file with example
jar file
jar file  jar file where it s used
jar file
jar file  how to create a jar file in java
jar filw
jar filw  how can i run window based program using jar file
JAR FILE
JAR FILE  WHAT IS JAR FILE,EXPLAIN IN DETAIL?   A JAR file... and applications. The Java Archive (JAR) file format enables to bundle multiple... link: Jar File Explanation
quotion on .jar
quotion on .jar  in realtime where we use .jar class files.   A Jar file combines several classes into a single archive file. Basically,library classes are stored in the jar file. For more information,please go through
jar file
jar file  how to run a java file by making it a desktop icon i need complete procedur ..through cmd
Runnable JAR
Runnable JAR  How to create runnable JAR file in eclipse ? Please provide me step by step demo... I am windows 7 user. I have made one jar file but when I double click it,it doesn't run. Why so
Runnable JAR
Runnable JAR  How to create runnable JAR file in eclipse ? Please provide me step by step demo... I am windows 7 user. I have made one jar file but when I double click it,it doesn't run. Why so
ModuleNotFoundError: No module named 'template_generation'
ModuleNotFoundError: No module named 'template_generation'  Hi, My... named 'template_generation' How to remove the ModuleNotFoundError: No module named 'template_generation' error? Thanks   Hi
Table generation error - EJB
Table generation error  Hi friends I am using EJB3.0 with sun java system application server 9.1 with postgresql database. Actually when i make an EJB it gives the following exception and doesnt generate any table
Automatic primary Generation in Hibernate - Hibernate
Automatic primary Generation in Hibernate   Hi, Ow primary key is generated automatically in hibernate.give me sample code.Thank u
Hibernate generation Id to postgreSQL
Hibernate generation Id to postgreSQL  Hi, I'm trying to implement a id generator in Hibernate, but I'm having a problem. Hibernate doesn't seems to reset the counting when the id is generated. For example: 1 - In PostgreSQL I
Play framework dynamic form generation
Play framework dynamic form generation  I want to generate dynamic forms using play framework can any one give me some idea about dynamic form generation using play framework
ModuleNotFoundError: No module named 'odoo10-addon-contract-sale-generation'
ModuleNotFoundError: No module named 'odoo10-addon-contract-sale-generation...: ModuleNotFoundError: No module named 'odoo10-addon-contract-sale-generation' How...-generation' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'random-user-generation'
ModuleNotFoundError: No module named 'random-user-generation'  Hi...: No module named 'random-user-generation' How to remove the ModuleNotFoundError: No module named 'random-user-generation' error? Thanks   Hi
ModuleNotFoundError: No module named 'ros-groovy-job-generation'
ModuleNotFoundError: No module named 'ros-groovy-job-generation'  Hi...: No module named 'ros-groovy-job-generation' How to remove the ModuleNotFoundError: No module named 'ros-groovy-job-generation' error? Thanks  
ModuleNotFoundError: No module named 'ros-job_generation'
ModuleNotFoundError: No module named 'ros-job_generation'  Hi, My... named 'ros-job_generation' How to remove the ModuleNotFoundError: No module named 'ros-job_generation' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'odoo10-addon-contract-sale-generation'
ModuleNotFoundError: No module named 'odoo10-addon-contract-sale-generation...: ModuleNotFoundError: No module named 'odoo10-addon-contract-sale-generation' How...-generation' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'ros-job_generation'
ModuleNotFoundError: No module named 'ros-job_generation'  Hi, My... named 'ros-job_generation' How to remove the ModuleNotFoundError: No module named 'ros-job_generation' error? Thanks   Hi, In your
Version of com.ariht>config-generation-maven-plugin dependency
List of Version of com.ariht>config-generation-maven-plugin dependency
Running Jar file in Windows
Running Jar file in Windows  Running Jar file in Windows
Extract Jar File
Extract Jar File  How to extract jar file?   Hi Please open the command Prompt and to the jar file and write the command jar -xvf JaraFileName.jar
servlet (tomcat server) and jar
servlet (tomcat server) and jar  how to create a jar file and where to place the jar file in tomecat server
Structs jar files
Structs jar files  please Post required jar files for structs. Thanks, L.Mahesh Kumar
ModuleNotFoundError: No module named 'jar'
ModuleNotFoundError: No module named 'jar'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'jar' How to remove the ModuleNotFoundError: No module named 'jar' error
jar file - Java Beginners
jar file  jar file When creating a jar file it requires a manifest... options in jar file. What is Jar File? JAR files are packaged in the zip format  What is Jar File?JAR files are packaged in the zip format making
quqtion on jar files
quqtion on jar files  give me realtime examples on .jar class files   A Jar file combines several classes into a single archive file. Basically,library classes are stored in the jar file. For more information,please go
org.apache.commons.collections15.Transformer jar file
org.apache.commons.collections15.Transformer jar file  Dear, Please can you provide me the link for the following jar file: import org.apache.commons.collections15.Transformer; tahnks too much
pom.xml local jar
pom.xml local jar  Hi, I have placed one jar file in the WEB-INF/lib folder now I wan't to use this jar in the maven build and test phases. How to use this jar file? Thanks
JAR File - Swing AWT
JAR File   Is there anybody to solve my problem of not executing the JAR file? I have asked the question previously but I haven't got any answer yet. Please help me
Adding Jar into Eclipse
Adding Jar into Eclipse  Hi, Please provide Step by step procedure to add jar, tld files and configurations in Eclipse Helios version and i am using Jboss5. Thanks&Regards, Shiva sADS_TO_REPLACE_1
jar file with html
jar file with html  I have a jar file. On double click it shows an applet page. Please tell me how to connect that applet into html or jsp page
guava-21.0.jar maven dependency
guava-21.0.jar maven dependency  hi, What is code for adding guava-21.0.jar maven dependency in pom.xml file? Thanks   Hi, Use this: <dependency> <groupId>com.google.guava</groupId> <
Java FTP jar
Java FTP jar  Which Java FTP jar should be used in Java program for uploading files on FTP server? Thanks   Hi, You should use commons-net-3.2.jar in your java project. Read more at FTP File Upload in Java
Creating an executable jar file
Creating an executable jar file  When I try to create jar.exe through CMD, the manifest file created doesn't contain Main-Class attribute, even though it is included in text file used during creation of jar file
Jar file - Java Beginners
Jar file  Hello this is my second question and i hope atleast i will get answer i bulid a jar file of my project using netbeans but now problem is when i open that jar file in command prompt i got error exception in thread
pom.xml local jar
pom.xml local jar  Hi, I have placed one jar file in the WEB-INF/lib folder now I wan't to use this jar in the maven build and test phases. How to use this jar file? Thanks   HI, You can add following dependency
File path for jar file
File path for jar file  Hi Experts, I have created one eclipse... jar file of that application, unfortunately it is giving the error that resource... handle the paths given to program in jar files
Need Jar in the JPA examples
Need Jar in the JPA examples  JPA - tutorial is good, very easy to understand and simple. But to run the examples, the jar/helping files are needed. and how Hibernates are used in the JPA example? Thnaks Abhijit Das   
creating JAR - Java Beginners
installed SQL and Tomcat.I created a JAR of my java project and included... JAR to the lib, the WEBAPPlication libraries doesnot reflect the new class added
How to create a jar file
How to create a jar file  Hello!!!! I have a project which has... a single executable jar of it... so pls tell me how it will b possible for me???   The given code creates a jar file using java. import java.io.*; import

Ads