I have two web applications for the Play platform running on my system, on ports 9001 and 9002. I was wondering if it was possible to somehow restore the port where they started from my Java code.
Is it possible?
Yes. You can get the port as follows:
int port = Integer.parseInt(Play.configuration.getProperty("http.port", 9000));
Of course you need to import the play.Play class.
play.Play
In Play 2.4.x:
Play.application().configuration().getString("http.port");
or simply:
System.getProperty("http.port");
This only works in production mode when http.port is set via the Java -D parameter.
Since the system does not yet give me comments, I am forced to add a comment here.
This Carsten answer should be for play1, for play2, see Extracting the port number in the Play Framework 2 application