2

I've been programming a simple webserver in Java, and though it works in Eclipse, I can't export it to a jar. I'm trying to package some files along with the binaries, and I know that I have to use InputStream to read from within the jar, but every time it seems to work, I access it from chrome and it says: "empty response". That probably means that the jar can't read itself, so I've decided to start over but don't really know how.

Here's the code for the problematic class:

    package handlers;

import java.io.*;
import com.sun.net.httpserver.*;

public final class Greeter implements HttpHandler {

    package handlers;

    import java.io.*;
    import com.sun.net.httpserver.*;

    public final class Greeter implements HttpHandler {

        static Headers h;
        static File file;
        static byte [] bytes;
        static FileInputStream fis;
        static BufferedInputStream bis;
        static OutputStream os;

        public void handle(HttpExchange t) throws IOException {

            h = t.getResponseHeaders();
            os = t.getResponseBody();
            file = new File("GreetingText.html");
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            bytes = new byte[(int) file.length()];

            h.add("Content-Type", "text/html");
            bis.read(bytes, 0, bytes.length);
            t.sendResponseHeaders(200, file.length());
            os.write(bytes, 0, bytes.length);
            os.close();

        }

    }

this is not a duplicate, since i've already tried the solutions given in other, similar posts. Note that a jar without the resources works perfectly, but with, it doesn't.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
DDetweiler
  • 35
  • 6
  • I'd recommend adding a manifest and accessing them as resources from the classpath. They aren't files per se; they aren't on the file system. – duffymo Jun 17 '15 at 23:48
  • What exactly have you tried from those other, similar posts? This has been answered so many times, I have trouble believing it doesn't work for you. Relevant answers are http://stackoverflow.com/questions/3369794/how-to-a-read-file-from-jar-in-java , http://stackoverflow.com/questions/2271926/how-to-read-a-file-from-a-jar-file , http://stackoverflow.com/questions/23001856/reading-a-text-file-from-a-jar , and http://stackoverflow.com/questions/9054880/java-how-to-package-and-access-resources-inside-a-runnable-jar-file . – VGR Jun 18 '15 at 00:24
  • Hadn't read the first one. Oops. – DDetweiler Jun 18 '15 at 01:17

1 Answers1

2

That's because the file is inside the JAR and is no longer accessible on the file system - using a FileInputStream is still using files.

You need to treat it as a resource instead and get an InputStream from that, then it will work both when packaged as a JAR and when not.

h = t.getResponseHeaders();
os = t.getResponseBody();

// Get an URL to the file
URL url = getClass().getResource("GreetingText.html");

// Open the stream and read the contents into a byte array
byte[] bytes;
try(InputStream in = url.openStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream()) {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) > 0) {
        bytes.write(buffer, 0, read);
    }
    bytes = out.toByteArray();
}

h.add("Content-Type", "text/html");
t.sendResponseHeaders(200, bytes.length);
os.write(bytes, 0, bytes.length);
os.close();

Place the file on your classpath, if it's in another package you need to prefix the path with /path/to/file, so if you put it in the directory html at the root of your classpath you will have to use "/html/GreetingText.html".

Raniz
  • 10,882
  • 1
  • 32
  • 64