简体   繁体   English

Java Maven Heroku App 中的“java.io.IOException: java.net.BindException: 地址已在使用中”

[英]"java.io.IOException: java.net.BindException: Address already in use" in Java Maven Heroku App

I am trying to pull data from google sheets using their API and my code works from my local machine.我正在尝试使用他们的 API 从谷歌表格中提取数据,我的代码在我的本地机器上工作。 However, after deploying it in Heroku, I get this error.但是,在 Heroku 中部署后,出现此错误。 This is the part of the code that I suspected to fail when deployed in Heroku.这是我怀疑在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.这是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.提前致谢。

Address already in use -- the address here usually mean PORT . Address already in use use——这里的地址通常是指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.似乎应用程序已经在端口8080上的Heroku上运行,所以尝试更改端口并重新部署,你应该没问题。

I used -1 in setPort, this value automatically connects the application to an unused port.我在 setPort 中使用了 -1,这个值会自动将应用程序连接到一个未使用的端口。

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.net.BindException:无法分配请求的地址,java.io.IOException:打开的文件太多 - java.net.BindException: Cannot assign requested address, java.io.IOException: Too many open files Maven - java.net.BindException:地址已在使用中 - Maven - java.net.BindException: Address already in use java.net.BindException:地址已在使用中 - java.net.BindException: Address already in use Java Linux:java.net.BindException:地址已在使用中 - Java Linux: java.net.BindException: Address already in use Hadoop-java.net.BindException:地址已在使用中 - Hadoop - java.net.BindException: Address already in use 如何解决java.net.BindException:地址已在使用中 - How to solve java.net.BindException: Address already in use 线程“ main”中的异常java.net.BindException:地址已在使用中 - Exception in thread “main” java.net.BindException: Address already in use java.net.BindException:地址已在使用中:8080 - java.net.BindException: Address already in use:8080 java.net.BindException:地址已在使用中:JVM_Bind - java.net.BindException: Address already in use: JVM_Bind jenkins 更新错误:java.net.BindException:地址已在使用中 - jenkins update error: java.net.BindException: Address already in use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM