I don't want to use large programs like Netbeans Eclipse and so on and should only do a small thing with pdfbox (https://pdfbox.apache.org/) but it is not working. Do I have to use maven etc?
I have downloaded pdfbox-2.0.20.jar and pdfbox-app-2.0.20.jar
Added PATH to environment variable in Windows.
Created this (Document_Creation.java) in Notepad++
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
public class Document_Creation {
public static void main (String args[]) throws IOException {
//Creating PDF document object
PDDocument document = new PDDocument();
//Saving the document
document.save("C:/folder/my_doc.pdf");
System.out.println("PDF created");
//Closing the document
document.close();
}
}
Open Windows CDM and type: javac Document_Creation.java
But get this error
Document_Creation.java:2: error: package org.apache.pdfbox.pdmodel does not exist
import org.apache.pdfbox.pdmodel.PDDocument;
^
Document_Creation.java:9: error: cannot find symbol
PDDocument document = new PDDocument();
^
symbol: class PDDocument
location: class Document_Creation
Document_Creation.java:9: error: cannot find symbol
PDDocument document = new PDDocument();
^
symbol: class PDDocument
location: class Document_Creation
3 errors
Looks like I can't access pdfbox with import org.apache.pdfbox.pdmodel.PDDocument;
How can I make this code work with latest java (working, tested with println without pdfbox), Notepad++ and Windows CMD?
As noted here, the solution is in the syntax order, requiring classpath items before the class:
java -cp .;"C:\Program Files\Java\pdfBox\pdfbox-app-2.0.24.jar" Document_Creation
I also put in the full path where I have pdfBox jar located.
---TLDR---
I've used pdfbox years ago as command line tool format with success, but wanted to step up to using it in a java application. After cleaning up stuff (deleting old java installations [to get java and javac on same version], cleaning up paths, figuring out how to create a macro in notepad++ that allowed for compilation and running of the application, and restarting notepad++) to get to the point of this question, I was almost ready to give up until my search result pulled up the solution. Now I feel ready to dive deeper and hopefully get to use pdfBox for my actual application.
I'm currently working with Apachi, as I needed to read data from a XLSX file, and it will later be converted to CSV. Here's the code I'm using to create my XSSFWorkbook, and it is causing an exception every single time. From what I could find, XMLBeans is part of the cause. It has been deprecated, however, it is a dependency of POI in this instance.
public static void appendCSV(File inputFile, String outputFile, String tag)
{
System.out.println(inputFile.getAbsolutePath());
InputStream inp = null;
try {
inp = new FileInputStream(inputFile);
XSSFWorkbook wb = new XSSFWorkbook(inp);
My exception gets thrown at the last line in the block above.
Exception in thread "main" org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:65)
at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:601)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:174)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:279)
at BigBangarang.appendCSV(BigBangarang.java:68)
at BigBangarang.main(BigBangarang.java:268)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)
at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:62)
... 5 more
Caused by: java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setLoadEntityBytesLimit(I)Lorg/apache/xmlbeans/XmlOptions;
at org.apache.poi.POIXMLTypeLoader.<clinit>(POIXMLTypeLoader.java:50)
at org.apache.poi.xssf.model.SharedStringsTable.readFrom(SharedStringsTable.java:127)
at org.apache.poi.xssf.model.SharedStringsTable.<init>(SharedStringsTable.java:108)
... 11 more
Has anybody ran into this situation before? I have the most up to date release of XMLBeans, and I'm almost feeling like I may need to find an older version, as it says a method is missing. I'm not sure if there is an alternate/easier way to either read an XLSX, or to simply convert it to a CSV prior to handling any data.
You either need to upgrade your XMLBeans version to 2.6, or to upgrade you Apache POI version to 3.15 beta 1 or later.
You're hitting Apache POI bug #59195, for which a temporary workaround was applied around a month ago, and is included in the 3.15 beta 1 release. (Also in nightly builds from the time of the commit onwards). A full fix will take a bit longer, follow that bug if you're interested!
I am a beginner of java i want to convert the existing .csv file into .arff file and i have written the below code and its not converting instead i am getting the errors. please can anybody help me in solving these errors and suggest me how
program :
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;
import java.io.File;
import java.io.IOException;
public class meena {
/**
* takes 2 arguments:
* - CSV input file
* - ARFF output file
*/
public static void main(String[] args) throws IOException {
String args0="C:\\Documents and Settings\\CORI\\My Documents\\NetBeansProjects\\trainingset\\build\\classes\\svmlearn\\in.csv ";
String args1="C:\\Documents and Settings\\CORI\\My Documents\\NetBeansProjects\\trainingset\\build\\classes\\svmlearn\\output1.txt";
// load CSV
CSVLoader loader = new CSVLoader();
loader.setSource(new File(args0));
Instances data = loader.getDataSet();
// save ARFF
ArffSaver saver = new ArffSaver();
saver.setInstances(data);
saver.setFile(new File(args[1]));
saver.setDestination(new File(args[1]));
saver.writeBatch();
}
}
I am getting the below error:
---Registering Weka Editors---
Trying to add database driver (JDBC): RmiJdbc.RJDriver - Error, not in CLASSPATH?
Trying to add database driver (JDBC): jdbc.idbDriver - Error, not in CLASSPATH?
Trying to add database driver (JDBC): com.mckoi.JDBCDriver - Error, not in CLASSPATH?
Trying to add database driver (JDBC): org.hsqldb.jdbcDriver - Error, not in CLASSPATH?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at meena.main(meena.java:26)
Java Result: 1
Please help me to convert .csv file into .arff file by suggesting me clearly how and where to pass the input
There are two points here.
On line 26 you use args[1] when I believe you mean to use args1.
Trying to add database driver (JDBC) does not prevent your code from running successfully.
Weka's official reasoning
Stack Overflow answer
I have a code for adding the texts to existing .doc file and it'll save that as another name by using apache POI.
The following is the code I have tried so far
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFooter;
import org.apache.poi.xwpf.usermodel.XWPFTable;
public class FooterTableWriting {
public static void main(String args[])
{
String path="D:\\vignesh\\AgileDocTemplate.doc";
String attch="D:\\Attach.doc";
String comment="good";
String stat="ready";
String coaddr="xyz";
String cmail="abc#gmail.com";
String sub="comp";
String title="Globematics";
String cat="General";
setFooter(path, attch, comment, stat, coaddr, cmail, sub, title, cat);
}
private static void setFooter(String docTemplatePath,String attachmentPath,String comments,String status,String coAddress,String coEmail,String subject,String title,String catagory)
{
try{
InputStream input = new FileInputStream(new File(docTemplatePath));
XWPFDocument document=new XWPFDocument(input);
XWPFHeaderFooterPolicy headerPolicy =new XWPFHeaderFooterPolicy(document);
XWPFFooter footer = headerPolicy.getDefaultFooter();
XWPFTable[] table = footer.getTables();
for (XWPFTable xwpfTable : table)
{
xwpfTable.getRow(1).getCell(0).setText(comments);
xwpfTable.getRow(1).getCell(1).setText(status);
xwpfTable.getRow(1).getCell(2).setText(coAddress);
xwpfTable.getRow(1).getCell(3).setText(coEmail);
xwpfTable.getRow(1).getCell(4).setText(subject);
xwpfTable.getRow(1).getCell(5).setText(title);
xwpfTable.getRow(1).getCell(6).setText(catagory);
}
File f=new File (attachmentPath.substring(0,attachmentPath.lastIndexOf('\\')));
if(!f.exists())
f.mkdirs();
FileOutputStream out = new FileOutputStream(new File(attachmentPath));
document.write(out);
out.close();
System.out.println("Attachment Created!");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
The following is what I got
org.apache.poi.POIXMLException: org.apache.xmlbeans.XmlException: error: The document is not a document#http://schemas.openxmlformats.org/wordprocessingml/2006/main: document element mismatch got themeManager#http://schemas.openxmlformats.org/drawingml/2006/main
at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:124)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:200)
at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:74)
at ext.gt.checkOut.FooterTableWriting.setFooter(FooterTableWriting.java:32)
at ext.gt.checkOut.FooterTableWriting.main(FooterTableWriting.java:25)
Caused by: org.apache.xmlbeans.XmlException: error: The document is not a document#http://schemas.openxmlformats.org/wordprocessingml/2006/main: document element mismatch got themeManager#http://schemas.openxmlformats.org/drawingml/2006/main
at org.apache.xmlbeans.impl.store.Locale.verifyDocumentType(Locale.java:458)
at org.apache.xmlbeans.impl.store.Locale.autoTypeDocument(Locale.java:363)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1279)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1263)
at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
at org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument$Factory.parse(Unknown Source)
at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:92)
... 4 more
I have added all the jar files corresponding to this but still I can't find the solution.I'm new to this apache poi so please help me with some explanations and examples.
Thanks
Copied from my comment done to the question:
Looks like you need poi-ooxml-schemas.jar that comes in the Apache POI distribution. Just adding a single jar doesn't mean that you have all the classes of the framework.
After solving the problem based on my comment (or another people answers), you have this new Exception
org.apache.xmlbeans.XmlException: error: The document is not a document#http://schemas.openxmlformats.org/wordprocessingml/2006/main: document element mismatch got themeManager#http://schemas.openxmlformats.org/drawingml/2006/main
Reading Apache POI - HWPF - Java API to Handle Microsoft Word Files, it looks like you're using the wrong class to handle 2003- word documents: HWPF is the name of our port of the Microsoft Word 97(-2007) file format to pure Java ... The partner to HWPF for the new Word 2007 .docx format is XWPF.. This means that you need HWPFDocument class to handle the document or change your document from Word 2003- to Word 2007+.
IMO I find Apache POI as a good solution to handling Excel files, but I would look another options to handling Word documents. Check this question to get more related info.
This is the dependency hierarchy for poi-ooxml-3.9.jar.
Which means any of them can be used at runtime even if they aren't used at compile-time.
Make sure you have all the jars in the classpath of your project.
Add this dependency on your config file:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
or
System couldn’t find the
poi-ooxml-schemas-xx.xx.jar
Please add the library to your classpath.
The class org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument.Factory is located in the jar ooxml-schemas-1.0.jar which can be downloaded here
You're getting that error because you don't have the proper dependency for the XWPFDocument. ooxml-schemas requires xmlbeans, and ooxml requires poi and ooxml-schemas, etc...
Check here: http://poi.apache.org/overview.html#components
Thought I would report my experience with this error. I started getting it out of the blue, and hadn't changed anything in my workspace. Turns out that it occurs while trying to read an Excel file that has more than 1 sheet (second sheet was a pivot table, large amount of data. Not quit sure if it's due to the size of the data (I suspect so, because I HAVE read Excel files that contain more than 1 worksheet). When I deleted that second sheet, it worked. No changes to classpath needed.
org.apache.poi.POIXMLException: org.apache.xmlbeans.XmlException: Element themeManager#http://schemas.openxmlformats.org/drawingml/2006/main is not a valid workbook#http://schemas.openxmlformats.org/spreadsheetml/2006/main document or a valid substitution.
Solution :- use .xlsx format instead of .xls
FWIW I had to add this:
compile 'org.apache.poi:ooxml-schemas:1.3'
For my case I had different versions of poi(s). poi-scratchpad was of 3.9 and all others - poi, poi-ooxml,poi-ooxml-schemas were of 3.12. I changed version of poi-scratchpad to 3.12 as well and everything started working.
If you are not using maven for your project dependencies. You should have the following jars in your classpath
I am using Aspose.Cells (trial version) to parse a .xls (Excel) file for Java. But when I try to load the file, it throws the exception given below:
SEVERE: java.lang.IllegalStateException: XML Stream Exception: XMLStreamException: com.ctc.wstx.sr.ValidatingStreamReader cannot be cast to com.ctc.wstx.sr.ValidatingStreamReader
Here is my code
Workbook workbook = new Workbook();
try {
workbook.open(path+fileName);
} catch (Exception e) {
e.printStackTrace();
}
Worksheet worksheet = workbook.getWorksheets().get(0);
This exception is coming at workbook.open(path+fileName); this line.I am quiet sure that this is not due to wrong path because when I give wrong path then aspose throws FileNotFoundException.So now I am stuck here and unable to find why this is happening?Note: In search of this problem, I found this answer on aspose forum but it is not helpful and feasible(to check all the classes present in jars placed in lib).
We recommend you to kindly try our latest version of the product (e.g v7.7.x (JAVA)) as we did remove some inter dependencies jars and have written/included our own custom XML parsers to perform some XML operations in the product. In the new versions, we we have removed the conflicting "com.etc.wstx" jar in the product, so you should not find this exception any more.
Thanks,