当前位置: 首页 > 知识库问答 >
问题:

从命令提示符运行Java时出错

伊温书
2023-03-14

我遇到了从命令提示符运行java程序的问题。我有一个名为DataRecover的java文件,还有一个名为Triple的java文件。现在,当我在命令提示符下运行javac triple.java时,它会执行它应该执行的操作。但是,当我运行javac DataRecover.java时,它会出现这样的错误消息:“线程”main“java.lang.noClassDeffounderRror:DataRecover(错误名称:ProjectBeng\DataRecover

编辑:我包含了两个类。我现在已经能够运行javac命令,并且在适当的文件夹中有一个针对每个命令的类文件。现在,我需要在命令提示符下运行DataRecover文件。当我运行“Java DataRecover”时,我得到以下错误:“线程”main“java.lang.NoClassDefoundError: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文件的正确方法是什么?

共有1个答案

殷轶
2023-03-14

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

javac Triple.java DataRecover.java

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

 类似资料:
  • 我是刚到爪哇的。我正在尝试将大型机代码转换为Java。我在命令行下面运行。 我可以知道如何设置res.jar。 命令行代码

  • 我试图用命令提示符用java编译一个程序。我的程序在eclipse中运行良好,但是,当我试图在命令提示符中编译它时,我收到了一条错误消息。任何帮助和指导都将不胜感激。 triton.java:20:错误:不能dind符号循环Loop=new Loop(); 符号:类循环位置:类Triton trion.java:20:错误:找不到符号循环循环=新循环();sybmol:类循环位置:类Triton

  • 我最近开始学习Java编程语言的基础知识。为了在命令提示符下运行我的程序,我下载了java开发工具包(也称为JDK),并将我的Windows10系统路径设置为: C:\Program Files\java\jdk-9.0.1\bin;C:\Program Files\java\jre-9.0.1\bin 在用以下格式编写了一个简单的Hello World程序后: 并在命令提示符下使用 ,然后写 j

  • 我刚刚使用IntelliJ完成了一个相当大的项目,并说我会为没有IDE的人提供命令行语句来运行它(该项目将由不同的人在不同的机器上运行)。我有一段时间没有使用命令行了,所以我有点生疏了。我得到了一个NoClassDefFoundError:错误的名称,我一直在研究关于S/O的问题,比如为什么我在Java中得到NoClassDefFoundError?但这些似乎不能解决我的问题。我使用了相当多的外部

  • 问题内容: 我们如何在命令提示符下运行jar文件? 问题答案: 尝试这个

  • 问题内容: 我创建了一个Java项目来调用Web服务。它具有一个Main java文件和另一个class文件。我已经将一些jar文件用于HTTP客户端。 在Eclipse中,它运行良好。我需要通过传递一些参数在命令提示符下运行Java程序。 在命令提示符下,我转到包含主Java和子类Java文件的src文件夹,并给出了以下命令 我收到以下错误 mainjava.java:14:找不到符号 symb