I am struggling to read excel file in a dynamic web project I am developing. I could easily read it in a java application by following the same steps.
What I did?
I added these to build path
xmlbeans-2.6.0.jar
poi-3.13-20150939
poi-examples
poi-excelant
poi-ooxml
poi-ooxml-schemas
poi-scratchpad
Code:
FileInputStream input_document = new FileInputStream(new File("file_location"));
XSSFWorkbook my_xls_workbook = new XSSFWorkbook(input_document);
XSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0);
XSSFCell cell=null;
for(int i=0;i<463;i++){
cell = my_worksheet.getRow(i).getCell(0);
words[i]=cell.getStringCellValue();
System.out.println(words[i]);
}
my_xls_workbook.close();
The Error
SEVERE: StandardWrapper.Throwable
java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook
org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet vhealth.GetMed
java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook
It is pretty long but this is the gist of it
The Question
I want to know how to solve this and as I mentioned it works in another similar java file after following the same steps. I want to integrate it in the web app. I've also tried calling the routine which imports the database in Java from the WebApp(which uses tomcat if that helps) still no luck.
You need to add jar files not only in the build path. You need obviously to have the same files in your war module in the WEB-INF/lib folder.
P.S. I think you don't need poi-examples jar.
Related
I have a java code snippet
TitanGraph g = TitanFactory.open("titan-all-0.4.4/conf/titan-berkeleydb.properties");
where titan-berkeleydb.properties is the in build config file that comes with Titan db installation.
On execution, it throws an exception
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.lang.StringUtils.isNotBlank(Ljava/lang/String;)Z
at com.thinkaurelius.titan.graphdb.configuration.KCVSConfiguration.<init>(KCVSConfiguration.java:40)
at com.thinkaurelius.titan.diskstorage.Backend.initialize(Backend.java:273)
at com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration.getBackend(GraphDatabaseConfiguration.java:1174)
at com.thinkaurelius.titan.graphdb.database.StandardTitanGraph.<init>(StandardTitanGraph.java:75)
at com.thinkaurelius.titan.core.TitanFactory.open(TitanFactory.java:40)
at com.thinkaurelius.titan.core.TitanFactory.open(TitanFactory.java:29)
Is there a way to resolve this issue?
Check your classpath for another commons-lang-*.jar. According to the Apache Commons documentation, the isBlank() method is new since 2.0.
https://commons.apache.org/proper/commons-lang/javadocs/api-2.5/org/apache/commons/lang/StringUtils.html#isBlank%28java.lang.String%29
Titan 0.4.4 ships with commons-lang-2.5.jar in the lib directory. Your original post didn't mention anything else about the runtime environment. I would suspect that if you had, for example, commons-lang-1.0.1.jar on the classpath, it would be picked up before commons-lang-2.5.jar and you would see the error.
If you're using a web application with Tomcat, you have to add the jar file into WEB-INF/lib folder of the application (or into $TOMCAT_HOME/lib folder, if you have more webapps using it).
Either create a library with this jar and add it to the project class path.
If you done all this and it still not working, you can change the jar extension to zip or rar and open it to see if the requested classes you need are inside.
I have a web app that uses JasperReports to power data export to Excel, PDF & CSV. It's a grails app using the DynamicJasperReports plugin and it uses a jrxml file as the template for the report.
This was working but has broken following some changes to our tomcat configuration. It still works locally in grails development mode.
FastReportBuilder drb = new FastReportBuilder()
drb.setTemplateFile("resources/reportTemplate.jrxml")
DynamicReport report = reportBuilder.call(drb)
JRDataSource ds = new JRMapCollectionDataSource(data)
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(report, new ClassicLayoutManager(), ds)
ReportWriter reportWriter = ReportWriterFactory.getInstance().getReportWriter(jp, format, [:])
reportWriter.writeTo(response)
It throws this error:
java.lang.NullPointerException
at ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperDesign(DynamicJasperHelper.java:151)
at ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperReport(DynamicJasperHelper.java:448)
at ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperPrint(DynamicJasperHelper.java:234)
at ar.com.fdvs.dj.core.DynamicJasperHelper.generateJasperPrint(DynamicJasperHelper.java:187)
at com.giivpro.services.analytics.JasperReportsService.runReport(JasperReportsService.groovy:30)
I eventually realized this was not due to upgrading to tomcat7 as I originally thought but instead was due to changes in how the app was being deployed, made around the same time.
All the jar file dependencies of the app are now copied to /var/lib/tomcat/lib rather than being deployed inside the war file. Which means that the line in DynamicJasperHelper
URL url = DynamicJasperHelper.class.getClassLoader().getResource(dr.getTemplateFileName());
now failed due to security restrictions on the ClassLoader. The jars in /var/lib/tomcat/lib get loaded by a separate ClassLoader and are not able to load resources from specific WAR files.
Need to change the old HSSFWorkbook based code XSSFWorkbook
Towards the task, i have added poi-3.9, poi-ooxml-schemas-3.9, poi-ooxml-3.9 jars to my build path and changed the code as suggested in
http://poi.apache.org/spreadsheet/converting.html
at XSSFWorkbook wb = new XSSFWorkbook() the flow control comes out of the flow at this line.
I try debugging the file, and observed that it was not able to create an instance for XSSFWorkbook.
But after reading from below url,
http://howtodoinjava.com/2013/06/19/readingwriting-excel-files-in-java-poi-tutorial/
i have added xmlbeans-2.3.0 jar, should i also add dom4j.jar also for this?
but still the same issue, control comes out from the line creation of instance for XSSFWorkbook. can any one please help me on this?
The issue is solved, after adding the dependent jar dom4j. Consolidating the details of jars needed for Apache POI 3.9 for XLSX format files to export are as follows,
Poi-,
poi-ooxml,
poi-ooxml-schems,
dom4j
InputStream inp = new FileInputStream("src/main/resources/ExportHour.xls");
I have a file in the src/main/resources folder of my Java Spring project.
I am attempting to create an inputstream in one of my Controllers, however I always get a file not found exception. When I change the path location to point specifically to the file on my machine, it works fine.
Any way I can make it so the file can be found within the java project?
Try with spring ClassPathResource.
InputStream inp = new ClassPathResource("ExportHour.xls").getInputStream();
That is because the resources folder in maven is put in your jar file directly i.e. the ExportHours.xls file is put inside your jar in the root directory.
It sounds like you could just change the working directory of your process - it's not where you think it is, I suspect. For example, I suggest you write
File file = new File("src/main/resources/ExportHour.xls");
and then log file.getAbsolutePath(), to see what exact file it's using.
However, you should almost certainly not be using a FileInputStream anyway. It would be better to use something like:
InputStream inp = Foo.class.getResourceAsStream("/ExportHour.xls");
... for some class Foo which has a classloader which includes the resources you need.
(Or possibly /resources/ExportHour.xls", depending on your build structure.)
That way even when you've built all of this into a jar file, you'll still be able to open the resource.
I have written a program which reads input from csv file and its working fine. I read the inputstream of the csv file as follows.
BufferedReader br=new BufferedReader(new InputStreamReader(item.getInputStream()));
Now I am changing the program so as to read input from excel file. So I changed the code to this format and while compilation there are no errors.
POIFSFileSystem ps = new POIFSFileSystem(item.getInputStream());
HSSFWorkbook workbook = new HSSFWorkbook(ps);
But while submitting the JSP page, I get the following error. What must be the problem and where I have went wrong? Please advise.
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.NoClassDefFoundError: org/apache/poi/poifs/filesystem/POIFSFileSystem
Readcsvv.doPost(Readcsvv.java:120)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Your POI jars are not in the classpath. Put them in the WEB-INF/lib folder.
Another less likely reason could be that your version of jars is different than those expected.
It looks like you've forgotten to include the POI library on the CLASSPATH (e.g. It's not referenced by your web app). You can include this lib under WEB-INF/lib (in your WAR archive or in the exploded directory format) or it can sit elsewhere in your app server/web server CLASSPATH