Java错误的NoClassDefFoundError [英] Java Error NoClassDefFoundError

查看:100
本文介绍了Java错误的NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的NoClassDefFoundError一些真正的麻烦,我挣扎理解的原因,所以我可以修复它​​。我想我已经正确地设置了我的类路径,但也许我失去了一些东西。

I'm having some real trouble with a NoClassDefFoundError, and I'm struggling to understand the cause so I can fix it. I think I've set up my class paths correctly, but maybe I'm missing something.

下面是我的错误:

下面是我的code(我把它换成我的用户配置文件中的所有直接引用与[USER]):

Here is my code (I've replaced all direct references to my user profile with [USER]):

此外,这通常是由我创造,而不是命令路径的UI叫,但由于没有得到我修改了它从一个命令窗口中运行测试......因为我不能完全弄清楚的log4j但错误

Also, this is usually called from a UI I created and not command path, but since wasn't getting errors I modified it to run from a command window for testing... since I couldn't quite figure out log4j yet.

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;
import java.sql.SQLException;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.log4j.Logger;

public class FilePrepMainStart {
    //declare variables on the class level
    static String fullinpath, fulloutpath, inpath, outpath, infilename, outfilename, outorigfn, delimiter, logfn;
    static boolean istextfile, isexcelfile, addrecid;
    //static Logger log;

    //Create contructor with arguments
    public void FilePrepMainStart(String in, String out, boolean txt, boolean exl, boolean rec, String delim) {
        this.fullinpath = in;
        this.fulloutpath = out;
        this.istextfile = txt;
        this.isexcelfile = exl;
        this.addrecid = rec;
        this.delimiter = delim;
    }

    private static String getPath(String in) {
        //Attempts to extract the directory path from the full file path
        Pattern pathPattern = createPattern("^([a-z]\\:.*\\\\)([a-zA-Z0-9 \\_\\$\\!\\?\\+\\%\\#\\@\\)\\(\\-\\.]+\\.[a-zA-Z0-9]{3,4})$");
        Matcher filePath = pathPattern.matcher(in);
        if (filePath.find()){
            return filePath.group(1);
        }
        else {
            return null;
        }
    }

    private static String getFileName(String in) {
        //Attempts to extract the sile name from the full file path
        Pattern namePattern = createPattern("^([a-z]\\:.*\\\\)([a-zA-Z0-9 \\_\\$\\!\\?\\+\\%\\#\\@\\)\\(\\-\\.]+\\.[a-zA-Z0-9]{3,4})$");
        Matcher filename = namePattern.matcher(in);
        if (filename.find()){
            return filename.group(2);
        }
        else {
            return null;
        }
    }

    private static Pattern createPattern(String pattern){
        //Creates a compiled Pattern object from a String pattern
        //args- pattern: String containing regex pattern to be compiled
        Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
        return p;
    }

    public static String appendOut(String name, String append) {
        //Adds the string "append" to the file name on output.
        //First checks for a file extension
        //Used to create a "_Clean" and "_Original" file
        Pattern filePattern = createPattern("(.*)(\\.[a-z0-9]{3,4})$");
        Matcher fileMatcher = filePattern.matcher(name);
        String outfilename = name;

        if (fileMatcher.find()) {
            outfilename = fileMatcher.group(1) + append + fileMatcher.group(2);
        }
        else {
            outfilename = outfilename + append;
        }
        return outfilename;
    }

    public static String appendOut(String name, String append, boolean excludeext) {
        //Overloaded appendOut to allow prevention of adding extension back into the fn
        //Adds the string "append" to the file name on output.
        //First checks for a file extension
        //Used to create a "_Clean" and "_Original" file
        //boolean excludeext: True to exclude adding the file extension back into the file name
        //False for default (non-overloaded) functionality
        Pattern filePattern = createPattern("(.*)(\\.[a-z0-9]{3,4})$");
        Matcher fileMatcher = filePattern.matcher(name);
        String outfilename = name;

        if (fileMatcher.find() && excludeext) {
            outfilename = fileMatcher.group(1) + append;
        }
        else if (fileMatcher.find()) {
            outfilename = fileMatcher.group(1) + append + fileMatcher.group(2);
        }
        else {
            outfilename = outfilename + append;
        }
        return outfilename;
    }

    private static int chkEXLFile(String name) {
        //Checks Excel file type
        String pattern = "(.*)\\.([a-z0-9]{3,4})$";
        String filename = name;
        Pattern p = createPattern(pattern);
        Matcher chkFile = p.matcher(filename);

        if (chkFile.find()){
            String file_ext = chkFile.group(2).toLowerCase();
            if (file_ext.equals("xls")) {
                //XLS file
                return 0;
            }
            else if (file_ext.equals("xlsx")) {
                //XLSX File
                return 1;
            }
            else {
                //Unrecognized - Not XLS or XLSX file
                return -1;
            }
        }
        else {
            //Unrecognized - Not XLS or XLSX file
            return -1;
        }
    }

    public static void openXLSFileStream(String input, String output, boolean rec, String worksheet) throws FileNotFoundException, IOException {
        //Opens input and output streams and kicks off processing
        FileInputStream in = new FileInputStream(input);
        HSSFWorkbook inbk = new HSSFWorkbook(in);
        FileOutputStream out = new FileOutputStream(output);
        HSSFWorkbook outbk = new HSSFWorkbook();

        if (rec) {
            processXLSFileWithRecID(in, inbk, out, outbk, worksheet);
        }
        else {

        }
    }

    private static void processXLSFileWithRecID(FileInputStream in, HSSFWorkbook inbk, FileOutputStream out, HSSFWorkbook outbk, String worksheet) throws FileNotFoundException, IOException {
        /*  Reads in and loops through each excel cell to clean it
        *       Need to add functionality to create a new workook, could be added for each part of the loop
        */
        Pattern pattern = createPattern("[^-a-z_0-9\"/\\t\\.#@\\|, ]");
        int sheetnum = -1;
        int sheets = inbk.getNumberOfSheets();

        //Get the worksheet number for the worksheet requested
        for (int i = 0; i < sheets; i++) {
            if (worksheet.equals(inbk.getSheetName(i))) {
                sheetnum = i;
                break;
            }
        }

        //To Do: If no worksheet is present, sheetnum will be -1, and an error should be raised
        if (sheetnum == -1) {}

        HSSFSheet sheet = inbk.getSheetAt(sheetnum);

        int rows = sheet.getPhysicalNumberOfRows();

        HSSFSheet outsheet = outbk.createSheet();

        //Loop through each row of the sheet
        for (int r = 0; r < rows; r++) {
            HSSFRow row = sheet.getRow(r);

            if (row == null) {
                continue;
            }

            HSSFRow outrow = outsheet.createRow(r);

            int cells = row.getPhysicalNumberOfCells();

            //Loop through each cell of the sheet
            for (int c = 0; c < cells; c++) {
                HSSFCell cell = row.getCell(c);

                String s = null;

                HSSFCell outcell = outrow.createCell(c);

                //Grab the cell value and assign it to variable s, based on the cell type
                switch(cell.getCellType()) {
                    case HSSFCell.CELL_TYPE_FORMULA:
                        s = cell.getCellFormula();
                        break;
                    case HSSFCell.CELL_TYPE_NUMERIC:
                        s = Double.toString(cell.getNumericCellValue());
                        break;
                    case HSSFCell.CELL_TYPE_STRING:
                        s = cell.getStringCellValue();
                        break;
                    default:
                }

                Matcher matcher = pattern.matcher(s);
                //If a match is found in the string:
                if (matcher.find()) {
                    //Replace all matches
                    String repl = matcher.replaceAll(" ");
                    outcell.setCellValue(repl);
                }
                else {
                    outcell.setCellValue(s);
                }
            }
        }

        outbk.write(out);
        out.close();
    }

    public static void run(String in, String out, boolean txt, boolean exl, boolean rec, String delim, String worksheet) {
        //Primary run process
        //Takes String args in, out, delim, and worksheet
        //in, out are file names for input and output files; delim is delimiter for adding recid on txt files
        //worksheet is the name of the worksheet that needs to be processed on Excel type files
        //Takes boolean args txt, exl, and rec
        //txt and exl indicate file type, diff file types have diff processing
        //rec indicates is a record id is requested to be added to the file

        try {
            delimiter = delim;

            outfilename = appendOut(out, "_Clean");
            outorigfn = appendOut(out, "_Original");
            //For when I add in logging
            logfn = appendOut(in, "_LogFile.txt", true);

            if (txt) {
                openTXTStreams(in, outfilename, outorigfn, delimiter, rec);
            }
            else if (exl) {
                int inflg = chkEXLFile(in);
                int outflg = chkEXLFile(out);

                if (inflg == 0) {
                    //throw new Exception("test");
                    openXLSFileStream(in, out, rec, worksheet);
                }
                else if (inflg == 1) {
                }
                else {
                }
            }
            else {
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String in = "D:\\Users\\[USER]\\Documents\\Java\\FilePrep\\TestFiles\\Name Address Phone.xls";
        String out = "D:\\Users\\[USER]\\Documents\\Java\\FilePrep\\TestFiles\\Name Address Phone_Out.xls";
        boolean txt = false;
        boolean exl = true;
        boolean rec = false;
        String delim = ",";
        String worksheet = "Sheet1";
        run(in, out, txt, exl, rec, delim, worksheet);
    }

}

下面是我使用编译命令:

Here is the command I use to compile:

"C:\Program Files\Java\jdk1.7.0\bin\javac.exe" -d D:\Users\[USER]\Documents\Java\FilePrep\fileprep -classpath D:\Users\[USER]\Documents\Java\FilePrep\fileprep;D:\Users\[USER]\Documents\Java\FilePrep\fileprep\lib\log4j-1.2.13.jar;D:\Users\[USER]\Documents\Java\Libraries\Apache\poi-3.9\poi-ooxml-3.9-20121203.jar;D:\Users\[USER]\Documents\Java\Libraries\Apache\poi-3.9\poi-3.9-20121203.jar D:\Users\[USER]\Documents\Java\fileprep\sources\fileprep\FilePrepMainStart.java

最后,这里是我目前正在使用,测试从app目录运行命令:

And lastly, here is the command I am currently using to test run from the app directory:

java FilePrepMainStart -classpath D:\Users\[USER]\Documents\Java\FilePrep\fileprep;lib\log4j-1.2.13.jar;lib\poi-ooxml-3.9-20121203.jar;lib\poi-3.9-20121203.jar;lib\commons-codec-1.5.jar;lib\stax-api-1.0.1.jar;lib\commons-logging-1.1.jar;lib\xmlbeans-2.3.0.jar;lib\dom4j-1.6.1.jar

这是第一个真正的Java程序我曾经尝试。目的是一个简单的应用程序,以从数据文件清洁特殊字符。我已经能够建立简单的文本文件清理正确,但是,我不能让程序与Apache POI工作的Excel文件。

This is the first real Java program I've attempted to make. The purpose is a simple app to clean special characters from a data file. I've been able to set up the simple text file cleaning correctly, however, I cannot get the program to work with Apache POI for Excel files.

我通过UltraEdit的工作,因为我没有安装权限使用像Eclipse或NetBeans的一个IDE,而在工作。一个IDE也许真的有所帮助,但是,因为我太新了这一点。我现在有了这个挣扎了一会儿,那就是拦着我从Java中完全进步路障。我研究了错误,并试图改变周围我的课的路径。我没有权限改变我的PATH环境变量,但我不知道这是否会导致此与否。我也试着移动我的JAR文件到不同的位置。

I am working through UltraEdit, as I don't have install permissions to use an IDE like Eclipse or NetBeans while at work. An IDE might really help, though, since I am so new to this. I've struggled with this for a while now and it's a road block that's stopping me from progressing in Java entirely. I've researched the error and tried changing my class paths around. I don't have permissions to change my PATH environment variable, but I don't know if that would cause this or not. I've also tried moving my JAR files to a different location.

谁能帮我找出什么我失踪或点我在正确的方向?我失去了在这一个我的脑海里。我觉得我应该能想出解决办法,如果我是一个程序员值得他体重咸味,但显然我还没有。我AP preciate任何建议。

Can anyone help me identify what I'm missing or point me in the right direction? I'm losing my mind on this one. I feel like I should be able to figure this out if I were a programmer worth his weight in salt- but clearly I'm not there yet. I appreciate any suggestions.

谢谢!

推荐答案

cd到你的项目目录,并尝试这个办法:

cd to your project directory and try this:

Java的-classpath .;lib\\log4j-1.2.13.jar;lib\\poi-ooxml-3.9-20121203.jar;lib\\poi-3.9-20121203.jar;lib\\commons-$c$cc-1.5.jar;lib\\stax-api-1.0.1.jar;lib\\commons-logging-1.1.jar;lib\\xmlbeans-2.3.0.jar;lib\\dom4j-1.6.1.jar文件prepMainStart

java -classpath .;lib\log4j-1.2.13.jar;lib\poi-ooxml-3.9-20121203.jar;lib\poi-3.9-20121203.jar;lib\commons-codec-1.5.jar;lib\stax-api-1.0.1.jar;lib\commons-logging-1.1.jar;lib\xmlbeans-2.3.0.jar;lib\dom4j-1.6.1.jar FilePrepMainStart

这篇关于Java错误的NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆