Well ... To evaluate custom code, you also need to know the class name using the main method.
I think the cheapest way is to get users to stick with some kind of code legend. ie "the package name must be foo.bar.baz, the name of the class is Qux, the main class is required." In this case, you can simply pass the error message from the compiler to the user as is and show the conditional agreement.
A slower way is to do some dirty tricks, for example, correctly process the resulting code with another class with a known name, mess with package names, and then try to make a reflexive call to the main class methods of the main , but in this case you will have problems with correct error reporting and itβs easy to do it wrong.
The right and potentially long way is to take a java source code parser (like this ), build an AST, find out the top level class, rename the file according to the package / class name and execute javac (or even produce bytecode from AST).
EDIT
You can also take the java-repl project. This may allow you to use script-like programs.
Hope this helps!
vsminkov
source share