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
From the menu bar's File menu, select Export.
Expand the Java node and select Runnable JAR file. Click Next.
In the Runnable JAR export wizard Runnable JAR File Specification page, I select the WriteExcel launch configuration to use to create a runnable JAR.
In the Export destination field, I placed it in my Eclipse workspace.
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();
}
}
After a small code fix
from
to
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
)content of the file
META-INF/MANIFEST.MF
Executing the Jar with
java -jar WriteExcel.jar
generates the fileexcel.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)