Make a jsf page use a java class
<p>I'm working on my project in my university which is on a social-media website and I came to a problem. I have my jsf pages and i also created a java class(more exactly got it on the web) for chatting among them. The user is supposed to be viewing a friend's profile where there is a button which he/she can press and a new JFrame appears for chatting.</p>

<p>However, the Chatbox.java already has a main. I modified the codes a little, changing the main function. It looks like this:</p>

<pre><code>@ManagedBean(name="chatbox")
@SessionScoped

public class ChatBox extends JFrame implements ActionListener { 
    String name; 
    InetAddress iadr; 
    int port; 
    MulticastSocket so; 
    JTextArea txt = new JTextArea(); 
    JScrollPane sp = new JScrollPane(txt); 
    JTextField write = new JTextField(); 
    JButton quit = new JButton("Go Offline"); 

    public ChatBox(String username, String groupAdr, int portNr) throws IOException { 
        name = username; 
        iadr = InetAddress.getByName(groupAdr); 
        port = portNr; so = new MulticastSocket(port); 
        so.joinGroup(iadr); 
        new Receiver(so,txt); 

        setTitle("Chatting with "+ name); 
        txt.setEditable(true); 
        add(quit,BorderLayout.NORTH); 
        add(sp,BorderLayout.CENTER); 
        add(write,BorderLayout.SOUTH); 
        quit.addActionListener(this); 
        write.addActionListener(this); 
        setSize(400,250); 
        setVisible(true); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
    }

    public void sendMess(String s) { 
        byte[] data = (name + ": " + s).getBytes(); 
        DatagramPacket packet = new DatagramPacket(data,data.length,iadr,port); 
        try { 
            so.send(packet); 
        } catch(IOException ie) { 
            Toolkit.getDefaultToolkit().beep(); 
            JOptionPane.showMessageDialog(null, "Data overflow !"); 
        } 
    }

    public void actionPerformed(ActionEvent e) { 
        if (e.getSource() == write) { 
            sendMess(write.getText()); 
            write.setText(""); 
        } 
        else if (e.getSource() == quit) { 
            sendMess("Offline"); 
            try { 
                so.leaveGroup(iadr); 
            } catch(IOException ie) { 
                Toolkit.getDefaultToolkit().beep(); 
                JOptionPane.showMessageDialog(null, "Data overflow, connection error !");
            } 
            so.close(); dispose(); System.exit(0); 
        } 
    }

    public void mmm()throws IOException { 
        String in = JOptionPane.showInputDialog(null,"What's your name?"); 
       //  if(arg.length&gt;0) in = arg[0];
        new ChatBox(in, "224.0.0.7",3); 
    }

    class Receiver implements Runnable { 
        Thread activity = new Thread(this);
        MulticastSocket so; 
        JTextArea txt; 

        Receiver(MulticastSocket sock, JTextArea txtAr) { 
            so = sock; 
            txt = txtAr;
            activity.start(); 
        }

        public void run() { 
            byte[] data = new byte[1024]; 
            while(true) try { 
                Calendar calendar = Calendar.getInstance();
                java.util.Date now = calendar.getTime();
                java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());

                DatagramPacket packet = new DatagramPacket(data,data.length); 
                so.receive(packet); 
                String mess = new String(data,0,packet.getLength()); 
                txt.append(currentTimestamp+ "\n" +mess+ "\n"); 
                System.out.println(mess);
            } catch(IOException e) { 
                break; 
            }    
        } 
    }
}
</code></pre>

<p>And here is the jsf page where i'm trying to call for that Chatbox.java by pressing a button:</p>

<pre><code>&lt;h:body&gt;
    &lt;div id="top"&gt;
        &lt;ui:insert name="top"&gt;

            &lt;h:form&gt;
                &lt;div style="height: 70px; margin: 10px auto 0px; background: #00A2E8; text-align: left;"&gt;
                    &lt;a href="../User/userhome.xhtml?faces-redirect=true"&gt;&lt;img src="../Images/logo.png" style="width: 100px; height: 70px; float: none;"/&gt;&lt;/a&gt;
                    &lt;div style="float: right; text-align: right;"&gt;
                        &lt;h:panelGrid columns="2"&gt;
                            Hello,&lt;h:outputLabel value="#{usersController.username}" /&gt;


                            &lt;h:commandButton value="Logout" action="/Guest/guesthome.xhtml?faces-redirect=true"/&gt;
                        &lt;/h:panelGrid&gt;
                    &lt;/div&gt;
                &lt;/div&gt;

            &lt;/h:form&gt;

        &lt;/ui:insert&gt;
    &lt;/div&gt;

    &lt;div id="content" class="center_content" style="background-image: url('./Images/backgroundblue.jpg'); background-repeat: no-repeat;
background-size:cover;"&gt;
        &lt;ui:insert name="content"&gt;
            &lt;div id="content2" style="background-color: #CCC; width: 1000px; margin: 0 auto; opacity: 1;
filter: alpha(opacity=40);"&gt;

                &lt;h:form style="margin-left: 300px;"&gt;
                    &lt;ui:include src="/User/usertemplate/sprybar.xhtml" /&gt;
                &lt;/h:form&gt;
                &lt;br&gt;&lt;/br&gt;
                &lt;br&gt;&lt;/br&gt;
                &lt;br&gt;&lt;/br&gt;
                &lt;h:form style="border-bottom: 1px solid #AFAFAF;"&gt;&lt;/h:form&gt;
                &lt;br&gt;&lt;/br&gt;

                &lt;h:form style="text-align: center;"&gt;
                    &lt;div style="text-align: center;"&gt;
                        &lt;h1&gt;&lt;h:outputText value="#{usersController.selected.firstname}" title="#{bundle.ViewUsersTitle_firstname}"/&gt;
                        &lt;h:outputText value="&amp;#160;" /&gt;
                        &lt;h:outputText value="#{usersController.selected.lastname}" title="#{bundle.ViewUsersTitle_lastname}"/&gt;&lt;/h1&gt;
                    &lt;/div&gt;
                    &lt;br&gt;&lt;/br&gt;&lt;br&gt;&lt;/br&gt;

                &lt;/h:form&gt;

                &lt;h:form&gt;

                    &lt;h:commandButton action="#{chatbox.mmm()}" value="Send Message"/&gt;
                    &lt;/h:form&gt;
                    &lt;br&gt;&lt;/br&gt;&lt;br&gt;&lt;/br&gt;

                &lt;p:panelGrid columns="2" style="float: left; margin-left: 150px;"&gt;
                    &lt;h:outputText value="#{bundle.ViewUsersLabel_userid}"/&gt;
                    &lt;h:outputText value="#{usersController.selected.userid}" title="#{bundle.ViewUsersTitle_userid}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUsersLabel_username}"/&gt;
                    &lt;h:outputText value="#{usersController.selected.username}" title="#{bundle.ViewUsersTitle_username}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUsersLabel_password}"/&gt;
                    &lt;h:outputText value="#{usersController.selected.password}" title="#{bundle.ViewUsersTitle_password}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUsersLabel_email}"/&gt;
                    &lt;h:outputText value="#{usersController.selected.email}" title="#{bundle.ViewUsersTitle_email}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUsersLabel_lastname}"/&gt;
                    &lt;h:outputText value="#{usersController.selected.lastname}" title="#{bundle.ViewUsersTitle_lastname}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUsersLabel_firstname}"/&gt;
                    &lt;h:outputText value="#{usersController.selected.firstname}" title="#{bundle.ViewUsersTitle_firstname}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUsersLabel_dob}"/&gt;
                    &lt;h:outputText value="#{usersController.selected.dob}" title="#{bundle.ViewUsersTitle_dob}"&gt;
                        &lt;f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss" /&gt;
                    &lt;/h:outputText&gt;
                    &lt;h:outputText value="#{bundle.ViewUsersLabel_detailid}"/&gt;
                    &lt;h:outputText value="#{usersController.selected.detailid}" title="#{bundle.ViewUsersTitle_detailid}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUsersLabel_roleid}"/&gt;
                    &lt;h:outputText value="#{usersController.selected.roleid}" title="#{bundle.ViewUsersTitle_roleid}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUsersLabel_countryid}"/&gt;
                    &lt;h:outputText value="#{usersController.selected.countryid}" title="#{bundle.ViewUsersTitle_countryid}"/&gt;


                &lt;/p:panelGrid&gt;


                &lt;p:panelGrid columns="2" style="float: right; margin-right: 150px;"&gt;
                    &lt;h:outputText value="#{bundle.ViewUserdetailsLabel_detailid}"/&gt;
                    &lt;h:outputText value="#{usersController.ud.detailid}" title="#{bundle.ViewUserdetailsTitle_detailid}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUserdetailsLabel_orientation}"/&gt;
                    &lt;h:outputText value="#{usersController.ud.orientation}" title="#{bundle.ViewUserdetailsTitle_orientation}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUserdetailsLabel_ethnicity}"/&gt;
                    &lt;h:outputText value="#{usersController.ud.ethnicity}" title="#{bundle.ViewUserdetailsTitle_ethnicity}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUserdetailsLabel_height}"/&gt;
                    &lt;h:outputText value="#{usersController.ud.height}" title="#{bundle.ViewUserdetailsTitle_height}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUserdetailsLabel_weight}"/&gt;
                    &lt;h:outputText value="#{usersController.ud.weight}" title="#{bundle.ViewUserdetailsTitle_weight}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUserdetailsLabel_smoking}"/&gt;
                    &lt;h:outputText value="#{usersController.ud.smoking}" title="#{bundle.ViewUserdetailsTitle_smoking}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUserdetailsLabel_drinking}"/&gt;
                    &lt;h:outputText value="#{usersController.ud.drinking}" title="#{bundle.ViewUserdetailsTitle_drinking}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUserdetailsLabel_drugs}"/&gt;
                    &lt;h:outputText value="#{usersController.ud.drugs}" title="#{bundle.ViewUserdetailsTitle_drugs}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUserdetailsLabel_horoscope}"/&gt;
                    &lt;h:outputText value="#{usersController.ud.horoscope}" title="#{bundle.ViewUserdetailsTitle_horoscope}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUserdetailsLabel_education}"/&gt;
                    &lt;h:outputText value="#{usersController.ud.education}" title="#{bundle.ViewUserdetailsTitle_education}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUserdetailsLabel_languagespoken}"/&gt;
                    &lt;h:outputText value="#{usersController.ud.languagespoken}" title="#{bundle.ViewUserdetailsTitle_languagespoken}"/&gt;
                    &lt;h:outputText value="#{bundle.ViewUserdetailsLabel_gender}"/&gt;
                    &lt;h:outputText value="#{usersController.ud.gender}" title="#{bundle.ViewUserdetailsTitle_gender}"/&gt;
                &lt;/p:panelGrid&gt;




            &lt;/div&gt;
        &lt;/ui:insert&gt;
    &lt;/div&gt;

    &lt;div id="bottom"&gt;
        &lt;ui:insert name="bottom"&gt;Bottom&lt;/ui:insert&gt;
    &lt;/div&gt;
&lt;/h:body&gt;
</code></pre>

<p>Is the idea of using the java class from my jsf possible? If yes, how?
Thanks in advance</p>