【问题标题】:Errors When Running Java from Command Prompt从命令提示符运行 Java 时出错
【发布时间】:2017-08-23 20:47:36
【问题描述】:

我在从命令提示符运行 java 程序时遇到问题。我有一个名为 DataRecover 的 java 文件,还有一个名为 Triple 的 java 文件。现在,当我在命令提示符中运行 javac Triple.java 时,它会做它应该做的事情。但是,当我运行 javac DataRecover.java 时,它带有以下错误消息:“线程中的异常“main”java.lang.NoClassDefFoundError:DataRecover(错误名称:projectbeng\DataRecover

DataRecover.java:61:错误:找不到符号
静态三重 extractTriples(String str) {
^
符号:三重类
位置:类 DataRecover
DataRecover.java:30:错误:找不到符号
三元组 = extractTriples(line);
^
符号:三重类
位置:数据恢复类

编辑:我已经包含了这两个类。我现在已经能够运行 javac 命令,并且在正确的文件夹中每个都有一个 CLASS 文件。现在,我需要在命令提示符中运行 DataRecover 文件。当我运行“java DataRecover”时,出现以下错误:“线程“main”java.lang.NoClassDefFoundError 中的异常:DataRecover(错误名称:projectbeng\DataRecover)”。

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 文件的正确方法是什么?

【问题讨论】:

  • 请发布更多您的代码。我们需要查看文件中代码的结构。
  • @Blorgbear 我添加了更多代码并解释了我当前的问题。

标签: java command-line


【解决方案1】:

当您在其他文件中引用源文件时,您必须将所有这些文件放在一起。在您的情况下,它应该是:

javac Triple.java DataRecover.java

许多现代 Java 项目使用构建工具来帮助管理源文件。两个流行的 Java 构建工具是 Gradle 和 Maven。

【讨论】:

  • 谢谢。这解决了我的问题;但是,我现在有另一个。 DataRecover 和 Triple 的 CLASS 文件现在都在文件夹中,但是当我在命令提示符中运行“java DataRecover”时,它显示“线程中的异常“main”java.lang.NoClassDefFoundError:DataRecover(错误名称:projectbeng\DataRecover” . 我似乎还缺少其他东西。
猜你喜欢
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
相关资源
最近更新 更多