Parsing Ofx file with java - java

I am currently trying to read an ofx file with java.
But I get the following error: Unhandled exception type FileNotFoundException (for the 2nd line). I am using OFx4j. Could you please give me some tips on that one?
Here is the code I have written so far:
String filename=new String("C:\\file.ofx");
FileInputStream file = new FileInputStream(filename);
NanoXMLOFXReader nano = new NanoXMLOFXReader();
try
{
nano.parse(stream);
System.out.println("woooo It workssss!!!!");
}
catch (OFXParseException e)
{
}
Thanks for your comments, I made some changes:
String FILE_TO_READ = "C:\\file.ofx";
try
{
FileInputStream file = new FileInputStream(FILE_TO_READ);
NanoXMLOFXReader nano = new NanoXMLOFXReader();
nano.parse(file);
System.out.println("woooo It workssss!!!!");
}
catch (OFXParseException e)
{
System.out.println("Message : "+e.getMessage());
}
catch (Exception e1)
{
System.out.println("Other Message : "+e1.getMessage());
}
But now I am getting this:
Exception in thread "main" java.lang.NoClassDefFoundError: net/n3/nanoxml/XMLParseException
at OfxTest.afficherFichier(OfxTest.java:31)
at OfxTest.main(OfxTest.java:20)
Caused by: java.lang.ClassNotFoundException: net.n3.nanoxml.XMLParseException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 2 more
I am trying to figure it out. I believe it can't find the XMLParseException. But I am not sure.

The second problem that you're encountering: "Exception in thread "main" java.lang.NoClassDefFoundError: net/n3/nanoxml/XMLParseException" means that you haven't included the NanoXML library from here: http://devkix.com/nanoxml.php
You will also need the Apache Commons Logging library, as NanoXML appears to be dependent on this. Available here: http://commons.apache.org/logging/download_logging.cgi

This means that you are not catching FileNotFoundException. Also although this is not relevant to your error message but as best practice you should always close you file stream in the finally block like I have below. There is also no need to do to new String() on the file name either.
Add this catch block for the FileNotFoundException:-
String filename = "C:\\file.ofx";
FileInputStream file = null;
NanoXMLOFXReader nano = null;
try
{
file = new FileInputStream(filename);
nano = new NanoXMLOFXReader();
nano.parse(stream);
System.out.println("woooo It workssss!!!!");
}
catch (OFXParseException e)
{
e.printStackTrace();
throw e;
}catch (FileNotFoundException e){
e.printStackTrace();
throw e;
}finally{
if(file!=null){
file.close();
}
}

Related

How to compare two xml file and provide output as HTML using multipart/Form-data Restassured java

I need to compare the two xml file and need to get the output file as html , below is the source code I used . I am getting Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError
String Revises = "D:\\xml1.xml";
String pageproof = "D:\\xml2.xml";
File Revisesxml = new File(Revises);
File pageproofxml = new File(pageproof);
String Endpoint = "***";
if (Revisesxml.exists() && pageproofxml.exists()) {
System.out.println("both file exists");
Response response = given().auth().none().header("Content-Type", "multipart/form-data")
.multiPart("compare", new File(Revises), "text/xml")
.multiPart("compare2", new File(pageproof), "text/xml")
.accept(ContentType.XML)
.when().log().all().post(Endpoint);
System.out.println("staus code : " + response.getStatusCode());
byte[] bytes = response.getBody().asByteArray();
File file = new File("D:\outputhtml.html");
try {
Files.write(file.toPath(), bytes);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
System.out.println("both file not exists");
}
exception :
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.apache.http.entity.ContentType.withParameters([Lorg/apache/http/NameValuePair;)Lorg/apache/http/entity/ContentType;
at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:219)
at org.apache.http.entity.mime.MultipartEntityBuilder.build(MultipartEntityBuilder.java:240)
at org.apache.http.entity.mime.MultipartEntityBuilder$build$5.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130)
at io.restassured.internal.multipart.RestAssuredMultiPartEntity.getEntity(RestAssuredMultiPartEntity.groovy:58)
at io.restassured.internal.multipart.RestAssuredMultiPartEntity.getContentType(RestAssuredMultiPartEntity.groovy:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:107)

Unable to connect to FTP using FTP4J

I'm writing a program that has to connect to an FTP server in order to download certain files. In order to do this I'm using the FTP4J library, However I'm running into some trouble.
So far I have:
if ("Dataset FTP location".equals(link.text())) {
String FTPURL = link.attr("href");
FTPClient client = new FTPClient();
try {
client.connect(FTPURL);
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}
Where the URL of the FTP is ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/10/PXD002829
However If I run the program I get:
Exception in thread "main" java.net.UnknownHostException: ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/10/PXD002829
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at it.sauronsoftware.ftp4j.FTPConnector.tcpConnectForCommunicationChannel(FTPConnector.java:208)
at it.sauronsoftware.ftp4j.connectors.DirectConnector.connectForCommunicationChannel(DirectConnector.java:39)
at it.sauronsoftware.ftp4j.FTPClient.connect(FTPClient.java:1036)
at it.sauronsoftware.ftp4j.FTPClient.connect(FTPClient.java:1003)
at Main.main(Main.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Any help would be appreciated.
Also I don't have a log in for the server, it's just a public repository of files. Will this effect how I go about doing things?
You need to split off the path and create a url that looks like:
ftp.pride.ebi.ac.uk
In answer to your comment you need to do something like this:
String ftpPath = "ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2015/10/PXD002829";
URL url = new URL(ftpPath);
String host = url.getHost();
FTPClient client = new FTPClient();
try {
client.connect(host);
client.login("anonymous", "anonymous");
FTPFile[] list = client.list(url.getPath());
for (FTPFile f : list) {
// Instead of printing out the file download it. See
// http://www.sauronsoftware.it/projects/ftp4j/manual.php#14
System.out.println(f);
}
} catch (FTPIllegalReplyException e) {
e.printStackTrace();
} catch (FTPException e) {
e.printStackTrace();
}

Saving data in a .db file - java.io.File

I'm trying to save the data of a JAVA program in a .db file but isn't working.
The following message error appears:
"java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at server.server.main(server.java:27)
Running"
My program is a Client-Server model working with JAVA RMI. I have 2 clients, 2 interfaces and then multiple classes including a server class and an implementation class with all the methods.
The "save" method is in the implementation class as follows:
public void save() {
ObjectOutputStream out;
try {
out = new ObjectOutputStream(new FileOutputStream(new java.io.File("C:/Users/Myself/Desktop/Hospital.db")));
out.writeObject(this);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
In the server class:
try {
ObjectInputStream in;
try {
in = new ObjectInputStream(new FileInputStream(new java.io.File("C:/Users/Myself/Desktop/Hospital.db")));
read = (implementation)in.readObject();
in.close();
System.out.println(read);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
read = new implementation();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
read = new implementation();
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
read = new implementation();
e.printStackTrace();
}
The "save" method is then used in both clients.
The exception stack trace shows an enf of file while trying to read the object stream header. From this we can determine several things:
the problems occurs while reading an object from file C:/Users/Myself/Desktop/Hospital.db. It does not happen while writing an object as your problem description seems to imply
the trace ObjectInputStream.readStreamHeader() shows that the stream is trying to read its header, so it happens at the beginning of the file. The stream header is normally made of 2 short values AC ED and 00 05, the first 4 bytes in the stream
the fact that EOFException is thrown before the stream header can be read implies that the stream has less than 4 bytes in it. I'll even bet it's empty. Why is it empty? It's impossible to know without seeing more of your code
So, in short: your code is trying to deserialize an object from an empty file, causing the exception you are observing.

HSSFWorkbook Error

Hi I am having an error when I try to use the HSSF Workbook. See this error
Exception in thread "Thread-13" java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
at digicare.tracking.serial.BulkUpload.UploadProgress$1read2.run(UploadProgress.java:95)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.hssf.usermodel.HSSFWorkbook
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at com.sun.jnlp.JNLPClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Here's my code:
try {
file = new FileInputStream(new File(FilePath));
try {
workbook = new HSSFWorkbook(file);
} catch (Exception e2){
JOptionPane.showMessageDialog(null, "Error1" + e2.getMessage());
}
//HSSFSheet sheet = workbook.getSheetAt(0);
//HSSFRow row;
//HSSFCell cell;
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "Error1" + e1.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "Error2 "+ e.getMessage());
}
It seems like whenever I try to use the workbook part it returns an error
Do you have the POI jars in your build path.It says No class found.HSSF is associated with XLS files.
See here for an example in Eclipse.
Depending on which IDE you use, the setup process might vary.

Java SerialIzation: 'ClassNotFoundException' when deserializing an Object

The error:
java.lang.ClassNotFoundException: testprocedure.tp$3
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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 java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at core.ProcedureSetup.load(ProcedureSetup.java:57)
at core.Engine.main(Engine.java:25)
I instantiate the object and call the "ProcedureSetup"'s "save" method from Class "tp".
ProcedureSetup ps=new ProcedureSetup(new Procedure(){ public void doStuff(){ System.out.println("Stuff is being done"); }});
ps.save();
however I load from a different collection of programs that has -ALL- required code but "tp"
ProcedureSetup ps=new ProcedureSetup();
ps.load();
Object saving and loading within class:
public void load(){
String path=Operator.persistentGetFile();//gets the file path
ObjectInputStream ois=null;
FileInputStream fin=null;
ProcedureSetup temp=null;
try {
fin = new FileInputStream(path);
ois = new ObjectInputStream(fin);
temp=(ProcedureSetup) ois.readObject();
ois.close();
fin.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if(ois!=null){
try {
ois.close();
} catch (IOException e) {}
}
if(fin!=null){
try {
fin.close();
} catch (IOException e) {}
}
if(temp!=null){
a=temp.a;
}else{
load();//If a load is failed, restart process.
}
}
public void save(){
String path=Operator.persistentGetDirectory();//get directory to save to
String input = JOptionPane.showInputDialog(this, "Enter the File name:");
ObjectOutputStream oos=null;
FileOutputStream fon=null;
try {
fon = new FileOutputStream(path+input+".obj");
oos = new ObjectOutputStream(fon);
try {
oos.writeObject(this);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
oos.close();
fon.close();
} catch (IOException e) {
e.printStackTrace();
}
if(oos!=null){
try {
oos.close();
} catch (IOException e) {}
}
if(fon!=null){
try {
fon.close();
} catch (IOException e) {}
}
}
My questions are:
Why are these errors happening?
Why (if necessary) do I need to have "tp" in my classpath?
If there is in fact a way to save the object in its current state with out the necessity of "tp" in the classpath how would I go about doing that? (Links would be lovely)
When you read in a serialized object, Java "reconstitutes" it by using the information in the serialized stream to build a live copy of the object. It can't do this unless it has the .class file for the object's class; it needs a blank copy to "fill out" with the information from the stream.
The best option is usually to make sure that the class is on the class path. If you have some particular reason why this won't work, Java serialization isn't for you; JSON may be a suitable option instead.
new Procedure(){ public void doStuff(){ System.out.println("Stuff is being done"); }}
The above is an anonymous inner class of your tp class. So, to be deserialized, this anonymous inner class, and its enclosing class tp, must be present in the classpath: the stream of bytes contains the name of the class and the fields of the object, but it doesn't contain the byte-code of the class itself.
You should make it a top-level class, or at least a static inner class.
You should also respect the Java naming conventions: classes are CamelCased.
Why are these errors happening?
There is only one error here: java.lang.ClassNotFoundException: testprocedure.tp$3. It means you haven't deployed testprocedure/tp$3.class to the peer.
Why (if necessary) do I need to have "tp" in my classpath?
So that deserialization can succeed. You can't do anything with a class you don't have the .class file for, let alone deserialize instances of it.

Categories