【问题标题】:Forked Java VM exited abnormally. JUnit Test?分叉的 Java VM 异常退出。 JUnit 测试?
【发布时间】:2013-03-06 08:02:25
【问题描述】:

我有一个简单的 Java 程序,它在上传到我学校的评分系统“WebCat”之前似乎运行良好,我假设它只是运行 JUnit。它返回的错误是:

分叉的 Java VM 异常退出。请注意,报告中的时间并不反映 >VM 退出前的时间。

我已经研究过这个问题,主要的第一个故障排除步骤似乎是查看转储日志。不幸的是,在这种情况下我不能这样做。考虑到评分系统缺乏反馈以及没有编译或运行时错误,我真的不知道如何开始解决这个问题。

如果有人熟悉此错误,或者至少可以给我一些从哪里开始排除故障的方向,这里是代码。非常感谢!

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.io.IOException;



class PlayerApp {
    public static void showMenu()
    {
        System.out.println("Player App Menu");
        System.out.println("P - Print Report");
        System.out.println("A - Add Score");
        System.out.println("D - Delete Score");
        System.out.println("L - Find Lowest Score");
        System.out.println("H - Find Highest Score");
        System.out.println("Q - Quit");
    }
    public static void main(String[] args) throws IOException 
    {   
        if (args.length == 0)
        {
            System.out.println("File name was expected as a run argument.");
            System.out.println("Program ending.");
            System.exit(0);
        }
        String fileName = args[0];
        Scanner sc = new Scanner(System.in);
        String stnew = "";
        boolean exit = false;
        Player p = null;
        double[] scoreList;

        File dbFile = new File(fileName);
        FileInputStream fis = new FileInputStream(fileName); 
        InputStreamReader inStream = new InputStreamReader(fis); 
        BufferedReader stdin = new BufferedReader(inStream);
       String name = stdin.readLine();
       stnew = stdin.readLine();
       int numScore = Integer.parseInt(stnew);
       scoreList = new double[numScore];
       for (int i = 0; i < numScore; i++)
       {
          stnew = stdin.readLine();
          scoreList[i] = Double.parseDouble(stnew);
       }

       p = new Player(name, numScore, scoreList);

       stdin.close();

        System.out.println("File read in and Player object created.");
       showMenu();
       while (exit == false)
       {

        System.out.print("\nEnter Code [P, A, D, L, H, or Q]:");
        String choice = sc.nextLine().toLowerCase();
        if (choice.equals("p"))
        {
            System.out.println(p.toString());
        }
        else if (choice.equals("a"))
        {
            System.out.print("   Score to add: ");
            stnew = sc.nextLine();
            double scoreIn = Double.parseDouble(stnew);
            p.addScore(scoreIn);
        }
        else if (choice.equals("d"))
        {
            System.out.print("   Score to delete: ");
            stnew = sc.nextLine();
            double scoreIn = Double.parseDouble(stnew);
            p.deleteScore(scoreIn);
            System.out.println("   Score removed.");
        }
        else if (choice.equals("l"))
        {
            System.out.println("   Lowest score: " + p.findLowestScore());
        }
        else if (choice.equals("h"))
        {
            System.out.println("   Highest score: " + p.findHighestScore());
        }
        else if (choice.equals("q"))
        {
            exit = true;
        }
        }


   }
}

休息

import java.text.DecimalFormat;



public class Player {

    //Variables
    private String name;
    private int numOfScores;
    private double[] scores = new double[numOfScores];

    //Constructor
    public Player(String nameIn, int numOfScoresIn, double[] scoresIn) {
       name = nameIn;
       numOfScores = numOfScoresIn;
       scores = scoresIn;
   }

    //Methods
    public String getName() {
       return name;
    }
    public double[] getScores() {
       return scores;
    }
    public int getNumScores() {
       return numOfScores;
    }
    public String toString() {

        String res = "";
        DecimalFormat twoDForm = new DecimalFormat("#,###.0#");
      DecimalFormat twoEForm = new DecimalFormat("0.0");
        res += "   Player Name: " + name + "\n   Scores: ";
        for (int i = 0; i < numOfScores; i++)
        {
            res += twoDForm.format(scores[i]) + " ";
        }
        res += "\n   Average Score: ";
        res += twoEForm.format(this.computeAvgScore());
        return res;
    }
    public void addScore(double scoreIn) {
       double newScores[] = new double[numOfScores +1 ];
       for (int i = 0; i < numOfScores; i++)
       {
           newScores[i] = scores[i];
       }
       scores = new double[numOfScores + 1];
       for(int i = 0; i < numOfScores; i++)
       {
           scores[i] = newScores[i];
       }
       scores[numOfScores] = scoreIn;
       numOfScores++;
    }
    public boolean deleteScore(double scoreIn) {
        boolean found = false; 
        int index = 0;
        for (int i = 0; i < numOfScores; i++)
        {
           if (scores[i] == scoreIn)
            {
                found = true;
                index = i;
            }
        }
        if (found == true)
        {
            double newScores[] = new double[numOfScores -1 ];
            for (int i = 0; i < index; i++)
            {
               newScores[i] = scores[i];
            }
            for (int i = index + 1; i < numOfScores; i++)
            {
                newScores[i - 1] = scores[i];
            }
            scores = new double[numOfScores - 1];
            numOfScores--;
            for (int i = 0; i < numOfScores; i++)
            {
                scores[i] = newScores[i];
            }
            return true;
        }
        else
        {
            return false;
        }


    }
    public void increaseScoresCapacity() 
    {
        scores = new double[numOfScores + 1];
        numOfScores++;
    }
    public double findLowestScore() {
        double res = 100.0;
        for (int i = 0; i < numOfScores; i++)
        {
            if (scores[i] < res)
            {
                res = scores[i];
            }
        }
        return res;
    }
    public double findHighestScore() {
        double res = 0.0;
        for (int i = 0; i < numOfScores; i++)
        {
            if (scores[i] > res)
            {
                res = scores[i];
            }
        }
        return res;
    }
    public double computeAvgScore() {
        double res = 0.0;
      if (numOfScores > 0) {
           for (int i = 0; i < numOfScores; i++)
        {
               res += scores[i];
           }
           return res / (double)(numOfScores);
      }
      else {
         //res = 0.0;
         return res;
      }
    }

}

【问题讨论】:

    标签: java junit


    【解决方案1】:

    您的程序正在为某些输入调用System.exit(0)。不要那样做!这正是消息告诉您的内容:在评分代码完成之前,JVM 在文本中间退出。不用调用exit(),只需使用returnmain()提早返回即可。

    【讨论】:

    • 天哪!承认我在这个小问题上浪费了时间是很尴尬的。非常感谢您的宝贵时间!
    • 这是我在现实生活中可能遇到的问题,还是只是自动评分问题?似乎“退出”程序是我在程序的那个时刻想要做的?
    • 在现实生活中拨打exit() 时确实需要小心。如果你自己写main(),那就去吧,因为你在控制之中。另一方面,如果您正在编写一个与扫描仪对话的库(只是一个示例),那么该库最好永远不要调用exit(),因为您不知道什么样的程序可能会调用您.如果我使用你的库并且我的程序神秘地退出了,我会不高兴的!
    • System.exit() 导致 JVM 立即关闭。您通常不希望在应用程序中执行此操作,除非有明确定义的关闭操作等位置。
    【解决方案2】:

    System Rules 有一个名为ExpectedSystemExit 的JUnit 规则。使用此规则,您可以测试调用 System.exit(...) 的代码。

    【讨论】:

      猜你喜欢
      • 2010-12-23
      • 1970-01-01
      • 2012-02-06
      • 2017-10-23
      • 2015-11-20
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多