2

I'm using Spring Social to retrieve data from twitter. This is my main class.

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    /*
     * SPRING BOOTSTRAP MAIN
     */
    public static void main(String[] args) {
        System.out.println("here");
        SpringApplication.run(Application.class, args);
    }

}

It runs fine when right click and run as-> java application. But when I call this main function from a GET function of servlet, it shows the following exception.

SEVERE: Servlet.service() for servlet [com.servlets.TwitterImport] in context with path [/ImportContacts] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at com.twitter.Application.main(Application.java:18)
    at com.servlets.TwitterImport.doGet(TwitterImport.java:35)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

My servlet code.

@WebServlet("/TwitterImport")
public class TwitterImport extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TwitterImport() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("In servlet");
        Application.main(null);
        System.out.println("App executed!");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-accessing-twitter</artifactId>
    <version>0.1.0</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.7.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-twitter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-linkedin</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.5</version>
        </dependency>
    </dependencies>

    <properties>
        <!-- use UTF-8 for everything -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <start-class>hello.Application</start-class>
        <!-- <properties> -->
        <!-- <start-class>org.springframework.boot.sample.tomcat.SampleTomcatApplication</start-class> -->
        <!-- </properties> -->
    </properties>

    <build>
        <plugins>
            <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

How can I resolve this? Thanks in advance.

Krishnalal P
  • 487
  • 4
  • 20
  • How do you call the main function from the get servlet? Your server probably didn't add the required spring dependencies on the class path. – Juru Oct 14 '14 at 08:25
  • Calling a that main function from the servlet works fine. Because "System.out.println("here");" is getting executed. – Krishnalal P Oct 14 '14 at 08:26
  • 1
    Of course it does, but when the classloader want to load that class it doesn't find it on the classpath. When you run it locally your ide adds the correct dependencies to the classpath, but on your server it does not have those dependencies on the classpath. Maybe read: http://stackoverflow.com/questions/2396493/what-is-a-classpath-confused-and-need-a-human-touch-to-understand/2396759#2396759 – Juru Oct 14 '14 at 08:28
  • I've all the dependencies in the server lib. Now it shows the error. 2014-10-14 14:50:30.828 INFO 12888 --- [bio-8080-exec-3] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/C:/Users/user/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/ImportContacts/WEB-INF/classes/] 2014-10-14 14:50:30.838 ERROR 12888 --- [bio-8080-exec-3] o.s.boot.SpringApplication : Application startup failed java.lang.NullPointerException: null – Krishnalal P Oct 14 '14 at 09:21
  • Seems to be a bug, can you check if its the same? https://github.com/spring-projects/spring-boot/issues/1364 – Juru Oct 14 '14 at 10:15
  • Yes. Almost same. I'm also getting the error. o.s.boot.SpringApplication : Application startup failed java.lang.NullPointerException: null – Krishnalal P Oct 14 '14 at 11:46
  • And the fix on that website, did you try it? – Juru Oct 14 '14 at 13:10
  • Yes. Changed my version to Spring Boot 1.1.4.RELEASE. But error still exists. – Krishnalal P Oct 15 '14 at 08:57

0 Answers0