-2

I have two applications (Two projects) on Eclipse :

Application 1 : This is normal Java standard application which has public static void main method as entry point of the application. (Jar)

Application 2 : This is the Rest web service using Jersey.

I want to call Application 1 from Application 2 on particular url. How can I do that ? To deploy application 2 , i can put war on tomcat folder, but what should i do about application 1 jar (should this be exectuable?)

I want to deploy these two applications on web container/server such that Client can call application1 through application 2 and person on server can call aplication 1 as normal application too.

tec
  • 41
  • 9

1 Answers1

2

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks a lot for your suggestion. How will I call application 1 as standalone if I dont have main method , so I should have two entry points for application 1 then ? one with main and one as you suggested for web. – tec Dec 13 '15 at 23:45
  • Import and call the bootstrap class of that application, such as `SomeClass` in above example. You should just do exactly the same as the `main()` method does: preparing arguments and invoking the boostrap class and presenting results. – BalusC Dec 14 '15 at 07:45