HSSFWorkbook Error - java

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.

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)

exception connection reset when exiting the program

I have a multithreaded server with a connection to MySQL and every time a I run it I get the same exception:java.net.SocketException: Connection reset
this is my server:
public void run(){
// synchronized(this){
try {
serverSocket = new ServerSocket(serverPort);
System.out.println("Server started on port " + serverPort);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
while(true){
Socket s = serverSocket.accept();
es.execute((Runnable) new WorkerThread(s));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
this is my worker thread:
String[] str = new String[10];
String arr;
try {
int i=0;
int b=0;
String message=null;
while(!Thread.currentThread().isInterrupted()) {
message=in.readLine();
if (message.equals("exit")){
System.exit(0);
}
// .... here I have other if statements
}
} catch (IOException | SQLException e1) {
e1.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
and the exception :
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Pack.WorkerThread.run(WorkerThread.java:62)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
it appears here:
message=in.readLine();
If you exit the program using System.exit(0); your connection will be terminated which leads to this exception.
If you do not want this exception to be thrown, make sure to close inputstream using in.close() prior to calling System.exit(0) or terminating the thread.

How to connect ORACLE DB [duplicate]

This question already has answers here:
connect to sql server using jdbc
(2 answers)
Closed 9 years ago.
Here I'm trying to connect oracle which is installed in local machine with DSN bam.
I'm getting java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver Exception. Anybody please help me to fix this issue.
public class JdbcConnectionExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager
.getConnection("jdbc:oracle:thin:#localhost:1521:bam"
,"system","tiger");
Statement stmt = con.createStatement();
System.out.println("Created DB Connection....");
ResultSet rs = stmt.executeQuery("select * from tt");
while(rs.next()){
System.out.println(rs.getString("ename"));
System.out.println(rs.getInt("age"));
}
rs.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
The Log is here
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
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 com.bam.sqlex.JdbcConnectionExample.main(JdbcConnectionExample.java:14)
May be u have not added jar of oracle driver download jar from here
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
Undoubtedly, you are lack of Oralce jar, improt it into your project
Class.forName("oracle.jdbc.driver.OracleDriver");
You have to check if this class is added to the classpath.

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.

Parsing Ofx file with 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();
}
}

Categories