vamsi3
vamsi3

Reputation: 115

How to compile a .java file which is been arbitrarily named different from public class name

I had been creating an online judge platform like codeforces.com on Django recently. Now, I'm struck across a problem that the user may possibly upload a Java file for testing with the filename different from the public class defined inside it. Yet, I should compile it automatically. So, what could be a possible solution to this?

If it's impossible to compile the file without renaming it properly, I need guidance how this 'renaming' can be done using a script/code?

Upvotes: 0

Views: 51

Answers (2)

lexicore
lexicore

Reputation: 43651

The easiert would be probably to parse the uploaded Java file using something like JavaParser, then extract the package name and top-level class name and rename the uploaded file accordingly.

By the way, make sure you run the uploaded code in a pretty sandboxed environment.

Upvotes: 0

user2862544
user2862544

Reputation: 425

It will be a compilation error. file name should be same as public class name. hence not possible at all

Upvotes: 2

Related Questions