Jar Executable 我做错了什么? [英] Jar Executable what am I doing wrong?
问题描述
我正在使用 Eclipse IDE 进行编程.遵循 Apache POI 教程后:
I am using the Eclipse IDE to program. After following a tutorial on Apache POI:
https://www.youtube.com/watch?v=RsrF2Ku7ad4
我通过 eclipse 创建了一个可执行的 jar,并从以下链接执行以下步骤:http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-37.htm
I created an executable jar through eclipse and the following steps from the following link: http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-37.htm
从菜单栏的文件菜单中,选择导出.
From the menu bar's File menu, select Export.
展开 Java 节点并选择 Runnable JAR 文件.点击下一步.
Expand the Java node and select Runnable JAR file. Click Next.
在 Runnable JAR 导出向导 Runnable JAR 文件规范页面中,我选择 WriteExcel 启动配置以用于创建可运行 JAR.
In the Runnable JAR export wizard Runnable JAR File Specification page, I select the WriteExcel launch configuration to use to create a runnable JAR.
在导出目标"字段中,我将其放置在我的 Eclipse 工作区中.
In the Export destination field, I placed it in my Eclipse workspace.
然后我选择将所需的库打包到生成的 JAR 文件中.
Then I chose package required libraries into generated JAR files.
然后我将 JAR 文件与 Windows 中的 Java(TM) Platform SE 二进制文件相关联.但是,当我双击它时,它不会像我在 Eclipse 中运行程序那样创建一个 excel 文件.从命令行运行 jar 文件也不会返回错误或创建 excel 文件.
I then associated JAR files with Java(TM) Platform SE binary in windows. However when I double click it doesn't create an excel file like it does when i run the program in Eclipse. Running the jar file from the comand line doesnt return an error or create the excel file either.
显然我做错了什么,非常感谢任何帮助.
Clearly I did something wrong any help is greatly appreciated.
使用的代码与 youtube 教程相同:
edit: Code Used is the same as the youtube tutorial:
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
public class WriteExcel {
public static void main(String[] args) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstExcelSheet");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("1. Cell");
cell = row.createCell(1);
DataFormat format = workbook.createDataFormat();
CellStyle dateStyle = workbook.createCellStyle();
dateStyle.setDataFormat(format.getFormat("dd.mm.yyyy"));
cell.setCellStyle(dateStyle);
cell.setCellValue(new Date());
row.createCell(2).setCellValue("3. Cell");
sheet.autoSizeColumn(1);
workbook.write(new FileOutputStream("excel.xls"));
workbook.close();
}
}
推荐答案
小代码修复后
来自
workbook.write(new FileOutputStream("excel.xls"));
workbook.close();
到
FileOutputStream fos = new FileOutputStream("excel.xls");
workbook.write(fos);
fos.close();
并执行您提到的步骤以生成 Jar 文件.我得到一个包含以下内容的 Jar 文件(jar tf WriteExcel.jar
的结果)
and executing the steps you mention to generate the Jar file. I get a Jar file with following content (result of jar tf WriteExcel.jar
)
META-INF/MANIFEST.MF
org/
org/eclipse/
org/eclipse/jdt/
org/eclipse/jdt/internal/
org/eclipse/jdt/internal/jarinjarloader/
org/eclipse/jdt/internal/jarinjarloader/JIJConstants.class
org/eclipse/jdt/internal/jarinjarloader/JarRsrcLoaderManifestInfo.class
org/eclipse/jdt/internal/jarinjarloader/JarRsrcLoader.class
org/eclipse/jdt/internal/jarinjarloader/RsrcURLConnection.class
org/eclipse/jdt/internal/jarinjarloader/RsrcURLStreamHandler.class
org/eclipse/jdt/internal/jarinjarloader/RsrcURLStreamHandlerFactory.class
WriteExcel.class
XLStoCSVConvert.class
org/apache/
org/apache/poi/
org/apache/poi/ss/
org/apache/poi/ss/examples/
org/apache/poi/ss/examples/UpdateWorkBook.class
org/apache/poi/ss/examples/ToCSVExcelFilenameFilter.class
org/apache/poi/ss/examples/ToCSV.class
org/apache/poi/ss/examples/CreateNewWorkBook.class
poi-ooxml-3.10.1.jar
poi-ooxml-schemas-3.10.1.jar
xmlbeans-2.6.0.jar
stax-api-1.0.1.jar
dom4j-1.6.1.jar
xml-apis-1.0.b2.jar
poi-3.10.1.jar
commons-codec-1.5.jar
文件内容META-INF/MANIFEST.MF
Manifest-Version: 1.0
Rsrc-Class-Path: ./ poi-ooxml-3.10.1.jar poi-ooxml-schemas-3.10.1.jar
xmlbeans-2.6.0.jar stax-api-1.0.1.jar dom4j-1.6.1.jar xml-apis-1.0.b2
.jar poi-3.10.1.jar commons-codec-1.5.jar
Class-Path: .
Rsrc-Main-Class: WriteExcel
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
使用 java -jar WriteExcel.jar
执行 Jar 会生成文件 excel.xls
.
Executing the Jar with java -jar WriteExcel.jar
generates the file excel.xls
.
双击编辑运行
如果在 Windows 资源管理器中双击 Jar 文件时它没有执行,主要原因可能是
edit run with double-click
If it's not executed when you do a double-click on the Jar file in the Windows Explorer the main reasons could be
Jar 文件是使用较新的 JDK 版本编译的,然后是分配用于执行 Jar 文件的 JRE(在两台 PC 上使用
java -version
检查)
分配了错误的应用程序来执行 Jar 文件
在注册表中检查键值 HKEY_CLASSES_ROOT\jarfile\shell\open\command
它必须类似于 "C:\Program Files\Java\jre1.8.0_66\bin\javaw.exe" -jar "%1" %*
(路径取决于安装的版本)
a wrong application has been assigned to execute the Jar file
check in the registry the value of key HKEY_CLASSES_ROOT\jarfile\shell\open\command
it must be similar to "C:\Program Files\Java\jre1.8.0_66\bin\javaw.exe" -jar "%1" %*
(the path depends on the installed version)
这篇关于Jar Executable 我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!