thuliobuarque / java2word

Automatically exported from code.google.com/p/java2word
0 stars 0 forks source link

java.lang.NoClassDefFoundError: word/api/interfaces/IDocument #71

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What web framework and version are you using (Struts1, Struts2, Spring,
JBoss Seam)?

I don't use any specific frame work. I am doing Dynamic Web Project on Eclipse 
IDE.
(I am new to web development.)

Did you download j2w-ejb-2.0.zip or built from the source code?
I download 'java2word-3.0_2011Aug02.jar', but I don't download 
'j2w-ejb-2.0_2011Apr14.jar'. Do I need both?

Which web server did you deploy your .WAR (Tomcat, JBoss)?
Tomcat v7.0

Are you using Maven or Ant?
I am using Ant.

What is your Operational System?
Windows Vista Home Premium.

What steps will reproduce the problem?
1. On my webpage I have a 'submit' button to activate the servlet which use 
java2word api to generate Word document for download. When I click this button 
I receive servlet exception instead of download document dialog box.

Please provide any additional information below.

I found similar issue posted by 'rajeev.i...@gmail.com' as the 'Issue 17'. He 
solved the problem and said that this is 'the error with the clash of two 
path'. In my case, I put the jar file in my project and I also set the jar file 
in my computer's 'class path'. Should I remove the one I set in 'class path'?

Here's is my Servlet code:

package com.p6qgen;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import word.api.interfaces.IDocument;
import word.w2004.*;
import word.w2004.elements.*;
//import word.w2004.style.*;
import word.w2004.style.HeadingStyle.Align;

/**
 * Servlet implementation class WordWithoutSol
 */
@WebServlet("/WordWithoutSol")
public class WordWithoutSol extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public WordWithoutSol() {
        super();
        System.out.println("WordWithoutSol constructor...");
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();

        DBAccess dbAccess;
        Connection conn = null;

        try {
            dbAccess = new DBAccess();
            conn = dbAccess.getConnection(); 

            try {
                response.setContentType("application/msword; charset=UTF-8");
                PrintWriter writer = response.getWriter();

                // Create Word Document
                IDocument myDoc = new Document2004();

                myDoc.addEle(Heading1.with("Primary 6 Mathematics").withStyle().bold()
                        .align(Align.CENTER).create());
                myDoc.addEle(BreakLine.times(2).create());
                myDoc.addEle(Paragraph.withPieces(ParagraphPiece.with("Instrutor:" +
                        "________________________   Name:_________________________")
                        .withStyle().bold().fontSize("11").create()));
                myDoc.addEle(BreakLine.times(2).create());
                myDoc.addEle(Paragraph.withPieces(ParagraphPiece.with("Topic: Percentage")
                        .withStyle().bold().fontSize("14").underline().create()));
                myDoc.addEle(BreakLine.times(2).create());

                // Counting total number of questions in T_Question table
                int row = dbAccess.countRow(conn, "Q_ID", "T_Question");
                for(int i=0; i<row; i++) {
                    int q_id = i+1;
                    String q_val = dbAccess.selectValue(conn, "Q_Val", "T_Question", "Q_ID", q_id);

                    myDoc.addEle(Paragraph.withPieces(ParagraphPiece.with(q_id + "." + q_val)
                            .withStyle().fontSize("14").create().create()));
                    myDoc.addEle(BreakLine.times(2).create());
                }

                String myWord = myDoc.getContent();
                writer.println(myWord);
            } finally {
                out.close();
            }
        } catch(SQLException e) {
            System.err.println("-----SQLException-----");
            System.err.println("WordWithoutSol's doGet Error: " + e.getSQLState());
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doGet(request, response);
    }

}

And below is part of the exception and root cause:

javax.servlet.ServletException: Error instantiating servlet class 
com.p6qgen.WordWithoutSol
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:619)
root cause

java.lang.NoClassDefFoundError: word/api/interfaces/IDocument
    java.lang.Class.getDeclaredConstructors0(Native Method)
    java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
    java.lang.Class.getConstructor0(Class.java:2699)
    java.lang.Class.newInstance0(Class.java:326)
    java.lang.Class.newInstance(Class.java:308)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:619)

Original issue reported on code.google.com by nyein...@gmail.com on 6 Nov 2011 at 6:25

GoogleCodeExporter commented 8 years ago
I guess the best way is put the java2word(or any other necessary jar file) jar 
file in the directory /yourApp/WEB-INF/lib.

The jar should end up in this directory inside your war file. Unpack your war 
file and take a look at it.

Leonardo 

Original comment by leonardo...@gmail.com on 6 Nov 2011 at 8:41

GoogleCodeExporter commented 8 years ago
Thanks for the advice.

I've already put the jar file in /WEB-INF/lib directory along with other 
necessary jar files.
I've also import 'java2word' jar file into /Libraries directory. (Build Path -> 
Configure Build Path -> Libraries -> Add Jars). If I don't import this jar into 
this directory, I got error in 'WordWithoutSol' servlet in Eclipse.

I attached a screen capture of Eclipse's Project Explorer for your reference.
You'll notice a yellow warning sign on the Project name (P6QGen_Ajax). This 
sign appear when I import 'java2word' jar into Libraries .

Is there any thing I should do to solve the problem?

Original comment by nyein...@gmail.com on 6 Nov 2011 at 10:02

Attachments:

GoogleCodeExporter commented 8 years ago
I forgot to mention that java2word-3.0_2011Aug02.jar is included in war file 
when I unpack it.

Original comment by nyein...@gmail.com on 6 Nov 2011 at 10:43

GoogleCodeExporter commented 8 years ago
What is the yellow exclamation mark saying? go to "windows/show view/problem" 
and post here what this warning is saying.

Original comment by leonardo...@gmail.com on 6 Nov 2011 at 12:05

GoogleCodeExporter commented 8 years ago
Here's the message about the yellow exclamation mark:
"Classpath entry 
/P6QGen_Ajax/WebContent/WEB-INF/lib/dist/java2word-3.0_2011Aug02.jar will not 
be exported or published. Runtime ClassNotFoundExceptions may result."

Btw, as I mentioned in my previous message, I can see this jar in "war" file 
when I unpacked it.

Thanks,
Nyein Zay Aung  

Original comment by nyein...@gmail.com on 6 Nov 2011 at 12:45

GoogleCodeExporter commented 8 years ago
After adding java2word jar file into 'Properties -> Deployment Assembly' the 
yellow exclamation mark disappear. But when run the web app, receive the same 
exception.

Thanks,
nza

Original comment by nyein...@gmail.com on 6 Nov 2011 at 4:30

GoogleCodeExporter commented 8 years ago
I can see this directory "dist" inside lib. I never do that. Put your jar files 
straight on lib.

I don't know how you deploy, if you deploy to tomcat or run inplace. It is 
difficult to help because I don't know your environment. Classpath issues are 
beyond this library and I've never had this kind of issues. 

Original comment by leonardo...@gmail.com on 6 Nov 2011 at 10:25

GoogleCodeExporter commented 8 years ago
I deploy war to tomcat. I've set all the class path through 'build.properties' 
and 'build.xml' and so tomcat can reach to all necessary jars. Other parts of 
my web app is working except this one.

Thanks again for taking time to look at my issue.

Cheers!
nza

Original comment by nyein...@gmail.com on 7 Nov 2011 at 3:32

GoogleCodeExporter commented 8 years ago
The problem is solved. I manually copy and paste the 'java2word' jar into 
Tomcat's library and now word download is working. This might be something to 
do with my configuration. I remember I need to do the same for 
'mysql-connector' jar file sometimes ago.

Thanks again for your kind attention to my issue and a great 'Java2Word' API.

Cheers!
nza

Original comment by nyein...@gmail.com on 7 Nov 2011 at 5:20

GoogleCodeExporter commented 8 years ago
cool you found out! there are so many different ways to configure jars, I use 
maven and they all go to the proper place inside my app.  

but it is good you resolved! 

cheers
Leonardo 

Original comment by leonardo...@gmail.com on 7 Nov 2011 at 5:23

GoogleCodeExporter commented 8 years ago

Original comment by leonardo...@gmail.com on 2 Feb 2012 at 12:29