NoClassDefFoundError while reading a pdf file - java

Iam trying to read a pdf file the code is
try {
File fileConn = new File(filePath);
InputStream inp = new FileInputStream(fileConn);
PdfReader reader = new PdfReader(inp);
int pages = reader.getNumberOfPages();
System.out.println("Pages" + pages);
} catch (Exception e) {
//Handle Exception
}
But the method is throwing NOClassDefFoundError. What coukd be the possible reason

Did you added pdfbox and itextpdf to your classpath?
Try this, if you're using maven:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.0.6</version>
</dependency>

Related

DOCX4j : space removed in pdf by deploying war file into tomcat

I am using below code to convert docx to pdf using DOCX4j.While running this code into IDE it is working fine and space preserved after conversion. For deployment I created war and put it into tomcat server. But into test server space is not preserved into document after conversion.
pom.xml
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.docx4j/docx4j-JAXB-Internal -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-Internal</artifactId>
<version>8.3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.docx4j/docx4j-JAXB-ReferenceImpl -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.docx4j/docx4j-JAXB-MOXy -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-MOXy</artifactId>
<version>8.3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.docx4j/docx4j-export-fo -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>8.3.7</version>
</dependency>
code for conversion
private byte[] docxToPdfBytes() {
InputStream templateInputStream = null;
try {
templateInputStream = new FileInputStream(ApplicationConstants.TEMP_DOCX_PATH);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateInputStream);
FileOutputStream os = new FileOutputStream(ApplicationConstants.TEMP_PDF_PATH);
Docx4J.toPDF(wordMLPackage, os);
os.flush();
os.close();
return Files.readAllBytes(Paths.get(ApplicationConstants.TEMP_PDF_PATH));
} catch (Throwable e) {
logger.info(XrefSubscriberServiceService.class.getName() + "==> Method : docxToPdfBytes");
logger.error(e.getMessage(), e);
}
finally {
if(null != templateInputStream) {
try {
templateInputStream.close();
} catch (IOException e) {
logger.info(XrefSubscriberServiceService.class.getName() + "==> Method : docxToPdfBytes");
logger.error(e.getMessage(), e);
}
}
}
return null;
}
So can anyone have idea why this type of behaviour is happened ??
this question is resolved. Just need to correct docx styling of font in docx document.

java ObjectOutputStream create csv file with gibberish data

I have over 300 tables which i want to export to CSV files. However i see gibberish data.
I send the tablename and the retrieve the data using jdbctemplate I can see the data but when i export I see gibberish values
sr1org.springframework.util.LinkedCaseInsensitiveMapiEkMÑ=f$LcaseInsensitiveKeystLjava/util/HashMap
sr1org.springframework.util.LinkedCaseInsensitiveMapiEkMÑ=f$LcaseInsensitiveKeystLjava/util/HashMap
q~q~q~q~xq~sq~?#wq~sq~q~trivera92q~
Below is the code:
exportData.java
public List getTableData(String tableName){
return jdbcTemplate.queryForList("select * FROM " + tableName + " LIMIT 10");
}
To create csv file
#Autowired
ExportData exportData;
public void CreateCsvData(){
String tableName = "user_details";
String folderPath = "/tmp/Data/";
List data = this.exportData.getTableData(tableName);
try {
FileOutputStream fis = new FileOutputStream(folderPath + tableName + ".csv",true);
ObjectOutputStream os = new ObjectOutputStream(fis);
for (int i = 0; i < data.size(); i++) {
os.writeObject(data.get(i));
os.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
here is pom.xml
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependencies>
I saw many examples with resultsetextractor and RowMapper but its difficult to write models for 300 tables.
Sorry my bad; I did iterate over the object forgot to paste it here;

Converting MultiPartFile to XWPFDocument

I need to know how to convert a multipartFile to a XWPFDocument in order to read it as a word file ( the file that I uploaded is in fact a .docx ) however I receive the error :
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
Here is my code :
#Override
public void addReport(MultipartFile report) throws ApcException {
try{
File convFile = new File(report.getOriginalFilename());
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(report.getBytes());
fos.close();
FileInputStream fis = new FileInputStream(convFile.getAbsolutePath());
XWPFDocument docx = new XWPFDocument(fis);
List<XWPFTable> tables =docx.getTables();
invokeUpdate(ADD_REPORT,new Object[]{
tables.get(0).getRow(1).getCell(0),
tables.get(1).getRow(0).getCell(0),
tables.get(2).getRow(0).getCell(0),
tables.get(3).getRow(2).getCell(1),
tables.get(4).getRow(1).getCell(1)});
}
catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here are my Apache Dependencies in my pom.xml :
For Apache POI i have these :
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
Thank you for your help !
You need to replace your apache poi version from 3.7 to 3.17.
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
The latest stable release is Apache POI 3.17

NPE while receiving messages from Azure Service bus Queue

NPE while receving messages from Queue. ( Only when messages are present in Queue). I feel like there is an issue with de-serializing the messages.
java.lang.NullPointerException
at com.sun.jersey.api.client.ClientResponse.getResponseDate(ClientResponse.java:738)
at com.microsoft.windowsazure.services.servicebus.implementation.ServiceBusRestProxy.receiveMessage(ServiceBusRestProxy.java:288)
at com.microsoft.windowsazure.services.servicebus.implementation.ServiceBusRestProxy.receiveQueueMessage(ServiceBusRestProxy.java:225)
at com.microsoft.windowsazure.services.servicebus.implementation.ServiceBusExceptionProcessor.receiveQueueMessage(ServiceBusExceptionProcessor.java:142)
RECEIVE_AND_DELETE option deletes the messages and throws NPE.
All other operations like create queue , send messages etc working fine. Any thoughts one this ?
Code to receive message
public void receiveMessage(String queueName) {
try {
ReceiveMessageOptions opts = ReceiveMessageOptions.DEFAULT;
opts.setReceiveMode(ReceiveMode.PEEK_LOCK);
while (true) {
ReceiveQueueMessageResult resultQM
= service.receiveQueueMessage(queueName, opts);
BrokeredMessage message = resultQM.getValue();
if (message != null && message.getMessageId() != null) {
log.println("MessageID: " + message.getMessageId());
// Display the queue message.
log.print("From queue: ");
byte[] b = new byte[200];
String s = null;
int numRead = message.getBody().read(b);
while (-1 != numRead) {
s = new String(b);
s = s.trim();
System.out.print(s);
numRead = message.getBody().read(b);
}
log.println("");
log.println("Custom Property: "
+ message.getProperty("MyProperty"));
// Remove message from queue.
log.println("Deleting this message.");
//service.deleteMessage(message);
} else {
log.println("Finishing up - no more messages.");
break;
// Added to handle no more messages.
// Could instead wait for more messages to be added.
}
}
} catch (Exception e) {
log.print(e);
}
}
Even though it was throwing NPE the actual issue was with the missing jar files. Here is the list of jar files
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-compute</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-network</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-sql</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-storage</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-websites</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-media</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-servicebus</artifactId>
<version>0.9.7</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-serviceruntime</artifactId>
<version>0.9.7</version>
</dependency>
</dependencies>

Classes not found in maven project despite the library being included in the pom

I'm trying to run this open-source project, rokuality-server (full codebase here: https://github.com/rokuality/rokuality-server).
But in this method below I'm getting a java.lang.NoClassDefFoundError when trying to instantiate any Sikuli classes like Pattern or Finder.
import org.sikuli.script.Finder;
import org.sikuli.script.Image;
import org.sikuli.script.Match;
import org.sikuli.script.Pattern;
#SuppressWarnings("unchecked")
public class ImageUtils {
private static JSONObject getElement(String sessionID, String locator, File screenImage) {
JSONObject element = new JSONObject();
element.put(SessionConstants.ELEMENT_ID, UUID.randomUUID().toString());
boolean isFile = locator != null && new File(locator).exists();
boolean elementFound = false;
if (!screenImage.exists()) {
return null;
}
if (isFile) {
Finder finder = null;
float similarity = Float.valueOf(
SessionManager.getSessionInfo(sessionID).get(SessionConstants.IMAGE_MATCH_SIMILARITY).toString());
Pattern pattern = null;
try {
//******** THIS LINE BELOW THROWS THE ERROR ********
pattern = new Pattern(locator).similar(similarity);
finder = new Finder(screenImage.getAbsolutePath());
} catch (Exception e) {
Log.getRootLogger().warn(e);
}
}
// more code here
}
}
My suspicion is that something in the pom.xml file is wrong, so here's the Sikuli X Api dependency as it appears there:
<dependency>
<groupId>com.sikulix</groupId>
<artifactId>sikulixapi</artifactId>
<version>1.1.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
</exclusion>
<exclusion>
<groupId>com.github.vidstige</groupId>
<artifactId>jadb</artifactId>
</exclusion>
<exclusion>
<groupId>com.sikulix</groupId>
<artifactId>sikulix2tigervnc</artifactId>
</exclusion>
</exclusions>
</dependency>
I tried changing the version to the latest one, 2.0.0 but it caused some errors in the project, which I think are related to changes in the org.sikuli.script.Image class's methods. Do I maybe need an earlier version?
This should be fixed in the newer releases of the rokuality project:
https://github.com/rokuality/rokuality-server/releases. It depended on the java jdk version the user was running.

Categories

Resources