tags:

views:

181

answers:

11

I am in the process of trying to untangle the (of course uncommented) code of a contractor that preceded (and I don't have access to currently) me at my current gig and I came across three pieces of code that I found odd and was hoping someone might clue me into why this might have been done, since I can't come up with any valid reason for this I was hoping someone else might clue me in:).

The application is structured as a Java web app, and there are a bunch of JSPs and a handful of servlets. Pretty standard stuff... until I came across four classes that are key to what this application is supposed to do. These classes do not extend HTTPServlet, but contain a main() method instead. To make things more confusing, the classes are not referenced anywhere in the project. It's as if they were just dumped in the containing package to avoid creating a new project?

My question in succinct form is this: is there a valid reason to have classes which contain main() methods contained in a web app?

A: 

They could be standalone unit tests for those parts of the app?

crazyscot
+7  A: 

Not really a valid one, but I've seen people put test code in main methods. It's convenient while you're developing the class, but it's pretty pointless now that there are so many unit test frameworks to choose from.

(Hm. I was tempted to ask if you work at my old company, but judging by the other responses, this is a more common bad practice than I thought.)

Bill the Lizard
A: 

I don't think there's any good reason, but a reason could be that it was used a form of unit testing?

Michael Barker
A: 

Main method with code? This mean test inside by class owner.

RoBYCoNTe
+1  A: 

Could be for testing purposes, could be code executed during build or deployment (called manually or from an Ant script), or they might be called from the app code via reflection (which would be pretty horrible).

Why don't you simply look at the code of those classes to see what it does and draw conclusions from there?

Michael Borgwardt
+1  A: 

The only practical reason I can think of is that they are called as command line processes to perform some function. Other than that, they sound dubious and should be removed to be safe. If nothing else, it will stop them confusing the next person.

Derek Clarkson
A: 

I have a main method in my web application. It generates sample data and write it to a DB.

It's needed to make customer able to test my application. I send him an application, he set corresponding configuration info of his DB, and then he run this main method in order to generate sample data. After that he can run an application and test it.

(there is also embedded HSQL DB, but he wanted to have possibility to test an application with his DB).

Roman
A: 

Besides what everyone else has said, which is that they are tests, I've also seen main methods used as a simple way to execute code, outside of loading the whole web framework.

Avi
A: 

I have seen main methods in Server based apps where those methods where used for batch processing. Quite a lot of batches where needed for our apps, and we didnt run those batches inside the app server but from a Control-M server. This was much easier than writing our own code to do reporting, scheduling, ...

Of course, those classes with main methods were always in a package called batch ;-)

Guillaume
+3  A: 

I think you are answering your own question:

It's as if they were just dumped in the containing package to avoid creating a new project?

Most probably the reason is just that, if the code is not test code.

fish
A: 

My question in succinct form is this: is there a valid reason to have classes which contain main() methods contained in a web app?

This is something I do sometimes when working with JAX-WS RI, I add a main method on classes (that will be deployed inside a war) to publish my endpoint using Sun's embedded server.

@WebService()
public class HelloWebService {
    public String sayHello(String name) {
        return "Hi" + name;
    }
    public static void main(String ... args) {
        HelloWebService h = new HelloWebService();
        Endpoint endpoint = Endpoint.publish("http://localhost:8081/hello", h);
    }
}

I find it useful when working on the client. So ease of development might be another "justification".

Pascal Thivent