views:

119

answers:

3
package myfirst;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.lang.Iterable;
import java.net.URL;
import java.net.URLConnection;
import java.sql.*;

public class ReadExcel {
public static String fileToBeRead = "C:/Documents and Settings/Developer/Desktop/Anand exmps/Anand.xls";
public static void main(String argv[]) {
String urlcnt=" ";
Connection con=null;
Statement stmt=null;
int i=0;
int j=0;

try {
    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
    HSSFSheet sheet = workbook.getSheetAt(0);
    //HSSFRow row = sheet.getRow(0);
    //HSSFCell cell = row.getCell((short) 0);
    for (Row row : sheet) {
    //for (Cell cell : row) {
        Cell firstCell = row.getCell(0);
        urlcnt=firstCell.getRichStringCellValue().getString();
        System.out.println(urlcnt);
        if(con==null){
                SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK");
                con=SQLConnection.getNewConnection();
                stmt=con.createStatement();
        }
        try{
            ResultSet rs;
            boolean hasRows=false;
            rs=stmt.executeQuery("select url from urls_linkins where url='"+urlcnt+"'");
            while(rs.next()){
                hasRows=true;
                i++;
                //String mem=rs.getString(1);
                rs.close();
                //return "This URL already exists in DB";
            }
            if(!hasRows){
                j++;
                PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls_linkins(url, source_name, is_active, is_periodic, Link_Type, New_Entry) VALUES(?, ?, ?, ?, ?, ?)");
                //insertUrlStatement.setInt(1, 21211);
                insertUrlStatement.setString(1, urlcnt);
                insertUrlStatement.setInt(2, 1);
                insertUrlStatement.setInt(3, 1);
                insertUrlStatement.setInt(4, 0);
                insertUrlStatement.setInt(5, 1);
                insertUrlStatement.setInt(6, 1);
                insertUrlStatement.executeUpdate();
                insertUrlStatement.close();
            }
            }
            catch(Exception e){
                e.printStackTrace();
            }
            }
}catch(Exception e){
    e.printStackTrace();
}finally{
    System.out.println(""+j+" url has been added and "+i+" url already exists in the DB");
}
}
}

I have compiled the above program succesfully. But while executing the same, I am getting the following error.

Exception in thread "main" java.lang.NoClassDefFoundError: ReadExcel (wrong name
: myfirst/ReadExcel)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(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)
Could not find the main class: ReadExcel.  Program will exit.

How this can be resolved?

Went one up and executed the program as myfirst.ReadExcel as requested, but found this error now.

    Exception in thread "main"java.lang.NoClassDefFoundError:org/apache/poi/hssf/usermodel/HSSFWorkbook
    at myfirst.ReadExcel.main(ReadExcel.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.hssf.usermodel.HSSFWorkbook
    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)
    ... 1 more
A: 

Try java -cp yourotherlib1.jar;yourotherlib2.jar myfirst.ReadExcel instead of java myfirst/ReadExcel.

Don't forget to include your POI libraries and its dependencies on the classpath.

EDIT: changed classpath delimiter from : to ; for Windows.

whaley
On doing so, I get this error as above..
LGAP
I would recommend updating your original question with what you tried and it's output. It would be more visible, and also more readable.
zigdon
+2  A: 

You should not be running this from inside the myfirst directory. Go UP one level and then run it:

C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\CheckURL\WEB-INF\classes> java -cp .;..\lib\poi-3.6-20091214.jar myfirst.ReadExcel

dogbane
I have updated my question. Could you pls help out?
LGAP
Noted should be that in case of a webapp the 3rd party JAR files normally go in `WEB-INF/lib` folder, so the `-cp` should rather read `.;../lib/poi-3.6-20091214.jar`.
BalusC
there is no folder called lib inside WEB-INF.
LGAP
please move the jar file out of the myfirst package and place it in WEB-INF/lib. This is the correct way to structure your project.
dogbane
yeah done as requested
LGAP
@Anand: Create one. This is as per the Servlet API specification. You need to drop JAR's in there in order to get them loaded automatically whenever you run the webapplication. However, in this particular case you're fiddling in commandline rather than running it by a webapp. I would almost say, stop putting your test/playground classes in Tomcat's webapps folder as long as they are in no way related to the webapp. It would only add more confusion and red herrings. Create a folder like `C:/Java/playground` and put your "plain vanilla" test classes in there.
BalusC
Yeah Balus. I would follow this going forward. But the reason I have pasted these in Tomcat folder is, going forward I will design a JSP program that passes the location of a file to above Java program.
LGAP
@BalusC and why is that we need to be in the parent folder for executing this program? Earlier, I did a program which also had a support of library, but it executed successfully in the exact folder itself. But why in this case alone the differentiation?
LGAP
and now for compiling the same program, what must be given as the command? please advise
LGAP
@anand I have edited the classpath in the command above to point to the poi jar in the lib directory.
dogbane
Thanks fahdshariff. I have come up with the result. Thanks a lot for your support. I would like to know the way of compiling the program now after certain modification made.
LGAP
"javac -cp .;../lib/poi-3.6-20091214.jar myfirst.ReadExcel.java" This is not working :(
LGAP
which directory is ReadExcel.java in?
dogbane
myfirst directory
LGAP
"javac -cp .;..\lib\poi-3.6-20091214.jar myfirst\ReadExcel.java"
dogbane
LGAP
maybe your previous program was not in a package? Since this class is within the myfirst package, you should be out of the myfirst directory when you run it.
dogbane
yeah.. thanks and now am pretty clear :-)
LGAP
A: 

You must be in the parent folder of the "myFirst" folder and run

java -cp **dependencies here** myfirst.ReadExcel
Colin Hebert
the -cp flag should come before the name of the class.
dogbane
@fahdshariff Yes, it does...
Colin Hebert