Simple web server using java - Resource not found

890 views
Skip to first unread message

KumarM

unread,
Jan 20, 2014, 10:43:07 AM1/20/14
to ve...@googlegroups.com
Couldn't get this to work and I was incorrectly spending time on stackoverflow. 

I'm trying to write a simple webserver that'd server a html file from the local file system using vertx in java. For some reason the below code doesn't find the file although I have my web/index.html in the resources folder. I'm using IntelliJ and it copies over this folder to the classes folder that it generates for the project. If I give the absolute path it works as expected. What am I doing wrong or how can I figure out whether 'web' folder is a part of the classpath? Btw, I have tested by running this from IntelliJ as well as from the terminal using "mvn exec:java -Dexec.mainClass="test.vertx.VertxDriver" but get the same result - Resource not found.

package test.vertx;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vertx.java.core.*;
import org.vertx.java.core.http.HttpServer;
import org.vertx.java.core.http.HttpServerRequest;

import java.io.IOException;

public class VertxDriver {
    private static final Logger logger = LoggerFactory.getLogger(VertxDriver.class);

    public static void main(String[] args) {
        VertxDriver driver = new VertxDriver();

        Vertx vertx = VertxFactory.newVertx();
        HttpServer httpServer = vertx.createHttpServer();
        httpServer.requestHandler(driver.new FileRequestHandler(vertx));

        httpServer.listen(9999,"localhost");

        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class FileRequestHandler implements Handler<HttpServerRequest> {
        private Vertx vertx;

        FileRequestHandler(Vertx vertx) {this.vertx = vertx;}

        @Override
        public void handle(HttpServerRequest httpServerRequest) {
            String file = "";
            if(httpServerRequest.path().equals("/")) {
                file = "index.html";
            }
            logger.info("File being served is: "+file);
            httpServerRequest.response().sendFile("web/"+file);
        }
    }
}

Tim Fox

unread,
Jan 20, 2014, 11:29:14 AM1/20/14
to ve...@googlegroups.com
Why not display what the program thinks is it's current directory?

System.out.println(new File(".").getCanonicalPath());

One observation - any particular reason why you're embedding Vert.x rather than running as a Verticle?
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

KumarM

unread,
Jan 20, 2014, 5:24:27 PM1/20/14
to ve...@googlegroups.com
Thanks Tim. That helped - would be awesome if there's a way on the APIs to pass in a file or stream object - because this is in the resources folder but sounds like vertx isn't reading it from there (even though it's on the classpath). Or may be I'm wrong and there's a way - I'm just getting started. 

I didn't want to run 'vertx' from the command prompt and I wanted to be in control of how to kickstart the program, hence I took this approach of embedding it. To me, embedding is the best way to integrate vertx into bigger projects that I'm working on. I'm learning and I might be more comfortable as I spend more time with this. But overall from what I read - it's awesome. Thanks for this great work. 

Tim Fox

unread,
Jan 21, 2014, 2:54:12 AM1/21/14
to ve...@googlegroups.com
On 20/01/14 22:24, KumarM wrote:
Thanks Tim. That helped - would be awesome if there's a way on the APIs to pass in a file or stream object - because this is in the resources folder but sounds like vertx isn't reading it from there (even though it's on the classpath). Or may be I'm wrong and there's a way - I'm just getting started.

Did you try what I suggested? What was the result?



I didn't want to run 'vertx' from the command prompt and I wanted to be in control of how to kickstart the program, hence I took this approach of embedding it. To me, embedding is the best way to integrate vertx into bigger projects that I'm working on. I'm learning and I might be more comfortable as I spend more time with this. But overall from what I read - it's awesome. Thanks for this great work.

No problem.

But I usually caution against embedding - it's really only intended for power users, and there are extra gotchas that you won't get with the platform, and you'll lose all the platform features.
Reply all
Reply to author
Forward
0 new messages