NoClassDefFoundError:ReadExcel(名称错误

发布于 2024-09-18 14:35:44 字数 4184 浏览 4 评论 0原文

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");
}
}
}

我已经成功编译了上面的程序。但是在执行相同的操作时,我收到以下错误。

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.

如何解决这个问题?

上去并按照要求执行程序 myfirst.ReadExcel ,但现在发现了这个错误。

    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
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

始于初秋 2024-09-25 14:35:44

您不应该从 myfirst 目录中运行它。向上一级然后运行它:

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

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

躲猫猫 2024-09-25 14:35:44

尝试使用 java -cp yourotherlib1.jar;yourotherlib2.jar myfirst.ReadExcel 而不是 java myfirst/ReadExcel

不要忘记将您的 POI 库及其依赖项包含在类路径中。

编辑:将类路径分隔符从 : 更改为 ;对于 Windows。

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.

紫瑟鸿黎 2024-09-25 14:35:44

您必须位于“myFirst”文件夹的父文件夹中并运行

java -cp **dependencies here** myfirst.ReadExcel

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

java -cp **dependencies here** myfirst.ReadExcel
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文