'Deploy simple HTTP Maven (jax-rs) server jar on heroku

i'm having some troubles trying to deploy a VERY basic server on heroku, all that i need the server for is some testing on an android app i'm developing.

Here is the Server.java code:

public static void main(String args[]) throws Exception {

    JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
    factoryBean.setResourceClasses(PipBoyRepository.class);
    PipBoyRepository wr = new PipBoyRepository();
    wr.setConnection();

    factoryBean.setResourceProvider(new SingletonResourceProvider(wr));

    factoryBean.setAddress("https://pipboy2022.herokuapp.com:8080/");

    List<Object> providers = new ArrayList<Object>();
    providers.add(new JacksonJaxbJsonProvider());
    factoryBean.setProviders(providers);

    BindingFactoryManager manager = 
      factoryBean.getBus().getExtension(BindingFactoryManager.class);
    
    JAXRSBindingFactory restFactory = new JAXRSBindingFactory();
    restFactory.setBus(factoryBean.getBus());
    
    manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID,
                                     restFactory);
    
    org.apache.cxf.endpoint.Server server = factoryBean.create();

    System.out.println("Server ready...");

    while (true) {
    }
}

Here is my 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>it.sapienza.softeng</groupId>
    <artifactId>pipboy-webserver</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.28.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.10.3</version>
        </dependency>
        <dependency> 
            <groupId>org.apache.cxf</groupId> 
            <artifactId>cxf-rt-transports-http</artifactId> 
            <version>3.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.3.1</version>
          </dependency>


    </dependencies>
    <build>
        <plugins>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <version>1.6.0</version>
                <artifactId>exec-maven-plugin</artifactId>
                <configuration>
                    <mainClass>it.sapienza.softeng.api.withjson.and.dbms.Server</mainClass>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <!-- Optional Start -->
                            <finalName>${artifactId}-${version}</finalName>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
                            <!-- Optional End -->
    
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>it.sapienza.softeng.api.withjson.and.dbms.Server</mainClass>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                    <resource>META-INF/cxf/cxf-extension-xml.xml</resource>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/cxf/bus-extensions.txt</resource>
                                </transformer>
    
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                    <resource>META-INF/cxf/cxf-extension-http-jetty.xml</resource>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                    <resource>META-INF/cxf/cxf-extension-http.xml</resource>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                    <resource>META-INF/cxf/cxf-servlet.xml</resource>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
                                    <resource>META-INF/cxf/cxf.xml</resource>
                                </transformer>
    
                            </transformers>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
        <groupId>com.heroku.sdk</groupId>
        <artifactId>heroku-maven-plugin</artifactId>
        <version>3.0.3</version>
        <configuration>
            <appName>pipboy2022</appName>
            <includeTarget>false</includeTarget>
            <includes>
            <include>target/pipboy-webserver-1.0-jar-with-dependencies.jar</include>
            </includes>
            <processTypes>
                <web>java -jar target/pipboy-webserver-1.0-jar-with-dependencies.jar</web>
            </processTypes>

        </configuration>        
      </plugin>

        </plugins>
    </build>
</project>

When i try to deploy using:

mvn clean heroku:deploy

i get this error:

2022-01-25T14:21:12.000000+00:00 app[api]: Build succeeded
2022-01-25T14:21:12.231638+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2022-01-25T14:21:12.234638+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8
2022-01-25T14:21:12.765460+00:00 app[web.1]: Exception in thread "main" org.apache.cxf.service.factory.ServiceConstructionException
2022-01-25T14:21:12.765542+00:00 app[web.1]: at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:216)
2022-01-25T14:21:12.765568+00:00 app[web.1]: at it.sapienza.softeng.api.withjson.and.dbms.Server.main(Server.java:44)
2022-01-25T14:21:12.765628+00:00 app[web.1]: Caused by: java.lang.RuntimeException: Protocol mismatch for port 8080: engine's protocol is http, the url protocol is https
2022-01-25T14:21:12.765653+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.finalizeConfig(JettyHTTPDestination.java:168)
2022-01-25T14:21:12.765678+00:00 app[web.1]: at org.apache.cxf.transport.http.HTTPTransportFactory.getDestination(HTTPTransportFactory.java:288)
2022-01-25T14:21:12.765702+00:00 app[web.1]: at org.apache.cxf.endpoint.ServerImpl.initDestination(ServerImpl.java:85)
2022-01-25T14:21:12.765726+00:00 app[web.1]: at org.apache.cxf.endpoint.ServerImpl.<init>(ServerImpl.java:64)
2022-01-25T14:21:12.765750+00:00 app[web.1]: at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:170)
2022-01-25T14:21:12.765774+00:00 app[web.1]: ... 1 more
2022-01-25T14:21:12.765815+00:00 app[web.1]: Caused by: java.io.IOException: Protocol mismatch for port 8080: engine's protocol is http, the url protocol is https
2022-01-25T14:21:12.765854+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory.createJettyHTTPServerEngine(JettyHTTPServerEngineFactory.java:277)
2022-01-25T14:21:12.765879+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory.createJettyHTTPServerEngine(JettyHTTPServerEngineFactory.java:307)
2022-01-25T14:21:12.765913+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.retrieveEngine(JettyHTTPDestination.java:134)
2022-01-25T14:21:12.765926+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.finalizeConfig(JettyHTTPDestination.java:166)
2022-01-25T14:21:12.765946+00:00 app[web.1]: ... 5 more
2022-01-25T14:21:12.866903+00:00 heroku[web.1]: Process exited with status 1
2022-01-25T14:21:12.921103+00:00 heroku[web.1]: State changed from starting to crashed
2022-01-25T14:21:12.938923+00:00 heroku[web.1]: State changed from crashed to starting
2022-01-25T14:21:15.612557+00:00 heroku[web.1]: Starting process with command `java -jar target/pipboy-webserver-1.0-jar-with-dependencies.jar`
2022-01-25T14:21:16.699261+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2022-01-25T14:21:16.702666+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8
2022-01-25T14:21:17.489264+00:00 app[web.1]: Exception in thread "main" org.apache.cxf.service.factory.ServiceConstructionException
2022-01-25T14:21:17.489367+00:00 app[web.1]: at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:216)
2022-01-25T14:21:17.489393+00:00 app[web.1]: at it.sapienza.softeng.api.withjson.and.dbms.Server.main(Server.java:44)
2022-01-25T14:21:17.489458+00:00 app[web.1]: Caused by: java.lang.RuntimeException: Protocol mismatch for port 8080: engine's protocol is http, the url protocol is https
2022-01-25T14:21:17.489484+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.finalizeConfig(JettyHTTPDestination.java:168)
2022-01-25T14:21:17.489508+00:00 app[web.1]: at org.apache.cxf.transport.http.HTTPTransportFactory.getDestination(HTTPTransportFactory.java:288)
2022-01-25T14:21:17.489535+00:00 app[web.1]: at org.apache.cxf.endpoint.ServerImpl.initDestination(ServerImpl.java:85)
2022-01-25T14:21:17.489559+00:00 app[web.1]: at org.apache.cxf.endpoint.ServerImpl.<init>(ServerImpl.java:64)
2022-01-25T14:21:17.489583+00:00 app[web.1]: at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:170)
2022-01-25T14:21:17.489611+00:00 app[web.1]: ... 1 more
2022-01-25T14:21:17.489655+00:00 app[web.1]: Caused by: java.io.IOException: Protocol mismatch for port 8080: engine's protocol is http, the url protocol is https
2022-01-25T14:21:17.489704+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory.createJettyHTTPServerEngine(JettyHTTPServerEngineFactory.java:277)
2022-01-25T14:21:17.489730+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPServerEngineFactory.createJettyHTTPServerEngine(JettyHTTPServerEngineFactory.java:307)
2022-01-25T14:21:17.489757+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.retrieveEngine(JettyHTTPDestination.java:134)
2022-01-25T14:21:17.489782+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.finalizeConfig(JettyHTTPDestination.java:166)
2022-01-25T14:21:17.489804+00:00 app[web.1]: ... 5 more
2022-01-25T14:21:17.608228+00:00 heroku[web.1]: Process exited with status 1
2022-01-25T14:21:17.660646+00:00 heroku[web.1]: State changed from starting to crashed
2022-01-25T14:21:51.884018+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=pipboy2022.herokuapp.com request_id=ce41a171-a7e0-45dd-94dd-fa4a759c2616 fwd="79.45.1.62" dyno= connect= service= status=503 bytes= protocol=https
2022-01-25T14:21:52.006333+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=pipboy2022.herokuapp.com request_id=f32fe23f-74b3-43a3-84c2-0be324164589 fwd="79.45.1.62" dyno= connect= service= status=503 bytes= protocol=https
2022-01-25T14:21:58.047298+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/weapons" host=pipboy2022.herokuapp.com request_id=a3899f90-4d60-4351-ad79-31caac2e7505 fwd="79.45.1.62" dyno= connect= service= status=503 bytes= protocol=https
2022-01-25T14:21:58.102843+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/weapons" host=pipboy2022.herokuapp.com request_id=90b37f28-5467-4ed7-ab21-e44d4fb7020e fwd="79.45.1.62" dyno= connect= service= status=503 bytes= protocol=https
2022-01-25T14:21:58.385152+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=pipboy2022.herokuapp.com request_id=f9742e2d-7f5d-4f59-a310-4278422e5859 fwd="79.45.1.62" dyno= connect= service= status=503 bytes= protocol=https

after searching this issue online i found out many people reporting issues with cxf versions, but i tried those fixes with no result.

Edit:

After trying the Fix suggested by @Chris the error changed and now when i try to deploy i get this:

2022-01-25T19:08:39.000000+00:00 app[api]: Build succeeded
2022-01-25T19:08:41.505337+00:00 heroku[web.1]: Starting process with command `java -jar target/pipboy-webserver-1.0-jar-with-dependencies.jar`
2022-01-25T19:08:42.417781+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2022-01-25T19:08:42.421138+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8
2022-01-25T19:08:42.679385+00:00 app[web.1]: log4j:WARN No appenders could be found for logger (org.apache.cxf.common.logging.LogUtils).
2022-01-25T19:08:42.679788+00:00 app[web.1]: log4j:WARN Please initialize the log4j system properly.
2022-01-25T19:08:42.679789+00:00 app[web.1]: log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2022-01-25T19:08:43.338711+00:00 app[web.1]: Exception in thread "main" org.apache.cxf.service.factory.ServiceConstructionException
2022-01-25T19:08:43.338823+00:00 app[web.1]: at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:216)
2022-01-25T19:08:43.338858+00:00 app[web.1]: at it.sapienza.softeng.api.withjson.and.dbms.Server.main(Server.java:44)
2022-01-25T19:08:43.339000+00:00 app[web.1]: Caused by: org.apache.cxf.interceptor.Fault: Could not start Jetty server on port 36,202: Failed to bind to pipboy2022.herokuapp.com/54.220.192.176:36202
2022-01-25T19:08:43.339000+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPServerEngine.addServant(JettyHTTPServerEngine.java:485)
2022-01-25T19:08:43.339002+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.activate(JettyHTTPDestination.java:187)
2022-01-25T19:08:43.339036+00:00 app[web.1]: at org.apache.cxf.transport.AbstractObservable.setMessageObserver(AbstractObservable.java:53)
2022-01-25T19:08:43.339077+00:00 app[web.1]: at org.apache.cxf.binding.AbstractBindingFactory.addListener(AbstractBindingFactory.java:95)
2022-01-25T19:08:43.339110+00:00 app[web.1]: at org.apache.cxf.jaxrs.JAXRSBindingFactory.addListener(JAXRSBindingFactory.java:91)
2022-01-25T19:08:43.339141+00:00 app[web.1]: at org.apache.cxf.endpoint.ServerImpl.start(ServerImpl.java:128)
2022-01-25T19:08:43.339173+00:00 app[web.1]: at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:209)
2022-01-25T19:08:43.339209+00:00 app[web.1]: ... 1 more
2022-01-25T19:08:43.339291+00:00 app[web.1]: Caused by: java.io.IOException: Failed to bind to pipboy2022.herokuapp.com/54.220.192.176:36202
2022-01-25T19:08:43.339324+00:00 app[web.1]: at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:346)
2022-01-25T19:08:43.339355+00:00 app[web.1]: at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:307)
2022-01-25T19:08:43.339387+00:00 app[web.1]: at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
2022-01-25T19:08:43.339418+00:00 app[web.1]: at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:231)
2022-01-25T19:08:43.339466+00:00 app[web.1]: at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
2022-01-25T19:08:43.339499+00:00 app[web.1]: at org.eclipse.jetty.server.Server.doStart(Server.java:385)
2022-01-25T19:08:43.339530+00:00 app[web.1]: at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
2022-01-25T19:08:43.339563+00:00 app[web.1]: at org.apache.cxf.transport.http_jetty.JettyHTTPServerEngine.addServant(JettyHTTPServerEngine.java:474)
2022-01-25T19:08:43.339590+00:00 app[web.1]: ... 7 more
2022-01-25T19:08:43.339671+00:00 app[web.1]: Caused by: java.net.BindException: Cannot assign requested address
2022-01-25T19:08:43.339707+00:00 app[web.1]: at sun.nio.ch.Net.bind0(Native Method)
2022-01-25T19:08:43.339739+00:00 app[web.1]: at sun.nio.ch.Net.bind(Net.java:461)
2022-01-25T19:08:43.339772+00:00 app[web.1]: at sun.nio.ch.Net.bind(Net.java:453)
2022-01-25T19:08:43.339802+00:00 app[web.1]: at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:222)
2022-01-25T19:08:43.339832+00:00 app[web.1]: at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:85)
2022-01-25T19:08:43.339866+00:00 app[web.1]: at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:342)
2022-01-25T19:08:43.339894+00:00 app[web.1]: ... 14 more
2022-01-25T19:08:43.481104+00:00 heroku[web.1]: Process exited with status 1
2022-01-25T19:08:43.772520+00:00 heroku[web.1]: State changed from starting to crashed



Solution 1:[1]

factoryBean.setAddress("https://pipboy2022.herokuapp.com:8080/");

You don't get to pick your port on Heroku.

Your app will be assigned a port dynamically every time it starts via the PORT environment variable. I'm far from a Java expert, but I believe you can do something like this:

factoryBean.setAddress("https://pipboy2022.herokuapp.com:" + System.getenv("PORT"));

Note that this is just the port your app should listen on. To connect to it from a browser or from your Android app, simply omit the port. Heroku will route requests from the standard HTTPS and HTTP ports to your app.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris