0

但是当我使用命令行运行程序时。我收到“java.lang.NoClassDefFoundError”的运行时错误。我所做的只是从 Netbeans 复制代码并将其粘贴到记事本文件中,然后尝试通过命令提示符运行它。我不确定我是/做错了什么。任何反馈都非常感谢!这是我的代码顺便说一句

package reader;



import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

import java.util.Scanner;


public class Reader{

public static final Scanner in = new Scanner( System.in);
static final int adult = 0;  // The index representation numbers for the total and count arrays;
static final int child = 1;
static final int adultMale = 2;
static final int adultFemale = 3;
static final int childMale = 4;
static final int childFemale = 5;
static final int people = 6;
static final int family = 7;


public static void main(String[] arg){

    if(arg.length > 2){ die("Too many arguments");}
    else {System.out.println("Good");}

String inFileName;

    if(arg.length > 0){ inFileName = arg[0];}
    else {
    inFileName = "population.txt";}

Scanner fin = openFile(inFileName);
int[] count = new int[8];  // adults,children,male adult, female adult, male child , female child, people, family
int[] total = new int[8]; //adults,children,male adult, female adult, male child , female child, people, family

for(  ;  fin.hasNextLine();  ){
     String line = fin.nextLine();
    String error = check(line);

    if(error != null){die(error);}
    else{ gather(line, count, total);}

}//loop

for(int i = 0; i< count.length; i++){ System.out.print(count[i] + " ");}
System.out.println();
for(int i = 0; i< total.length; i++){ System.out.print(total[i] + " ");}
System.out.println();
System.out.println((float)count[family]/count[people]);

fin.close();

String outFileName;
if( arg.length > 1){ outFileName = arg[1];}
else{outFileName = "output.txt";}

PrintStream fout = outFile(outFileName);

showCensus(fout,count,total);

}//main


public static void die(String message){

System.err.println("Error: " + message);
System.exit(1);

}//die 

public static Scanner openFile(String fileName){

Scanner inputFile = null;
try{
inputFile = new Scanner(new File(fileName));
}
catch(FileNotFoundException e){ die("File not found: " + fileName);
}

return inputFile;

}// OpenFIle

public static PrintStream outFile(String fileName){

    Scanner temp = null;
    try{
    temp = new Scanner(new File(fileName));
    } catch(FileNotFoundException ei){
        PrintStream result = null;
        try{
        result = new PrintStream( new File(fileName));
        }catch(FileNotFoundException eo){die("Can't open " + fileName);}
        return result;
    }
    die("The file " + fileName + " already exists!");
    return null;
}

public static String check(String line){
    int change = 0;

    String sex;
    int age;
    Scanner sin = new Scanner(line);
    if(!sin.hasNext()){return null;}
    if(sin.next().equalsIgnoreCase("Comment")){return null;}
    Scanner sin2 = new Scanner(line);
    while(sin2.hasNext()){
       change++;
       if(change % 2 == 0){} 
       else{ 
           sex = sin2.next();
           if(!sex.equals("M")&& !sex.equals("F")){return "Gender must be 'M' or 'F', not " + sex;}
           if(!sin2.hasNext()){return "No data after " + sex ;}
           if(!sin2.hasNextInt()){return "age must be a number not " + sin2.next();}
           age = sin2.nextInt();
           //System.out.print(sex + " " + age + " ");
       }

    }

    System.out.println();

    return null;

}


public static void gather(String line, int[] count, int[] total){

   int change = 0; 
   Scanner sin = new Scanner(line);
   if(!sin.hasNext()){return ;}
   if(sin.next().equalsIgnoreCase("Comment")){return;}
   Scanner sin2 = new Scanner(line);
   while(sin2.hasNext()){
       change++;
       if(change % 2 == 0){}
       else{
           String sex = sin2.next();
           int age = sin2.nextInt();
           if(sex.equals("M") && age > 17){
               count[adultMale]++; 
               count[adult]++; 
               count[people]++;
               total[adultMale]+= age;
               total[adult]+= age;
               total[people]+= age;}
           else if(sex.equals("M") && age <= 17){
               count[child]++; 
               count[people]++; 
               count[childMale]++;
               total[child]+= age;
               total[people]+= age;
               total[childMale]+= age;}
           else if(sex.equals("F") && age > 17 ){
               count[adult]++; 
               count[adultFemale]++; 
               count[people]++;
               total[adult]+= age;
               total[adultFemale]+= age;
               total[people]+= age;}
           else if(sex.equals("F") && age <= 17){
               count[childFemale]++; 
               count[child]++; 
               count[people]++;
               total[childFemale]+= age;
               total[child]+= age;
               total[people]+= age;}
       }
   }// while

count[family]++;







}

public static void showCensus(PrintStream out, int[] count, int[] total){

    out.println("The Family Statistics 2013 Report");
    out.println();
    out.println("People: " + count[people] + " Average Age: " + (float)total[people]/count[people]);
    out.println("   Adults: " + count[adult] + " Average Age: " + (float)total[adult]/count[adult]);
    out.println("       Males: " + count[adultMale] + " Average Age: " + (float)total[adultMale]/count[adultMale]);
    out.println("       Females: " + count[adultFemale] + " Average Age: " + (float)total[adultFemale]/count[adultFemale]);
    out.println("   Children: " + count[child] + " Average Age: " + (float)total[child]/count[child]);
    out.println("       Males: " + count[childMale] + " Average Age: " + (float)total[childMale]/count[childMale]);
    out.println("       Female: " + count[childFemale] + " Average Age: " + (float)total[childFemale]/count[childFemale]);
    out.println("Families: " + count[family] + " Average Family Size " + (float)count[family]/count[people]);



}





}//Reader
4

1 回答 1

1

您的类Readerreader package. 您需要为 JVM 提供正确的类路径。创建一个名为 reader 的文件夹并将您的课程放在那里。然后在调用 java 时使用 -classpath 标志。

c:\>javac reader\Reader.java
c:\>java -classpath . reader.Reader
于 2013-04-26T20:01:22.113 回答