Runtime Error in onlinejudge.org website for Java submission


I am using OnlineJudge.org website for years now but I used to submit my solutions in C++ and ANSI C. Recently I started submitting code in Python and there was no big issue. Today I tried to submit a very simple Java code and it started giving Runtime Error. The code was working for all the inputs and I was not able to recreate the Runtime Error even on uDebug website test cases.

Just see below screenshot:

Almost every program I submitted gave my Runtime Error.

So, after debugging a lot I came to know that for submission to OnlineJudge.org website your Java code should be structured in following way:

/* Your main class should not be public else it will give Compilation Error */
/* The class should be having name "Main" else it will give Runtime Error */
class Main{
    public static void main(String[] args){
        /* Your code here */
    }
}

This might be required because the code submitted might have been saved on compilation/test server with some temporary file so if we place public in class then the file containing code must have the class name as file name, so we can’t put public with our Main class.

Other thing if we place any name in class, for example Test, the class file after compilation will be created with name Test, so the online judge will not be able to find the class file as any user submitting the code can put any name for the class. So, in order to help online judge find the class one need to specify the class name as “Main” only.

Hope, this is helpful for some people starting to go towards competitive programming in Java.

If this is helpful, please share a comment in comment section.

Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post