1

I've been having trouble finding information on how I might go about hosting a .jar file with a simple Java server. Basically, I'm writing a 2D game in Java and I would like to create a launcher that, given internet access is present, will check for the latest version on launch and download the newest version if necessary. Now, I'm aware I could use a file sharing site, simply up the latest build and have the launcher download from there; I just happen to find the redundancy of hosting the file on a Java server that I will myself be running all the more satisfying.

So basically, my question is: how would I create a Java server that simply hosts the latest build's .jar file and when an incoming connection requests it, the server sends that entire file to the client PC?

Also, please omit any concerns regarding the possible inefficiency of downloading the entire game .jar, as the game will be extremely small. The only threat of an over-sized download I can foresee is with music; however, I could create a separate request for music files and only grab what is needed. That goes the same for graphical assets if they become an issue as well, but 16x16 textures don't strike me as having potential for getting in the way.

2
  • Ready to accept an anwer yet? Commented Dec 13, 2015 at 11:13
  • I was waiting for a reply to my last question, but yes I am. Commented Dec 14, 2015 at 1:37

1 Answer 1

1

If you are willing to obtain a code signing certificate, you could use WebStart

  1. Check Java WebStart - this covers all your needs regarding checking and downloading.

  2. Build a simple webapp that contains / hosts two things: Your .jnlp (Web Start descriptor file) and your game .jar

  3. Sign your jar using a valid code signing certificate (this is required for webstart)

  4. Deploy your webapp or jnlp/jar combination to whatever webserver you can get a hold of. As you're only uploading two files, there are a lot of "free" webhosting sites where you can upload some files.

If you really, really like to run the server for the downloads yourself, you should possibly check out Tomcat or Jetty or run Java SE HttpServer like this:

    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/game.jnlp", new FileHandler("game.jnlp"));
        server.createContext("/game.jar", new FileHandler("game.jar"));
        server.setExecutor(null); 
        server.start();
    }

    static class FileHandler implements HttpHandler {
        String file = "";
        public FileHandler(String string) {
            file = string;
        }
        @Override
        public void handle(HttpExchange t) throws IOException {
            byte[] data = IOUtils.toByteArray(getClass().getResourceAsStream(file));
            t.sendResponseHeaders(200, data.length);
            OutputStream os = t.getResponseBody();
            os.write(data);
            os.close();
        }
    }

If you believe Webstart is overkill, which I'd understand in your situation, you would need two jar files in the client: One to check and do the download of the other and then start the newly loaded file up. Sequence of steps would be:

  • Check if .jar is reachable by network.
  • If it is, load it and store it in defined path
  • Start a new java Process (-> ProcessBuilder) to run that java-program
  • Wait for game to finish (downloader will finish right after)

In that case your server would only need to host the game.jar

Sign up to request clarification or add additional context in comments.

8 Comments

Excuse my lacking knowledge of WebStart in general, but isn't that a way of running a .jar or web applet from a client connection? Or is it actually a download where even if the client PC had no internet connection, they could still play the last build?
Oh you can run webstart apps offline. But then again - your question implied this is a client/server game? So without network there will be no server?
Sorry, I should have specified this in my original post. The game is a 2D RPG game, so there is no networking in the actual game. The only networking I'm trying to do is downloading the latest build of the game so my friends can play/test for me without me having to manually hand out the jar every time I update the game. I'm looking for a way to download the latest build to a specified location on the client computer and be able to run it indefinitely after that regardless of internet connection.
Then my second option would be worth following up on - write the downloader yourself (URLConnection, IOUtils.copy()...) and start game when done or download failed with process builder
Thx. You now even have the power to upvote... if you get my drift ;-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.