Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a java main class from JSP

This is how my java main class looks like

     public class Main {

public static void main(String[] args) throws Exception {

    XmlParser b = new XmlParser();

    ServiceController sl = new ServiceController();

    Pipeline2 objPipeline2 = new Pipeline2();

    objPipeline2.main(args);

    b.parseXML();

    sl.callServiceByDomain();

}

public void function1() throws Exception {
    System.out.println("hello");

}

Here is the run.jsp file where i want to run the main function

<%@ page import="java.io.*" %>
<%@ page import="main.Main" %>

<HTML>
    <HEAD>
        <TITLE>Enter Email over here</TITLE>
    </HEAD>
    <BODY>
    <jsp:useBean id="link" scope="application" class = "main.Main" />   
  <%Main r=new Main();
  Main.main(null);%>
    </BODY>
</HTML>

I am not able to run it if i am running the run.jsp file where as if i am trying to run the function1 from the jsp in place of main....it is running. please help me in this regard

like image 673
user3324788 Avatar asked Jul 09 '26 04:07

user3324788


1 Answers

The main method accepts a parameter (String[] args). Try <%r.main(null);%> if you are not interested in passing any parameters.

BTW, main is static, so <%Main.main(null);%> should also work, and is the preferred way of calling a static method (i.e. without creating an instance of Main).

like image 166
Eran Avatar answered Jul 11 '26 17:07

Eran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!