Reputation: 1
I am building online compiler website.
To execute a program in local system where we know the file name and class name.
eg: MyProgram.java
class MyProgram{
public static void main(String[] args){
System.out.println("Myprogram in local");//
}
}
$ javac MyProgram.java
$ java MyProgram
but in online compiler user can have any class name
eg : MyProgram.java//this can be whatever defined by admin
class UserProgram{
public static void main(String[] args){
System.out.println("users program");
}
}
$ javac MyProgram.java
$ java somethingthatidontknow //how do i get "users program" outpout ?
Upvotes: 0
Views: 356
Reputation: 370377
The name of the class only has to be the same as that of the file if the class is public. So if the user does not declare the class as public, the code will compile fine no matter the name of the file.
So then all you have to do is find out which of the created .class files contains the main
method. One way to do that would be to invoke javap
on each class file and grep the output for static void main
.
Note that when a class is declared public, that's a problem for most (all?) existing online IDEs. For example Ideone requires you to name your class Main
if you make it public.
Upvotes: 1