0

I have developed a small java application using Jersey + Jetty in Java 11. This works perfectly when running it in intelliJ but doesn't work from the Shadowjar. When I go to localhost:8080/hello no response or error.

When I print server object, I'm getting null during Shadowjar run.

What am I doing wrong??

package com.jettypoc;

import org.eclipse.jetty.server.Server;
import org.glassfish.jersey.internal.util.JdkVersion;
import org.glassfish.jersey.jetty.JettyHttpContainerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

import java.net.URI;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainApp {

    public static final String BASE_URI = "http://localhost:8080/";

    public static Server startServer() {
        Server server = null;
    try {
        //WeldContainer container = weld.initialize();
        // scan packages
        final ResourceConfig config = new ResourceConfig().packages("com.jettypoc");

        //final ResourceConfig config = new ResourceConfig(MyResource.class);
        System.out.println("JDK Version: " + JdkVersion.getJdkVersion().getMajor());
        server =
                JettyHttpContainerFactory.createServer(URI.create(BASE_URI), config);
        System.out.println("Server successfully initialized, waiting for start.");
    } catch (Exception e) {
        System.out.println("Exception " +e);
    }
    return server;

    }

    public static void main(String[] args) {

        try {

            final Server server = startServer();
            if(Objects.isNull(server)) {
                System.out.println("Server null");
            } else {
                System.out.println("Server Non null");
                server.setDumpAfterStart(true);
            }

            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                try {
                    System.out.println("Shutting down the application...");
                    server.stop();
                    System.out.println("Done, exit.");
                } catch (Exception e) {
                    Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, e);
                }
            }));

            System.out.println(String.format("Application started.%nStop the application using CTRL+C"));

            // block and wait shut down signal, like CTRL+C
            Thread.currentThread().join();

            // alternative
            // Thread.sleep(Long.MAX_VALUE);       // sleep forever...
            // Thread.sleep(Integer.MAX_VALUE);    // sleep around 60+ years

        } catch (InterruptedException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

plugins {
    id 'java'
    id 'com.github.johnrengelman.shadow' version '5.2.0'
}

group 'com.jettypoc'
version '1.0-SNAPSHOT'

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    implementation 'org.glassfish.jersey.containers:jersey-container-jetty-http:3.0.2'
    implementation 'org.glassfish.jersey.inject:jersey-hk2:3.0.2'
    implementation 'jakarta.servlet:jakarta.servlet-api:5.0.0'
    implementation 'org.glassfish.jersey.media:jersey-media-json-jackson:3.0.2'
    implementation 'jakarta.activation:jakarta.activation-api:2.0.1'
    implementation group: 'log4j', name: 'log4j', version: '1.2.15'
}

test {
    useJUnitPlatform()
}
Mohan
  • 351
  • 1
  • 2
  • 4
  • 1
    There's no configuration for your shadow jar in your gradle build. (I would have expected at least a configured mergeServiceFiles entry) – Joakim Erdfelt Mar 14 '22 at 17:32
  • When building a uber jar, without the merging of service files, the service loader can't load the required providers – Paul Samsotha Mar 14 '22 at 17:54
  • 1
    What exactly is the error? The server is null because there was an exception and the server never got initialized. Also you kinda just swallow the exception. It doesn't really makes sense to continue if there is an exception creating the server. – Paul Samsotha Mar 14 '22 at 22:37
  • https://stackoverflow.com/q/53751401/2587435 – Paul Samsotha Mar 14 '22 at 22:41
  • After updating Jersey version to 2.35 it worked. – Mohan Mar 15 '22 at 14:23

0 Answers0