When I am importing the HTML File according to the tutorialpoint link https://www.tutorialspoint.com/jsoup/jsoup_load_file.htm
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class jsoupTester {
public static void main(String[] args) throws IOException, URISyntaxException {
URL path = ClassLoader.getSystemResource("test.htm");
File input = new File(path.toURI());
Document document = Jsoup.parse(input, "UTF-8", "");
System.out.println(document.title());
}
}
I got this error when I run the program:
Exception in thread "main" java.lang.NullPointerException
at jsoupTester.main(jsoupTester.java:13)
Note: jsoupTester.java file and temp.htm are in the same location
May I know how to solve this issue? Your suggestions will be highly appreciated :)
Have you checked the website properly? the code documentation showed this
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class JsoupTester {
public static void main(String[] args) throws IOException, URISyntaxException {
URL path = ClassLoader.getSystemResource("test.htm");
File input = new File(path.toURI());
Document document = Jsoup.parse(input, "UTF-8"); // Only 2 parameters
System.out.println(document.title());
}
}
Error
Document document = Jsoup.parse(input, "UTF-8", ""); // 3rd parameter is not included in the documentation
As you can see the error is that you have another redundant parameter which I believe is causing the error. Remove that "" in your code and it will work fine. Hope that answers your question :)
Related
I am developing a project which needs a docx file to be converted to pdf. I found same question already posted and used the code which was provided by "Kishan C S". It uses docx4J2.8.1
The code is working fine , pdf is generated but only problem I am facing is that the docx file contains logo.jpg (images header part) which are not converted. Only textual format is converted to pdf.
I am posting the code which I have used. Please let me know what how can I solve the problem
P.S: link I referred Convert docx file into PDF with Java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.List;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFont;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class DocxConverter {
public static void main(String[] args) throws FileNotFoundException, Docx4JException, Exception {
InputStream is = new FileInputStream(new File("D:\\Test\\C_IN0004_AppointmentLetter.docx"));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
List sections = wordMLPackage.getDocumentModel().getSections();
for (int i = 0; i < sections.size(); i++) {
wordMLPackage.getDocumentModel().getSections().get(i).getPageDimensions();
}
Mapper fontMapper = new IdentityPlusMapper();
PhysicalFont font = PhysicalFonts.getPhysicalFonts().get("Comic Sans MS");//set your desired font
fontMapper.getFontMappings().put("Algerian", font);
wordMLPackage.setFontMapper(fontMapper);
PdfSettings pdfSettings = new PdfSettings();
org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
//To turn off logger
List<Logger> loggers = Collections.<Logger> list(LogManager.getCurrentLoggers());
loggers.add(LogManager.getRootLogger());
for (Logger logger : loggers) {
logger.setLevel(Level.OFF);
}
OutputStream out = new FileOutputStream(new File("D:\\Test\\C_IN0004_AppointmentLetter.pdf"));
conversion.output(out, pdfSettings);
System.out.println("DONE!!");
}
}
I get the following error when I try to run
java -cp 'angus-sdk-java-0.0.2-jar-with-dependencies.jar:.' FaceDetect
I am following a tutorial for face detection in http://angus-doc.readthedocs.io/en/latest/getting-started/java.html . Below is my java code,
import java.io.IOException;
import org.json.simple.JSONObject;
import ai.angus.sdk.Configuration;
import ai.angus.sdk.Job;
import ai.angus.sdk.ProcessException;
import ai.angus.sdk.Root;
import ai.angus.sdk.Service;
import ai.angus.sdk.impl.ConfigurationImpl;
import ai.angus.sdk.impl.File;
public class FaceDetect {
public static void main(String[] args) throws IOException, ProcessException {
Configuration conf = new ConfigurationImpl();
Root root = conf.connect();
Service service = root.getServices().getService("age_and_gender_estimation", 1);
JSONObject params = new JSONObject();
params.put("image", new File("Downloads/IMG_1060.jpg"));
Job job = service.process(params);
System.out.println(job.getResult().toJSONString());
}
}
I don't understand the problem with it. I have tried all the answers in the stack overflow but nothing is working for me.
remove the single qoutes around the classpath:
java -cp angus-sdk-java-0.0.2-jar-with-dependencies.jar:. FaceDetect
Our code is reading tagnames in the xml file instead of values inside.
We tried to change the values in the tags but it does not help.
The o/p is the name of the tag instead of the value
java file:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
import javax.xml.bind.Unmarshaller;
public class Main {
public static void main(String[] args) throws JAXBException, FileNotFoundException{
LoginRequest url=null;
JAXBContext jbc=null;
Unmarshaller um=null;
FileInputStream fis=null;
jbc= JAXBContext.newInstance(LoginRequest.class);
um = jbc.createUnmarshaller();
fis = new FileInputStream("maintenance_technicalinformationmsgs_v1.0.xml");
url = (LoginRequest)um.unmarshal(fis);
//url.setUserId("MARMIK");
System.out.println(url);
System.out.println(url.getUserId());
System.out.println(url.getLanguage());
System.out.println(url.getPassWord());
System.out.println(url.getMarket());
}
xml file:
<?xml version="1.0" encoding="UTF-8"?>
<tns:LoginRequest xmlns:tns=**Namespace name
xmlns:tns1=**
xmlns:xsi=**
xsi:schemaLocation=**Schema location
>
<tns:userId>tns:ud</tns:userId>
<tns:passWord>tns:passord</tns:passWord>
<tns:language>tns:language</tns:language>
<tns:market>tns:market</tns:market>
</tns:LoginRequest>
Thanks!
How to read data from solr/data/index by some simple console Java application? I found some solution.
But maybe there is more simple way.
Help please with that, I really don't know what to do.
It's my own solution. I get index files from solr 4.4 and I also use lucene-core-4.4.0.jar library. Maybe it can help someone.
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.solr.client.solrj.SolrServerException;
public class SomeClass {
public static void main(String[] args) throws IOException {
Directory dirIndex = FSDirectory.open(new File("solr/home/data/index"));
IndexReader indexReader = IndexReader.open(dirIndex);
Document doc = null;
for(int i = 0; i < indexReader.numDocs(); i++) {
doc = indexReader.document(i);
}
System.out.println(doc.toString());
indexReader.close();
dirIndex.close();
}
}
There is a project called Luke which is a GUI for users to inspect Lucene indices.
Here is a blog post with more detail.
Hi I am very new to jasper reports. Just have a requirement where i need to retrieve the info from a .jasper file. I am using the below code for that.
But issue is compileReport does not take up the file i have in my local machine and i get a error like
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:150)
at jasper.test(jasper.java:28)
at jasper.main(jasper.java:40)
I am not sure what is wrong.Is it not able to get my .jasper file? which class is missing?
I am absolutely clueless.
My code is:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import dori.jasper.engine.JRException;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
public class jasper {
public static void test() throws FileNotFoundException, net.sf.jasperreports.engine.JRException{
JasperReport report = JasperCompileManager.compileReport("C:\\Users\\Mandakini\\Downloads\\testReport.jasper");
JasperPrint print = JasperFillManager.fillReport(report, new HashMap<String, String>());
// export it!
//File pdf = File.createTempFile("output.", ".pdf");
OutputStream output= new FileOutputStream(new File("F:/catalog.pdf"));
JasperExportManager.exportReportToPdfStream(print,output);
}
public static void main(String args[]) throws FileNotFoundException, net.sf.jasperreports.engine.JRException{
test();
}
}
Thanks
A java.lang.NoClassDefFoundError or ClassNotFoundException should almost always tell you that the class it references is missing from your classpath. In this case, it's org.apache.commons.digester.Digester. You can download the jar containing this class here.
To produce a report, do something like:
JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile(yourJasperFilePath);
Map<String, Object> parameters = new HashMap<>();
// set your parameters
Class.forName("com.mysql.jdbc.Driver");
Connection con = ...; // possibly get a connection
byte[] report = JasperRunManager.runReportToPdf(jasperReport, parameters, con); // pass the report, the report parameters, and a connection
// this will fill the byte[] with the produced report
FileOutputStream out = new FileOutputStream(somePath);
out.write(report);
out.close();
As to what kind of report it generates, I'm not sure how you set that. That info might be in the .jasper or .jrxml file.