## Question Authors' Forum

### Detection of the main class name in Java

by Antoni Oliver -
Number of replies: 2

Hello, I'll start to use this this next course and I'd like to know if there is a way to work around the detection of the main class in Java assignments when they are attached.

So, the following code would work in the text box:

public class Main {    public static void main(String[] args) {        System.out.println("Hello world!");    }}

But if it is attached in a file named Main.java, it cannot find the prog class, so you need to rename the file to prog.java and change the class name to prog (which goes against Java naming convention).

Is there a way to either:

a) Fix this, so it works with attachments.

b) Force the name of the main class (either always the same, like Main, or exercise-dependent, like Ex123)

Thank you!

EDIT:

Ok, so... I think b) is possible.

I've checked the source code. And apparently one can define the sourcefilename param in the advanced customization area, as this:

{"sourcefilename": "Main.java"}

Also, per the source code, I see that the main class name guessing is only performed for the code in the text box, and not for the code in the attachments, so a) is not done.

Tags:

### Re: Detection of the main class name in Java

by Richard Lobb -

I don't fully understand what question types you're using nor how you're setting up your question(s), so I'm not sure I can answer properly.

If you're using the Java Program question type, students need to provide non-empty executable Java code in the answer box, and it's that code that gets run. If they also attach Java files - presumably classes needed by their main program - it's then over to them what names they give the files, but of course those names will need to match the names required by the class loader when it executes their main program. There's not generally any need to do name-fiddling on attached files in this context.

However, it sounds like you're expecting students to provide their main program as an attachment, so what's in the answer box and what question type are you using?

If you're letting students submit attachments, you can specify the allowable file names of those attachments, so again I'm not sure why you need to do any name fiddling.

### Re: Detection of the main class name in Java

by Antoni Oliver -

Hello.

This is for a java_program question type.

So, let me ellaborate: we normally work in an IDE (NetBeans), so I expect part of the students to upload their java files directly and the other part to work directly on Moodle.

Those who code in Moodle will surely make use of the answer box, at least until we start dealing with additional code files when maybe, if they have to code in two places in the same time (Moodle for the main class and some text editor / IDE for the additional classes), they will switch to work entirely in the IDE.

So, if they work in the IDE, I expect them to submit all their files, and not to copy the contents of one in the answer box and provide them as attachments.

Anyway, CodeRunner does work with an empty answer box and attachments, but it does not autodetect the main class name, as it does when the code is submitted with the answer box (it is not trivial to know which one of the attachments is the main class, I guess).

And yes, there's no need to restrict file names. Sure, I expect the main class to be named Main.java, but they'll submit other files which I don't know their names.

As I said previously, a compromise is using the parameters in advanced configuration:

{"sourcefilename": "Main.java"}
This way, it will always execute the class named Main. If they do not provide it, it is going to fail, which is good enough for me.

Thank you!