Reading Excel File using POI jar file - java

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

Related

How to include external TXT and XML resources to Runnable Jar export in Eclipse?

These are the 2 files I need to make my program work (customers.txt & log4j2.xml). They are placed directly in the project folder.
This is the execution of the Jar file in the command prompt. As you can see, it cannot find either customers.txt or log4j2.xml.
This was what happened when I exported my program into a Runnable Jar file. I don't know what this error means.
Running the program in Eclipse works fine, but error occurs when exported as Jar file. What is the fix? Thank you very much.
In case it matters, Basically what my program does is:
1. Read the customer.txt file using BufferedReader(new FileReader(inputFile));
2. Do some stuff
3. Output a new .txt file using PrintWriter(new FileWriter(fileName));
4. Log some stuff using the log4j2.xml configuration file.
5. Output a log file.

How to access a ClassPath ressource from a jar?

I am writing a Spring Boot web app.
In my app I need to be able to download a zip file that is packaged into the executable application .jar.
I am using ClassPathResource to load the stream of that file:
Resource applier=new ClassPathResource("applier/com.itnsa.patch.applier-1.0.25-SNAPSHOT-package.zip");
if (applier.exists()) {//do stuff}
The zip file is located in /src/main/resources/applier.
In some other classes of my app I already use this method to retrieve some .txt files from /src/main/resources/exception and everything works correctly. When I try to access the zip the exists method returns false.
What am I doing wrong in accessing the zip archive? How can I achieve this?
Resource applier=new ClassPathResource("applier/com.itnsa.patch.applier-1.0.25-SNAPSHOT-package.zip");
if (applier.exists()) {//do stuff}
It should work, i tried same file name and same folder structure it returns true, Make sure that jar file is on class path.
If you are doing/working it with any IDE make sure jar file is on classpath.
There is also another way you can utilize given below, but this is not the case for you
InputStream in = getClass().getResourceAsStream("/fileName.zip");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

Read excel fie in java using poi

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.

RandomAccessFile to read files in a Jar file

I am having problem to use JWNL wordnet in a Jar file.
JWNL uses RandomAccessFile to read wordnet dictionary files. In order to create a Jar file, wordnet dictionary files are put in resources/wordnet folder. As resources is in my Build Path, I have no problem to run the application I created in Eclipse. However, when I use another application to run the created jar file, I get the following error:
java.io.FileNotFoundException: resources/wordnet/data.noun (No such file or directory)
from the following code:
RandomAccess _file = new RandomAccessFile(path, _permissions);
I use the following code to check the current working directory:
URL location = PrincetonRandomAccessDictionaryFile.class.getProtectionDomain().getCodeSource().getLocation();
System.out.println(location.getFile());
It seems both situation have the same location: /project/bin/
How should I fix the problem? Thank you
The key information you seem to be missing is that Jar files are compressed, and you can't "seek" because of the compression (which is I believe the DEFLATE algorithm).
However, you could extract the file(s) into temp file(s) on start and then use that. Temp files would be removed on application exit, and are the best answer I can think of.
RandomAccessFile to read files in a Jar file
There are no files in a JAR file. There are JAR entries. You can't read them with FileInputStreams, RandomAccessFiles, or FileReaders.You need to use a JarInputStream or its friends.

java read file for ftp upload from jar file

I am trying to upload files by ftp using the apache common library. I want to keep those file inside the jar, since these are files that will stay the same and need to be uploaded on the different hosts several time.
I am trying the following:
public void doCGIRecovery() throws IOException{
System.out.println(getClass().getResource("/cgi/example").getPath());
FileInputStream file = new FileInputStream(getClass().getResource("/cgi/example").getPath());
f.storeFile("./", file);
}
Using eclipse this works and the sysout is giving me:
/Users/user/Documents/workspace/example-Project/bin/com/example/java/example/business/cgi/example
But after the compilation into a jar it returns me the following:
file:/Users/user/Desktop/example.jar!/com/example/java/example/business/cgi/example
And I get (No such file or directory).
So how does that come? Further where does the "!" after example.jar come from?
Best Regards
Don't worry about messing with the path, use getClass().getResourceAsStream() instead.

Categories

Resources