Why does the Java web application contain a series of classes with the main () method?

I try to unravel (of course, without comment) the code of the contractor that was preceded (and I don’t have access to it now) at my current concert, and I came across three parts of the code that I found strange and hoped that someone could understand me, why this could be done, since I cannot come up with any good reason for this, I was hoping that someone else could understand me :).

The application is structured as a Java web application, and there are many JSPs and several servlets. Pretty standard stuff ... until I came across four classes that are the key to what this application should do. These classes do not extend the HTTPServlet , but contain the main() method. To make things more confusing, classes are not mentioned anywhere else in the project. As if they were simply dumped into the containing package to avoid creating a new project?

My question in condensed form is this: is there any good reason to have classes containing main() methods contained in a web application?

+4
source share
11 answers

I think you are answering your question:

As if they were simply dumped into the containing package to avoid creating a new project?

Most likely, the reason is if the code is not a test code.

+3
source

Not really correct, but I saw people putting test code in the main methods. This is useful when you are developing a class, but now it’s pretty pointless that there are so many unit test frameworks.

(Hmm. I was tempted to ask if you work for my old company, but judging by other answers, this is more common bad practice than I thought.)

+7
source

It may be for testing purposes, it may be code that is executed during assembly or deployment (called manually or from an Ant script), or they can be called from application code via reflection (which would be pretty awful).

Why don't you just take a look at the code of these classes to see what it does and draw conclusions from there?

+1
source

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

+1
source

Can they be standalone unit tests for these parts of the application?

0
source

I don't think there is any good reason, but reason a is that it was used in the form of unit testing?

0
source

The main method with code? This is an average test within the class owner.

0
source

I have a main method in my web application. It generates sample data and writes it to the database.

This required the client to test my application. I send him a request, he sets the appropriate configuration information for his database, and then he runs this basic method to create sample data. After that, he can run the application and test it.

(there is also a built-in HSQL database, but he wanted to be able to test the application from his database).

0
source

Besides the fact that everyone else said that these are tests, I also saw the main methods used as an easy way to execute code, outside of loading the entire web infrastructure.

0
source

I saw the main methods in server-based applications, where those methods are used for batch processing. There are quite a few parties where it is necessary for our applications, and we did not run these parties inside the application server, but from the Control-M server. It was much easier than writing custom code for reporting, planning, ...

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

0
source

My concise question is this: is there a good reason to have classes that contain main () methods contained in a web application?

This is what I sometimes do when working with JAX-WS RI, adding the main method for classes (which will be deployed inside the war) to publish my endpoint using the built-in Sun 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 this useful when working with a client. Therefore, ease of development may be another “excuse”.

0
source

Source: https://habr.com/ru/post/1311861/


All Articles