Jar可执行文件我做错了什么? [英] Jar Executable what am I doing wrong?

查看:297
本文介绍了Jar可执行文件我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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


  1. 从菜单栏的文件菜单中选择导出。

  1. From the menu bar's File menu, select Export.

展开Java节点并选择Runnable JAR文件。单击Next。

Expand the Java node and select Runnable JAR file. Click Next.

在Runnable JAR导出向导的Runnable JAR文件规范页面中,我选择用于创建可运行JAR的WriteExcel启动配置。

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.

然后我在Windows中将JAR文件与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

执行Jar -jar WriteExcel.jar 生成文件 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可执行文件我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆