【问题标题】:creating executable jar file for my java application为我的 java 应用程序创建可执行 jar 文件
【发布时间】:2010-03-24 12:35:25
【问题描述】:
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){}
}

等等。它工作正常。 我有 jxl.jar 和 ojdbc14.jar 文件(需要这个 jar 文件来创建 Excelsheet 和 DB 连接)和 createExcel.class(.class 文件)文件。 如何将此代码制作为可执行的 jar 文件。

【问题讨论】:

  • @Manu - 聪明点,使用 maven!
  • @c0mrade: maven 如何帮助处理可执行的 jars(甚至是 windows 中的 .exe 文件)?
  • 在您的 IDE 中执行此操作通常最容易,他们甚至经常为您执行此操作。你在用吗?
  • @Roman,谷歌 Maven jar 插件,它有一个主类选项。
  • @Jason Nichols:据我了解,他的主要问题是还有几个 jars(即依赖项)。并且 maven 不会帮助创建 .bat 或 .sh 文件(恕我直言,这是这种情况下的最佳解决方案)。

标签: java


【解决方案1】:

有几种方法:

  1. 创建一个 jar 文件并将您的类(没有依赖关系)放在那里。使用一些工具(任何 IDE 都有)来执行此操作并使用 main 函数指定类。您也可以从命令行手动执行此操作。当用户想要运行它时,他应该指定类路径,并且所有依赖项都应该在该类路径中。

  2. 创建相同的 jar 并创建 .bat 或 .sh 文件,在其中设置类路径并运行您的 jar。

  3. 使用一些特殊工具创建跨平台安装程序(但好的工具不是免费的)。

【讨论】:

  • @Manu:在你的 IDE 中找到类似“build jar”的东西(谷歌如何在你的 IDE 中做到这一点)。指定主类(我从您的来源了解的 CreateExcel)并构建它。然后你必须解决第二个问题:如何附加依赖项(即 jxl.jar 和其他)。最简单的方法 - 编写文档并在其中提到要运行您的应用程序用户应该设置类路径。类路径应包含所有依赖项。
【解决方案2】:

您需要创建一个包含 .class 和清单文件的 jar。我建议您阅读http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html 并发布其他问题。

【讨论】:

    【解决方案3】:

    您可以通过执行以下命令来创建一个 JAR 文件:

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

    MANIFEST.MF 文件应包含以下行:

    Main-Class: createExcel
    

    【讨论】:

    • 能否请您一步一步解释。我有 jxl.jar 和 ojdbc14.jar 文件和 createExcel.class 文件(供您参考,我在“mypack”包中的源文件并编译以获取 .class 文件)。我需要在清单文件和可执行 jar 文件中输入什么内容。如果我单击 jar 文件,它应该运行应用程序
    • 这是我选择的方法。只要最终用户配置了文件关联,它就可以通过双击文件来工作。 (最新版本的 Java 安装程序将设置文件关联)
    【解决方案4】:

    如果您正在寻找的是创建一个 .jar 文件,那么您可以使用“jar”命令行工具。这是它的描述和选项/用法:

    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/ .
    

    希望对你有帮助。

    【讨论】:

    • 能否请您一步一步解释。我有 jxl.jar 和 ojdbc14.jar 文件和 createExcel.class 文件(供您参考,我在“mypack”包中的源文件并编译以获取 .class 文件)。我需要在清单文件和可执行 jar 文件中输入什么。如果我单击 jar 文件,它应该运行应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 2014-08-20
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多