Consume . jar in java web project

Asked

Viewed 80 times

1

I created a file. jar (script.jar) that contains the "print()" method only returns a string and would like when I clicked a button on my Java Web application (Javaee) this "print()" method to be executed.

Method of script.jar file

package jardeteste.metodos;

public class Test{
     public static String imprimir(){
          return "Teste de Impressão";
     }
}

Bean method that receives and manipulates the click (checkBoxView.java)

package projetojavaweb.bean;

import jardeteste.metodos;

@ManagedBean
public class CheckboxView {

        public static void executar(ActionEvent event) {
                String recebedor = Teste.imprimir();
                System.out.println(recebedor);
        }

}

View if necessary: (testList.xhtml)

 <p:commandButton value="Printar" actionListener="#{checkboxView.executar}" icon="ui-icon-check"/>

Error that generates in the log:

WARNING: /pages/testList.xhtml @20,124 actionListener="#{checkboxView.listarTestes}": java.lang.NoClassDefFoundError: jardeteste.metodos
    javax.el.ELException: /pages/testList.xhtml @20,124 actionListener="#{checkboxView.listarTestes}": java.lang.NoClassDefFoundError: jardeteste/metodos
        at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
        at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:147)
        at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
        at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
        at javax.faces.component.UICommand.broadcast(UICommand.java:300)
        at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
        at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1368)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Unknown Source)
  • You must import your java class into your classpath or manage its dependency directly from Maven.

1 answer

0

You must define a class or import the entire package in import:

import jardeteste.metodos.*;

or

import jardeteste.metodos.Teste;

You can also directly import static components from the Test class:

import static jardeteste.metodos.Teste.*;

This way you don’t need to place the class during the method call:

String recebedor = imprimir();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.