PDF reader not working - pdf

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.file.Files;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
`
public class Test{
public static void main(String []args) throws IOException
{
String pdf= "c:\\sample.pdf";
PdfReader reader = new PdfReader(pdf);
}
}
it not working
like it should i am running windows
need help please help i tried a lot of things but still getting the same message
here is the error message
this is the output i get when i tried ur code
File Exists: true
Exception in thread "main" java.lang.NoClassDefFoundError:
org/bouncycastle/asn1/ASN1Encodable
at com.itextpdf.text.pdf.PdfEncryption.<init>(PdfEncryption.java:148)
at com.itextpdf.text.pdf.PdfReader.readDecryptedDocObj(PdfReader.java:1024)
at com.itextpdf.text.pdf.PdfReader.readDocObj(PdfReader.java:1430)
at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:732)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:181)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:219)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:207)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:197)
at pdfconverter.Test.main(Test.java:37)
Caused by: java.lang.ClassNotFoundException:
org.bouncycastle.asn1.ASN1Encodable
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 9 more

The code looks good to me. Perhaps more information on the error you are having? For instance, it could be a classpath error - something like the itextpdf classes not being located...
In case it helps as a baseline - the following code works for me. I removed the extraneous includes, though they won't hurt to leave them in.
import java.io.File;
import com.itextpdf.text.pdf.PdfReader;
public class Test {
public static void main(String[] args) {
String pdf= "C:\\Java-Design-Patterns.pdf";
try {
System.out.println("File Exists: "+new File(pdf).exists());
PdfReader reader = new PdfReader(pdf);
int count = reader.getNumberOfPages();
System.out.println("PDF has "+count+" pages.");
} catch (Exception e) {
System.out.println("Failed to open PDF ["+pdf+"]: "+e);
e.printStackTrace();
}
}
}
The output is:
File Exists: true
PDF has 183 pages.
The itext jar I used is: itextpdf-5.5.12.jar (included via maven).
The pdf I used (courtesy of google: java design patterns pdf) is here: http://enos.itcollege.ee/~jpoial/java/naited/Java-Design-Patterns.pdf
I haven't read it yet, but the first page looks good ;)
That said, itextpdf is quite awesome.

java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1Encodable
clearly indicates the issue: Bouncy Castle is missing! (Or at least the required version is missing.)
Bouncy Castle is a library used by iText for encryption, decryption, signing, and signature verification, here their web representation.
Thus, add the Bouncy Castle libraries to your class path. Please be aware, though, that the BC version required depends on the iText version in question. The maven link you provided for your itext7 version indicates that BC 1.49 is required.

Related

lucene java11 WARNING?

I have this really simple java Lucene code that works in my friends PC but it makes a lot of errors for me ... we all use Lucene 5.3.1, I think that the problem is my jdk11
package project;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class Act1 {
public static void main(String[] args) throws
IOException {
Document doc = new Document();
Field titre = new TextField("Titre", "mon A staticestimation technique",Field.Store.YES);
Field ref = new IntField("Year", 2001,Field.Store.YES);
Field auteurs = new TextField("Auteurs","TaewhanKim, Ki-Seok Chung, C. L. Liu",Field.Store.YES);
doc.add(titre);
doc.add(ref);
doc.add(auteurs);
Path p = Paths.get("C:\\Users\\user\\Desktop\\abc");
Directory dir = FSDirectory.open(p);
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter index = new IndexWriter(dir, config);
index.addDocument(doc);
index.close();
}
}
but it make those errors
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.lucene.store.MMapDirectory$1 (file:/C:/Users/user/Downloads/lucene-5.3.1/core/lucene-core-5.3.1.jar) to method java.nio.DirectByteBuffer.cleaner()
WARNING: Please consider reporting this to the maintainers of org.apache.lucene.store.MMapDirectory$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Exception in thread "main" java.io.IOException: Unable to unmap the mapped buffer: MMapIndexInput(path="C:\Users\user\Desktop\abc\_1_Lucene50_0.doc")
at org.apache.lucene.store.MMapDirectory$2.freeBuffer(MMapDirectory.java:329)
at org.apache.lucene.store.ByteBufferIndexInput.freeBuffer(ByteBufferIndexInput.java:376)
at org.apache.lucene.store.ByteBufferIndexInput.close(ByteBufferIndexInput.java:355)
at org.apache.lucene.codecs.lucene50.Lucene50CompoundFormat.write(Lucene50CompoundFormat.java:92)
at org.apache.lucene.index.IndexWriter.createCompoundFile(IndexWriter.java:4680)
at org.apache.lucene.index.DocumentsWriterPerThread.sealFlushedSegment(DocumentsWriterPerThread.java:492)
at org.apache.lucene.index.DocumentsWriterPerThread.flush(DocumentsWriterPerThread.java:459)
at org.apache.lucene.index.DocumentsWriter.doFlush(DocumentsWriter.java:503)
at org.apache.lucene.index.DocumentsWriter.flushAllThreads(DocumentsWriter.java:615)
at org.apache.lucene.index.IndexWriter.doFlush(IndexWriter.java:3121)
at org.apache.lucene.index.IndexWriter.flush(IndexWriter.java:3096)
at org.apache.lucene.index.IndexWriter.shutdown(IndexWriter.java:1078)
at org.apache.lucene.index.IndexWriter.close(IndexWriter.java:1123)
at projet.Act1.main(Act1.java:31)
Caused by: java.lang.IllegalAccessException: class org.apache.lucene.store.MMapDirectory$2$1 cannot access class jdk.internal.ref.Cleaner (in module java.base) because module java.base does not export jdk.internal.ref to unnamed module #6e8dacdf
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591)
at java.base/java.lang.reflect.Method.invoke(Method.java:558)
at org.apache.lucene.store.MMapDirectory$2$1.run(MMapDirectory.java:322)
at org.apache.lucene.store.MMapDirectory$2$1.run(MMapDirectory.java:314)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at org.apache.lucene.store.MMapDirectory$2.freeBuffer(MMapDirectory.java:314)
... 13 more
please help me to fix this problem ...
and plz if there is anyone who is good in java and Lucene help I'm struggling I have a mini java/Lucene project and because of covid19 we didn't had any course... as you can see I can't even write a simple code

How to create a cucumber-java custom formatter to get cucumber tags

I have a cucumber project and I want to get al the tags in the project to be able to choose them as parameters.
I found this question where cucumber had an option to get the tags but I found that doesn't work anymore, then I found this other question where I found I need a custom formatter to get my tags, but it is for ruby, and I need it for Java, so then I found this article on how to create a custom formatter, but I found that this worked for the cukes version and I'm using the io one.
So I searched inside the cucumber packages and created a custom formatter from a copy of the JSONFormatter inside the package cucumber.runtime.formatter, here is my code:
import cucumber.api.TestCase;
import cucumber.api.event.*;
import cucumber.api.formatter.Formatter;
import cucumber.api.formatter.NiceAppendable;
import gherkin.deps.com.google.gson.Gson;
import gherkin.deps.com.google.gson.GsonBuilder;
import gherkin.pickles.PickleTag;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TagsFormatter implements Formatter {
private String currentFeatureFile;
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final NiceAppendable out;
private List<String> tags = new ArrayList<>();
private EventHandler<TestCaseStarted> caseStartedHandler = this::handleTestCaseStarted;
private EventHandler<TestRunFinished> runFinishedHandler = event -> finishReport();
public TagsFormatter(Appendable out) {
this.out = new NiceAppendable(out);
}
#Override
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestCaseStarted.class, caseStartedHandler);
publisher.registerHandlerFor(TestRunFinished.class, runFinishedHandler);
}
private void handleTestCaseStarted(TestCaseStarted event) {
if (currentFeatureFile == null || !currentFeatureFile.equals(event.testCase.getUri())) {
currentFeatureFile = event.testCase.getUri();
collectTags(event.testCase);
}
}
private void finishReport() {
out.append(gson.toJson(tags));
out.close();
}
private void collectTags(TestCase testCase) {
testCase.getTags();
tags.addAll(testCase.getTags()
.stream()
.map(PickleTag::getName)
.collect(Collectors.toList()));
}
}
I copied the libraries I need to run cucumber in a lib folder inside my project and tried running it using my formatter like this:
java -cp .\lib\cucumber-core-2.4.0.jar;.\lib\gherkin-5.0.0.jar;.\lib\cucumber-java-2.4.0.jar;.\lib\cucumber-jvm-deps-1.0.6.jar cucumber.api.cli.Main -p "com.myproject.formatters.TagsFormatter:tags.txt"
But Im getting a class not found exception:
λ java -cp .\lib\cucumber-core-2.4.0.jar;.\lib\gherkin-5.0.0.jar;.\lib\cucumber-java-2.4.0.jar;.\lib\cucumber-jvm-deps-1.0.6.jar cucumber.api.cli.Main -p "com.myproject.formatters.TagsFormatter:tags.txt"
Exception in thread "main" cucumber.runtime.CucumberException: Couldn't load plugin class: com.myproject.formatters.TagsFormatter
at cucumber.runtime.formatter.PluginFactory.loadClass(PluginFactory.java:181)
at cucumber.runtime.formatter.PluginFactory.pluginClass(PluginFactory.java:166)
at cucumber.runtime.formatter.PluginFactory.getPluginClass(PluginFactory.java:223)
at cucumber.runtime.formatter.PluginFactory.isFormatterName(PluginFactory.java:201)
at cucumber.runtime.RuntimeOptions$ParsedPluginData.addPluginName(RuntimeOptions.java:471)
at cucumber.runtime.RuntimeOptions.parse(RuntimeOptions.java:157)
at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:115)
at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:108)
at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:100)
at cucumber.api.cli.Main.run(Main.java:31)
at cucumber.api.cli.Main.main(Main.java:18)
Caused by: java.lang.ClassNotFoundException: com.myproject.formatters.TagsFormatter
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at cucumber.runtime.formatter.PluginFactory.loadClass(PluginFactory.java:174)
... 10 more
So, how can I create this formatter in a way it is recognized? or at least get the tags list from cucumber from console?
Thanks
By just eyeballing your code, I don't think there is anything wrong with it. However your command does not appear to include a compiled version of the TagsFormatter on the class path.
If your compiled sources are in .\bin\ make sure to include that folder ie:
java -cp .\lib\cucumber-core-2.4.0.jar;.\lib\gherkin-5.0.0.jar;.\lib\cucumber-java-2.4.0.jar;.\lib\cucumber-jvm-deps-1.0.6.jar;.\bin\* cucumber.api.cli.Main -p "com.myproject.formatters.TagsFormatter:tags.txt"

Domino Java Agent Error

When i try an java agent to run i get this error on java console. Target property of agent is None. Trigger is "On Event, "Action Menu Selected".I do ot know what i have to do to solve this problem. Any suggestion.
import org.openntf.domino.*;
import org.json.JSONArray;
import org.json.JSONObject;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.openntf.domino.ext.Session.Fixes;
import org.openntf.domino.utils.Factory;
public class JavaAgent extends AgentBase
{
public void NotesMain()
{
try
{
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
ERROR LINE:
Unexpected error while executing java agent:JavaAgent
-->java.lang.UnsupportedClassVersionError: JVMCFRE003 bad major version; class=, offset=6
java.lang.UnsupportedClassVersionError: JVMCFRE003 bad major version; class=, offset=6
at java.lang.ClassLoader.defineClass(ClassLoader.java:287)
at java.lang.ClassLoader.defineClass(ClassLoader.java:224)
at lotus.domino.AgentLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(ClassLoader.java:638)
at java.lang.ClassLoader.defineClassImpl(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:287)
at java.lang.ClassLoader.defineClass(ClassLoader.java:224)
at lotus.domino.AgentLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(ClassLoader.java:638)
at lotus.domino.AgentLoader.runAgent(Unknown Source)
I have added this .jar file in to archive that

How to use selenium-server-standalone-2.0rc2 while creating script with RC

package com.html;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import junit.framework.TestCase;
public class Html5 extends TestCase{`enter code here`
Selenium selenium1;
public void setUp()
{
selenium1=new DefaultSelenium("localhost",4444,"*firefox","http://live.com");
selenium1.start();
}
}
Error appearing in com.thoughtworks.selenium.DefaultSelenium; and DefaultSelenium("localhost",4444,"*firefox","http://live.com"); line.
Please suggest.
First :
What the enter code here string does there ?
Secondly :
If there is an error in the import com.thoughtworks.selenium.DefaultSelenium; and in the new DefaultSelenium, it's certainly that the jars are not in your classpath
selenium-server-standalone contains the Selenium server classes, but not the client ones, where DefaultSelenium is. You'll have to bring the client jars in your classpath, that is selenium2-java for this version I think
I think you need to give Path to firefox.exe in your Constructor..So
selenium1 = new DefaultSelenium("localhost",4444,"*firefox","http://live.com"); Goes like
selenium1 = new DefaultSelenium("localhost",4444,"*firefox C:\Documents and Settings\Mozilla Firefox\firefox.exe","http://live.com");
Try this once.

Eclipse Juno EE NoClassDefFoundError when using external Jar

I added an external jar in my eclipse dynamic webproject via Folder -> properties -> build path -> Libraries -> add external jar.
The code is working fine on compile time.
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tika.Tika;
#WebServlet(name="UploadServlet", urlPatterns={"/uploadFile"}) // specify urlPattern for servlet
#MultipartConfig //Specify that this servlet will receive a multipart data
public class UploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
Tika tika = new Tika();
res.setContentType("text/html");
PrintWriter writer = res.getWriter();
writer.write(mediaType);
}
}
I added Apache Tika but when I run my application. these exception occured.
root cause
java.lang.NoClassDefFoundError: org/apache/tika/Tika
servlet.UploadServlet.doPost(UploadServlet.java:19)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.ClassNotFoundException: org.apache.tika.Tika
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
servlet.UploadServlet.doPost(UploadServlet.java:19)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
I am using Apache Tika to determine what file has been upload, I want to use it for validating if a file is an image, or audio file
You forgot to add that jar to the Deployment Assembly page. It's not deployed to the server when you run your application, hence the NoClassDefFoundErrors.