I found one way to do this, but it's a little cheesy.
First add the following helper class to the project:
// other imports import com.google.appengine.tools.development.DevAppServerMain; public class DevServer { public static void launch(final String[] args) { Logger logger = Logger.getLogger(""); logger.info("Launching AppEngine server..."); Thread server = new Thread() { @Override public void run() { try { DevAppServerMain.main(args); // run DevAppServer } catch (Exception e) { e.printStackTrace(); } } }; server.setDaemon(true); // shut down server when rest of app completes server.start(); // run server in separate thread URLConnection cxn; try { cxn = new URL("http://localhost:8888").openConnection(); } catch (IOException e) { return; } // should never happen boolean running = false; while (!running) { // maybe add timeout in case server fails to load try { cxn.connect(); // try to connect to server running = true; // Maybe limit rate with a Thread.sleep(...) here } catch (Exception e) {} } logger.info("Server running."); } }
Then add the following line to the record class:
public static void main(String[] args) { DevServer.launch(args);
Finally, create the appropriate launch configuration:
- Just click "Run As" β "Web Application". To create a default startup configuration.
- In the created launch configuration, in the "Home" -tab section, select your own recording class as "Main class" instead of the standard "com.google.appengine.tools.development.DevAppServerMain".
Now, if you run this launch configuration, it will first open the AppEngine server, and then continue with the rest of the main(...) method in the input class. Since the server thread is marked as a daemon thread, as soon as the second code in main(...) is executed, the application terminates, also closing the server.
Not sure if this is the most elegant solution, but it works. If anyone has a way to achieve this without the DevServer helper class, please post it!
In addition, there may be a more elegant way to check if the AppEngine server is working, in addition to pinging it using a URL connection, as I said above.
Note. The AppEngine Dev server registers its own URLStreamHandlerFactory to automatically map Http(s)URLConnections to the AppEngine URL-fetch framework. This means that you get errors complaining about the lack of ability to receive URL data if you use HttpURLConnections in your client code. Fortunately, this can be fixed in two ways, as described here: Getting the default Java link http httpStreamHandler .