简体   繁体   English

代码可以在Netbeans中编译并运行,但不能在Java命令行中运行

[英]Code compiles and runs in Netbeans but not java command line

but when I run the program using the command line. 但是当我使用命令行运行程序时。 I get a run time error of "java.lang.NoClassDefFoundError". 我收到“ java.lang.NoClassDefFoundError”的运行时错误。 All I did was copy the code from Netbeans and paste it on a notepad file and then tried running it by command prompt. 我所做的就是从Netbeans复制代码并将其粘贴到记事本文件中,然后尝试在命令提示符下运行它。 I am not sure what I am/ did wrong. 我不确定自己做错了什么。 Any feedback is greatly appreciated it! 任何反馈,我们将不胜感激! Here is my code BTW 这是我的代码顺便说一句

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

Your class Reader is defined in the reader package . 您的类Reader是在reader package定义的。 You need to give the JVM the proper class path. 您需要为JVM提供正确的类路径。 Create a folder called reader and place your class there. 创建一个名为reader的文件夹,然后将您的课程放在此处。 Then use the -classpath flag when call java. 然后在调用java时使用-classpath标志。

c:\>javac reader\Reader.java
c:\>java -classpath . reader.Reader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java代码在命令行中编译,但不能在NetBeans中编译 - Java Code Compiles in Command Line but not NetBeans Java项目在netbeans中运行,但不能从命令行运行 - Java project runs in netbeans but not from command line Java泛型代码在eclipse中编译,但不在命令行中编译 - Java generics code compiles in eclipse but not in command line Netbeans 上的 NoClassDefFoundError 但在命令行上运行 - NoClassDefFoundError on Netbeans but runs on command line 同一JDK上的相同代码在Eclipse上编译,但在Netbeans,IDEA或命令行上导致不可转换的类型 - Same code on same JDK compiles on Eclipse but causes inconvertible types on Netbeans, IDEA or command line Java 编译没有错误,运行第一行,然后不继续或结束 - Java compiles with no errors, runs first line, then doesn't continue or end java项目从命令行运行良好,但从NetBeans运行时会抛出异常 - A java project runs well from the command line, but it throws an exception when run from NetBeans Java jit编译器是否每次运行都会编译其代码? - Does Java jit compiler compiles its code every time it runs? 识别在Java和C#中编译但运行方式不同的代码 - identifying code that compiles in both Java and C# but runs differently Java程序在eclipse中编译而不是netbeans? - Java program compiles in eclipse but not netbeans?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM