There's a conceptual mistake.
The main()
method is basically only for the command line interface. It takes CLI arguments, validates and prepares them to proper arguments if necessary and then finally does the "usual" Java thing of creating and invoking some bootstrap class. A good main()
method basically boils down to this:
public class MainClass {
public static void main(String... args) {
// Prepare arguments.
String foo = args[0];
Integer bar = Integer.valueOf(args[1]);
// Do the real work.
SomeClass someClass = new SomeClass();
SomeResult someResult = someClass.fuzz(foo, bar);
// Present result and exit.
System.out.println(someResult);
System.exit(0);
}
}
It makes no sense to invoke such a method from a web application. Even more, it could be potentially dangerous when such a main()
method explicitly invokes System#exit()
. It could shut down the whole server! (although the more decent servers have builtin restrictions to prevent code like that).
You should merely add a web interface instead of using the command line interface. Just bake a normal JAR of the Java application project and put it in /WEB-INF/lib
folder of the webapp (or via Deployment Assembly in case you're using Eclipse and have both projects in workspace). It doesn't need to be executable (even if it were, that fact would be ignored by the webapp anyway). Given that you're using JAX-RS as web interface, here's how the proper replacement look like:
@Path("main")
public class MainResource {
@GET
@Path("fuzz/{foo}/{bar}")
@Produces(MediaType.TEXT_PLAIN)
public String fuzz(@PathParam("foo") String foo, @PathParam("bar") Integer bar) {
// Do the real work.
SomeClass someClass = new SomeClass();
SomeResult someResult = someClass.fuzz(foo, bar);
// Return result.
return someResult.toString();
}
}
No need to execute/process/invoke the main()
method via the CLI/ProcessBuilder
in the webapp. That wouldn't make sense if it's Java already and can simply be placed in the runtime classpath.
If you, however, have the entire logic in the main()
method, then you've an object oriented design problem. It would be time to refactor reusable code into a SomeClass
like above so it can be reused elsewhere, such as in your JAX-RS resource class.