Home > OS >  "java.io.IOException: java.net.BindException: Address already in use" in Java Maven Heroku
"java.io.IOException: java.net.BindException: Address already in use" in Java Maven Heroku

Time:03-19

I am trying to pull data from google sheets using their API and my code works from my local machine. However, after deploying it in Heroku, I get this error. This is the part of the code that I suspected to fail when deployed in Heroku.

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        InputStream in = ProcessGoogleSheets.class.getResourceAsStream(CREDENTIALS_FINAL_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: "   CREDENTIALS_FINAL_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8080).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }

This is the log returned by Heroku.

2022-03-18T06:12:02.088566 00:00 app[web.1]: Mar 18, 2022 6:12:02 AM org.apache.catalina.core.StandardWrapperValve invoke
2022-03-18T06:12:02.088574 00:00 app[web.1]: SEVERE: Servlet.service() for servlet [GetTrackOrderData] in context with path [] threw exception
2022-03-18T06:12:02.088574 00:00 app[web.1]: java.io.IOException: java.net.BindException: Address already in use
2022-03-18T06:12:02.088575 00:00 app[web.1]: at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:127)
2022-03-18T06:12:02.088576 00:00 app[web.1]: at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:121)
2022-03-18T06:12:02.088576 00:00 app[web.1]: at com.servlets.ProcessGoogleSheets.getCredentials(ProcessGoogleSheets.java:46)
2022-03-18T06:12:02.088577 00:00 app[web.1]: at com.servlets.ProcessGoogleSheets.getData(ProcessGoogleSheets.java:51)
2022-03-18T06:12:02.088577 00:00 app[web.1]: at com.servlets.GetTrackOrderData.processRequest(GetTrackOrderData.java:20)
2022-03-18T06:12:02.088578 00:00 app[web.1]: at com.servlets.GetTrackOrderData.doPost(GetTrackOrderData.java:68)
2022-03-18T06:12:02.088578 00:00 app[web.1]: at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
2022-03-18T06:12:02.088578 00:00 app[web.1]: at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
2022-03-18T06:12:02.088579 00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
2022-03-18T06:12:02.088579 00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
2022-03-18T06:12:02.088579 00:00 app[web.1]: at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
2022-03-18T06:12:02.088580 00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
2022-03-18T06:12:02.088580 00:00 app[web.1]: at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
2022-03-18T06:12:02.088580 00:00 app[web.1]: at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
2022-03-18T06:12:02.088581 00:00 app[web.1]: at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
2022-03-18T06:12:02.088581 00:00 app[web.1]: at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
2022-03-18T06:12:02.088581 00:00 app[web.1]: at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
2022-03-18T06:12:02.088581 00:00 app[web.1]: at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
2022-03-18T06:12:02.088582 00:00 app[web.1]: at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
2022-03-18T06:12:02.088582 00:00 app[web.1]: at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
2022-03-18T06:12:02.088582 00:00 app[web.1]: at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
2022-03-18T06:12:02.088582 00:00 app[web.1]: at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
2022-03-18T06:12:02.088583 00:00 app[web.1]: at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
2022-03-18T06:12:02.088583 00:00 app[web.1]: at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598)
2022-03-18T06:12:02.088583 00:00 app[web.1]: at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
2022-03-18T06:12:02.088584 00:00 app[web.1]: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
2022-03-18T06:12:02.088584 00:00 app[web.1]: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
2022-03-18T06:12:02.088584 00:00 app[web.1]: at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
2022-03-18T06:12:02.088585 00:00 app[web.1]: at java.lang.Thread.run(Thread.java:750)
2022-03-18T06:12:02.088586 00:00 app[web.1]: Caused by: java.net.BindException: Address already in use
2022-03-18T06:12:02.088587 00:00 app[web.1]: at sun.nio.ch.Net.bind0(Native Method)
2022-03-18T06:12:02.088587 00:00 app[web.1]: at sun.nio.ch.Net.bind(Net.java:461)
2022-03-18T06:12:02.088587 00:00 app[web.1]: at sun.nio.ch.Net.bind(Net.java:453)
2022-03-18T06:12:02.088587 00:00 app[web.1]: at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:222)
2022-03-18T06:12:02.088588 00:00 app[web.1]: at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:85)
2022-03-18T06:12:02.088588 00:00 app[web.1]: at org.eclipse.jetty.server.nio.SelectChannelConnector.open(SelectChannelConnector.java:187)
2022-03-18T06:12:02.088588 00:00 app[web.1]: at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:316)
2022-03-18T06:12:02.088589 00:00 app[web.1]: at org.eclipse.jetty.server.nio.SelectChannelConnector.doStart(SelectChannelConnector.java:265)
2022-03-18T06:12:02.088589 00:00 app[web.1]: at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
2022-03-18T06:12:02.088589 00:00 app[web.1]: at org.eclipse.jetty.server.Server.doStart(Server.java:293)
2022-03-18T06:12:02.088589 00:00 app[web.1]: at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
2022-03-18T06:12:02.088590 00:00 app[web.1]: at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:123)
2022-03-18T06:12:02.088591 00:00 app[web.1]: ... 28 more
2022-03-18T06:12:02.088591 00:00 app[web.1]:

Any ideas to fix this? Thanks in advance.

CodePudding user response:

Address already in use -- the address here usually mean PORT.

It seems an application is already running on Heroku on port 8080, so try changing the port and redeploy and you should be fine.

CodePudding user response:

I used -1 in setPort, this value automatically connects the application to an unused port.

This is the code that works now.

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
    InputStream in = ProcessGoogleSheets.class.getResourceAsStream(CREDENTIALS_FINAL_PATH);
    if (in == null) {
        throw new FileNotFoundException("Resource not found: "   CREDENTIALS_FINAL_PATH);
    }
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
            .setAccessType("offline")
            .build();
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(-1).build();
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
  • Related