javamaven-assembly-pluginbufferedinputstreamnanohttpd

Cannot read resource as stream when running jar


I'm using NanoHTTPD as my app server and everything is working fine from IDE. When I'm running the same code form jar (which was generated with maven-assembly-plugin), the resources are not loaded.

my ApServer code:

public class HintTestAppServer extends NanoHTTPD {

private HintTestAppServer() throws IOException {
    super(8000);
    start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
    System.out.println("Running on port " + super.getListeningPort() + "\n");
}

public static void main(String[] args) {
    try {
        new HintTestAppServer();
    } catch (IOException ioe) {
        System.err.println("Couldn't start server:\n" + ioe);
    }
}

@Override
public Response serve(IHTTPSession session) {

  //below line always return null fot the buffered input stream
    BufferedInputStream bufferedInputStream = (BufferedInputStream)
            Thread.currentThread().getContextClassLoader().getResourceAsStream("web/" + session.getUri());
    String mimeType;
    try {
        mimeType = URLConnection.guessContentTypeFromStream(bufferedInputStream);
    } catch (IOException e) {
        e.printStackTrace();
        return newFixedLengthResponse("ERROR");
    }

    try {
        return newFixedLengthResponse(Response.Status.OK, mimeType, bufferedInputStream, bufferedInputStream.available());
    } catch (IOException e) {
        e.printStackTrace();
        return newFixedLengthResponse("ERROR");
    }

}

}

my pom.xml assembly plugin:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>server.HintTestAppServer</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

The command I'm using to run the jar is:

java -cp target/hint-test-1.0.1-jar-with-dependencies.jar server.HintTestAppServer

I also tried to load the resource in the following way: (worked on IDE but not within jar)

BufferedInputStream bufferedInputStream = (BufferedInputStream)
getClass().getClassLoader().getResourceAsStream("web/" + session.getUri());

When I'm unarchiving the jar, I see the web folder with all my files there on the root folder.

please assist


Solution

  • Figured out the solution was:

    InputStream in = this.getClass().getClassLoader().getResourceAsStream("web" + session.getUri());