2
public class createExcel {

public  void write() throws IOException, WriteException {

        WorkbookSettings wbSettings = new WorkbookSettings();
        wbSettings.setLocale(new Locale("en", "EN"));
        WritableWorkbook workbook1 =Workbook.createWorkbook(new File(file), wbSettings);
        workbook1.createSheet("Niru ", 0);
        WritableSheet excelSheet = workbook1.getSheet(0);
        createLabel(excelSheet);
        createContent(excelSheet,list);
        workbook1.write();
        workbook1.close();
    }


 public void createLabel(WritableSheet sheet)throws WriteException {

WritableFont times10pt = new WritableFont(WritableFont.createFont("D:\font\trebuct"),8);

// Define the cell format

        times = new WritableCellFormat(times10pt);
        // Lets automatically wrap the cells
        times.setWrap(false);
        WritableFont times10ptBoldUnderline = new WritableFont(
        WritableFont.createFont("D:\font\trebuct"), 9, WritableFont.BOLD, false,
        UnderlineStyle.NO_UNDERLINE);
        timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);

        sheet.setColumnView(0,15);
                sheet.setColumnView(1,13);

        // Write a few headers
        addCaption(sheet, 0, 0, "Business Date");
        addCaption(sheet, 1, 0, "Dealer ID");


    }
        private void createContent(WritableSheet sheet, ArrayList list) throws WriteException,RowsExceededException {
                // Write a few number
        for (int i = 1; i < 11; i++) {
                    for(int j=0;j<11;j++){
            // First column
            addNumber(sheet, i, j,1);
            // Second column
            addNumber(sheet, 1, i, i * i);
                    }
        }
            }
private void addCaption(WritableSheet sheet, int column, int row, String s)     throws RowsExceededException, WriteException {

        Label label;
        label = new Label(column, row, s, timesBoldUnderline);
                sheet.addCell(label);
    }

    private void addNumber(WritableSheet sheet,  int row,int column,
            Integer integer) throws WriteException, RowsExceededException {
        Number number;
        number = new Number(column,row, integer, times);
        sheet.addCell(number);
    }


public static void main(String[] args) {

        JButton myButton0 = new JButton("Advice_Report");
        JButton myButton1 = new JButton("Position_Report");
        JPanel bottomPanel = new JPanel();   
        bottomPanel.add(myButton0);
        bottomPanel.add(myButton1);  
        myButton0.addActionListener(this);
        myButton1.addActionListener(this);  
        createExcel obj=new  createExcel();
        obj.setOutputFile("c;\\temp\\swings\\jack.xls");
        try{
        obj.write();
        }catch(Exception e){}
}

and so on. it working fine. i have jxl.jar and ojdbc14.jar files(need this jar file for Excelsheet creation and DB connection )and createExcel.class(.class file) file. how to make this code as executable jar file.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
Manu
  • 3,179
  • 23
  • 57
  • 69
  • @Manu - Be smart , use maven ! – ant Mar 24 '10 at 12:51
  • @c0mrade: how maven can help with executable jars (or even .exe files in windows)? – Roman Mar 24 '10 at 12:59
  • It's usually easiest to do this from within your IDE, they often even do it for you. Are you using one? – Jason Nichols Mar 24 '10 at 12:59
  • @Roman, Google the Maven jar plugin, which has an option for main-class. – Jason Nichols Mar 24 '10 at 13:02
  • @Jason Nichols: as I understand his main problem is that there are several another jars (i.e. dependencies). And maven won't help to create .bat or .sh files (which IMHO is the best solution in this situation). – Roman Mar 24 '10 at 13:21
  • Possible dup: http://stackoverflow.com/questions/2288440/how-to-create-a-java-application-which-can-be-run-by-a-click/2288921#2288921 – ewernli Mar 24 '10 at 13:35

4 Answers4

2

There are several ways:

  1. Create a jar file and put your classes (without dependencies) there. Use some tool (any IDE has it) to do this and specify class with main function. You can also do it manually from command-line. When user want to run it he should specify classpath and all dependencies should be in that classpath.

  2. Create the same jar and create .bat or .sh file in which set classpath and run your jar.

  3. Create cross-platform installer with some special tool (but good tools aren't free).

Roman
  • 64,384
  • 92
  • 238
  • 332
  • @Manu: find in your IDE something like "build jar" (google how to do that in your IDE). Specify main class (CreateExcel as I understand from your sources) and build it. Then you'll have to solve second problem: how to attach dependencies (i.e. jxl.jar and other). The easies way - write documentation and mention there that to run your app user should set classpath. Classpath should include all dependencies. – Roman Mar 24 '10 at 13:26
1

You need to create a jar that contains your .class and a manifest file. I suggest you read http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html and post additional questions.

Manuel Darveau
  • 4,585
  • 5
  • 26
  • 36
1

You create a JAR-file by executing following command:

jar -cvfm excel.jar MANIFEST.MF *.class

The MANIFEST.MF-file should contain following line:

Main-Class: createExcel
djangofan
  • 28,471
  • 61
  • 196
  • 289
Dishayloo
  • 390
  • 2
  • 8
  • can you please explain in step by step. i have jxl.jar and ojdbc14.jar files and createExcel.class file(for your info my source file in "mypack" package and compiled to get .class file). what are the things i need to enter in manifest file and in executable jar file. if i click jar file it should run the application – Manu Mar 24 '10 at 13:16
  • This is my method of choice. As long as the end-user has the file association configured, it will work by double clicking the file. (recent versions of Java installer will setup the file assocation) – djangofan Nov 28 '12 at 23:44
0

If what you're looking for is creating a .jar file, then you can use the "jar" command line tool. Here's its description and options/usage:

Usage: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Options:
    -c  create new archive
    -t  list table of contents for archive
    -x  extract named (or all) files from archive
    -u  update existing archive
    -v  generate verbose output on standard output
    -f  specify archive file name
    -m  include manifest information from specified manifest file
    -e  specify application entry point for stand-alone application
        bundled into an executable jar file
    -0  store only; use no ZIP compression
    -M  do not create a manifest file for the entries
    -i  generate index information for the specified jar files
    -C  change to the specified directory and include the following file
If any file is a directory then it is processed recursively.
The manifest file name, the archive file name and the entry point name are
specified in the same order as the 'm', 'f' and 'e' flags.

Example 1: to archive two class files into an archive called classes.jar:
       jar cvf classes.jar Foo.class Bar.class
Example 2: use an existing manifest file 'mymanifest' and archive all the
           files in the foo/ directory into 'classes.jar':
       jar cvfm classes.jar mymanifest -C foo/ .

I hope that helps.

Juan
  • 1,428
  • 2
  • 22
  • 30
  • can you please explain in step by step. i have jxl.jar and ojdbc14.jar files and createExcel.class file(for your info my source file in "mypack" package and compiled to get .class file). what are the things i need to enter in manifest file and in executable jar file. if i click jar file it should run the application. – Manu Mar 24 '10 at 13:04