Jar Executable what am I doing wrong?

2019-09-02 05:29发布

问题:

I am using the Eclipse IDE to program. After following a tutorial on Apache POI:

https://www.youtube.com/watch?v=RsrF2Ku7ad4

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. From the menu bar's File menu, select Export.

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

  3. In the Runnable JAR export wizard Runnable JAR File Specification page, I select the WriteExcel launch configuration to use to create a runnable JAR.

  4. In the Export destination field, I placed it in my Eclipse workspace.

  5. Then I chose package required libraries into generated JAR files.

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.

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

回答1:

After a small code fix

from

workbook.write(new FileOutputStream("excel.xls"));
workbook.close();

to

FileOutputStream fos = new FileOutputStream("excel.xls");
workbook.write(fos);
fos.close();

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

content of the file 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

Executing the Jar with java -jar WriteExcel.jar generates the file excel.xls.

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

  • the Jar file was compiled with a newer JDK version then the JRE which is assigned to execute Jar files (check with java -version on both PCs)

  • 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)