Forked Java VM exited abnormally. JUnit Test?

2019-08-15 12:10发布

问题:

I have a simple Java program that seems to work well until uploaded to my school's grading system, "WebCat", which I'm assuming is just running JUnit. The error it kicks back is:

Forked Java VM exited abnormally. Please note the time in the report does not reflect the >time until the VM exit.

I've researched this issue and and the main first troubleshooting step seems to be to look at the dump log. Unfortunately I cannot do that in this case. I am really at a loss on how to begin to troubleshoot this considering the lack of feedback from the grading system and the lack of compile or run-time errors.

Here is the code if anyone is familiar with this error or can at least give me some direction of where to begin troubleshooting. Much appreciated!

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;
        }
        }


   }
}

break

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;
      }
    }

}

回答1:

Your program is calling System.exit(0) for certain inputs. Don't do that! That's exactly what the message is telling you: that the JVM exited in the middle of the text, before the scoring code could finish up. Instead of calling exit(), just use return to return from main() early.



回答2:

The library System Rules has a JUnit rule called ExpectedSystemExit. With this rule you are able to test code, that calls System.exit(...).



标签: java junit