【问题标题】:java.lang.NoClassDefFoundError Issue from Command Prompt命令提示符中的 java.lang.NoClassDefFoundError 问题
【发布时间】:2023-03-07 09:32:01
【问题描述】:

我正在尝试从命令提示符运行 java 文件。当我在 NetBeans IDE 中运行该程序时,它运行良好。不幸的是,我需要能够从命令提示符运行它。我有两节课。如果相关,我将包括所有代码。我查找了有关此问题的帖子,但没有看到任何似乎可以解决我的问题的内容。在解释事情时,请像与对此知之甚少的人交谈一样,因为事实就是如此。

package projectbeng;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
public class DataRecover {
    public static void main(String[] args) throws Exception {
        
        //Create a Scanner for the user
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter file name to process: ");
        File fileName = new File(sc.nextLine() + ".txt"); //Do not include the .txt extension
        
        if(!fileName.exists()){ //does not exist
            throw new IOException("File \"" + fileName + "\" not found.");
        }
        
        System.out.println("\nProcessing file: " + fileName + "\n----------------------------------------");
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        int lineCount = 0; //assumes file does not end with a new line character
        int tripleLineCount = 0;
        int tripleCount = 0;
        String line = "";
            
        //Read data from file
        while((line = br.readLine()) != null){ //has another line in the file
            lineCount++;
            if(!line.equals("")) { //is not a blank line
                Triple triples = extractTriples(line);
                if(triples.getHasTriple()) { //line contains triples
                    System.out.println(triples.getTriples());
                    tripleLineCount++;
                }
                for(int j = 0; j < triples.getTriples().length(); j++) {
                    if(triples.getTriples().charAt(j) == '(') tripleCount++;
                }
            }
        }
            
        //prints out the summary of the file
        System.out.println("\nSummary\n----------------------------------------");
        System.out.println("Total lines:              " + lineCount);
        System.out.println("Lines containing triples: " + tripleLineCount);
        System.out.println("Total number of triples:  " + tripleCount);  
    }
    
    /*Given a string, returns a Triple with a string containing the triples (if any) and a boolean stating whether
    or not it contains a triple.
    
    Assumptions:
    1.) If a '-' sign is found, it has been added. If preceeding a number (for example -32), the number is 32 where
        the '-' sign is simply garbage.
    2.) If a '.' is found in a number (for example 2.32), the potential integers are 2 and 32 where the '.' is
        garbage.
    3.) For part c, if the first valid character found is a letter, this will always be the real triple. It does not
        matter whether or not it is part of a word (for example, if it comes across "Dog a", 'D' will be the triple.)
    4.) The strings "<null>", "<cr>", "<lf>", and "<eof>" as well as multi-digit numbers (ex. 32) count as single
        characters. Thus, they cannot be broken up (no garbage in between the characters).
    */

    static Triple extractTriples(String str) {     
        /*Grammar:
        Triple is in form (a,b,c) where a is either a non-negative integer or the string "<null>", b is a
            non-negative integer where b <= a (b must be 0 if a is <null>), and c is either an individual letter 
            (upper or lower case), period, colon, semicolon, or one of the three strings "<cr>", "<lf>", or "<eof>".
        state == 0 ==> needs left parenthesis
        state == 1 ==> needs right parenthesis
        state == 2 ==> needs comma
        state == 3 ==> needs a
        state == 4 ==> needs b
        state == 5 ==> needs c
        */
        int state = 0;
        int a = -1;
        int b = -1;
        String triples = "";
        String tempTriples = "";
        
        for(int i = 0; i < str.length(); i++) {
            if(str.charAt(i) == '.' || str.charAt(i) == ':' || str.charAt(i) == ';' || str.charAt(i) == '<' ||
                    (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')
                    || (str.charAt(i) >= '0' && str.charAt(i) <= '9') || str.charAt(i) == ',' ||
                    str.charAt(i) == '(' || str.charAt(i) == ')') {
                if(state == 0) {
                    if(str.charAt(i) == '(') {
                        tempTriples = str.substring(i, i+1);
                        state = 3;
                    }
                }else if(state == 1) {
                    if(str.charAt(i) == ')') {
                        triples = triples + tempTriples + str.substring(i, i+1) + "  ";
                        tempTriples = "";
                        state = 0;
                        a = -1;
                        b = -1;
                    }
                }else if(state == 2) {
                    if(str.charAt(i) == ',') {
                        tempTriples = tempTriples + str.substring(i, i+1);
                        if(b != -1) state = 5;
                        else state = 4;
                    }
                }else if(state == 3) {
                    if(str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                        int j = i;
                        while(j < str.length() && str.charAt(j) >= '0' && str.charAt(j) <= '9') j++;
                        a = Integer.parseInt(str.substring(i, j));
                        i = j - 1;
                        tempTriples = tempTriples + a;
                        state = 2;
                    }else if(str.length() > i + 5 && str.substring(i, i+6).equals("<null>")) {
                        a = 0;
                        tempTriples = tempTriples + str.substring(i, str.indexOf(">", i)+1);
                        i = str.indexOf(">", i);
                        state = 2;
                    }
                }else if(state == 4) {
                    if(str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                        int j = i;
                        while(j < str.length() && str.charAt(j) >= '0' && str.charAt(j) <= '9') j++;
                        b = Integer.parseInt(str.substring(i, j));
                        i = j - 1;
                        if(b <= a) {
                            tempTriples = tempTriples + b;
                            state = 2;
                        }else b = -1;
                    }
                }else if(state == 5) {
                    if(str.charAt(i) == '.' || str.charAt(i) == ':'||(str.charAt(i) <= 'z' && str.charAt(i) >= 'a')
                            || str.charAt(i) == ';' || (str.charAt(i) <= 'Z' && str.charAt(i) >= 'A')) {
                        tempTriples = tempTriples + str.substring(i, i+1);
                        state = 1;
                    }else if((str.length() > i + 4 && str.substring(i, i+5).equals("<eof>")) ||
                            (str.length() > i + 3 && (str.substring(i, i+4).equals("<cr>") ||
                            str.substring(i, i+4).equals("<lf>")))) {
                        tempTriples = tempTriples + str.substring(i, str.indexOf(">", i)+1);
                        i = str.indexOf(">", i);
                        state = 1;
                    }else if(str.length() > i + 5 && str.substring(i, i+6).equals("<null>")) {
                        i = str.indexOf(">", i);
                    }
                }
            }
        }
        Triple triple = new Triple(true, triples);
        if(triples.equals("")) triple.setHasTriple(false); //does not contain a triple
        return triple;
    }
    
}

package projectbeng;
class Triple {
    boolean hasTriple = this.hasTriple;
    String triple = this.triple;
    
    //creates a new Triple
    Triple(boolean newHasTriple, String newTriple){
        this.hasTriple = newHasTriple;
        this.triple = newTriple;
    }
     //returns whether or not Triple contains any triples
    boolean getHasTriple() {
        return hasTriple;
    }
    
    //returns the triples in Triple
    String getTriples() {
        return triple;
    }
    
    //changes the state of whether a Triple contains triples
    void setHasTriple(boolean newHasTriple){
        this.hasTriple = newHasTriple;
    }
}

现在,我运行命令提示符,如图所示运行它。还可以看出,创建了 DataRecover 和 Triple 的 CLASS 文件。

Screenshot of Command Prompt

现在,我所做的是在我的 C 盘中创建了文件夹 ProjectBenG。我将 DataRecover.java 和 Triple.java 文件放在了这个文件夹中。之后,我运行了命令提示符。我很好奇我需要做什么才能运行 DataRecover。此外,您可以在文件夹中看到一个文件“triplestest.txt”。我希望这是在我的代码中由 DataRecover 处理的文件。我还没有走那么远,但是如果我需要做一些具体的事情才能让它也能正常工作,我会很感激这些信息

提前感谢大家的帮助。

【问题讨论】:

  • 现在最好花一个小时来了解 JAR 包以及如何使用 Netbeans 制作它们,而不是浪费大量时间在 javac 上。对于较大的应用程序,单独的 .class 文件无论如何都无法使用。 (可能你应该把类放到目录projectbeng,还要设置java参数-cp)
  • @OlegGritsak 我熟悉使用 NetBeans。问题是,有人需要使用我编写的程序。他们不想使用 NetBeans。相反,他们想通过命令提示符运行它。至于您的评论,我已将课程保存到“projectbeng”文件夹中。我尝试使用参数-cp,但似乎没有帮助。
  • 现在最好花一个小时来了解 JAR 包。然后你的“某人”会像 java -jar anyname.jar 这样运行你的程序,而不需要 Netbeans。整个应用程序将在单个 JAR 文件中

标签: java command-line


【解决方案1】:

我认为问题在于您的项目结构。

据我所知,您有两个选择。

1) 删除每个文件顶部的两个包声明。然后,像你一样,编译

$javac *.java

$java 数据恢复

2) 保持包完好无损,并将其放在名为 [包名称] 的文件夹中(在您的情况下为 projectbeng)。然后编译并从父目录运行。

$mkdir projectbeng​​p>

$cd projectbeng​​p>

$javac *.java

$cd ..

$java projectbeng.DataRecover

请记住,如果您正在运行 Windows,则目录/文件夹管理会略有不同。告诉我进展如何。

正如其他人所说,您可能想了解如何制作 jar 文件以供使用和分发。我猜你的教授喜欢简单地标记他的作业(我的也是如此)。无论如何......这个链接可能会帮助http://www.skylit.com/javamethods/faqs/createjar.html

编辑:查看您的屏幕截图,您似乎已经创建了文件夹,尝试向上移动一个目录并运行

$java projectbeng.DataRecover

【讨论】:

  • 非常感谢您的帮助!我能够成功地做我正在尝试的事情。我还查看了您发送给我的链接。它让事情变得容易多了。感谢您的宝贵时间!
  • 如果我帮助了你,请接受答案。它会帮助我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多