210

How can I change the default port used by the play framework in development mode when issueing the "run" command on the play console.

This is for playframework 2.0 beta.

Using the http.port configuration parameter either on the command line or in the application.conf seems to have no effect:

C:\dev\prototype\activiti-preso>play run --http.port=8080
[info] Loading project definition from C:\dev\prototype\activiti-preso\project
[info] Set current project to activiti-preso (in build file:/C:/dev/prototype/activiti-preso/)


Windows, really? Ok, disabling colors.

--- (Running the application from SBT, auto-reloading is enabled) ---

[error] org.jboss.netty.channel.ChannelException: Failed to bind to: 0.0.0.0/0.0.0.0:9000
[error] Use 'last' for the full log.
Boris Terzic
  • 10,482
  • 8
  • 42
  • 58
  • 3
    @PereVillega it's hilarious because it's true: https://github.com/playframework/playframework/blob/9206bea8c9c88acdc6786ebb2554f081396e8f6a/framework/src/play/src/main/scala/play/utils/Colors.scala#L14 – Gregory Kalabin Oct 03 '13 at 14:05
  • 1
    After I saw the "Windows, really?" I had to ask my Windows using colleague to check if it still does that. We are using 2.2.0, and it no longer displays that message, but it does disable colors. I suspect that it was only in the beta version. – Eric Wilson Nov 22 '13 at 16:58
  • put PlayKeys.devSettings := Seq("play.server.http.port" -> "9001") in build.sbt – Rajat Dec 18 '18 at 08:57
  • https://www.playframework.com/documentation/2.5.x/ConfigFile#Using-with-the-run-command – Rajat Dec 18 '18 at 09:00

23 Answers23

412

Play 2.x

In Play 2, these are implemented with an sbt plugin, so the following instructions are really just sbt tasks. You can use any sbt runner (e In Play 2, these are implemented with an sbt plugin, so the following are really just sbt tasks. You can use any sbt runner (e.g. sbt, play, or activator). Below the sbt runner is used, but you can substitute it for your sbt runner of choice.

Play 2.x - Dev Mode

For browser-reload mode:

sbt "run 8080"

For continuous-reload mode:

sbt "~run 8080"

Play 2.x - Debug Mode

To run in debug mode with the http listener on port 8080, run:

sbt -jvm-debug 9999 "run 8080"

Play 2.x - Prod Mode

Start in Prod mode:

sbt "start -Dhttp.port=8080"

Play 2.x - Staged Distribution

Create a staged distribution:

sbt stage

For Play 2.0.x and 2.1.x use the target/start script (Unix Only):

target/start -Dhttp.port=8080

For Play 2.2.x & 2.3.x use the appropriate start script in the target/universal/stage/bin directory:

target/universal/stage/bin/[appname] -Dhttp.port=8080

With Play 2.2.x & 2.3.x on Windows:

target\universal\stage\bin\[appname].bat -Dhttp.port=8080

Play 2.x - Zip Distribution

To create a zip distribution:

sbt dist

For Play 2.0.x and 2.1.x use the start script (Unix Only) in the extracted zip:

start -Dhttp.port=8080

For Play 2.2.x use the appropriate script in the [appname]-[version]/bin directory:

[appname]-[version]/bin/[appname] -Dhttp.port=8080

With Play 2.2.x on Windows:

[appname]-[version]\bin\[appname].bat -Dhttp.port=8080

Play 1.x

Change the http.port value in the conf/application.conf file or pass it command line:

play run --http.port=8080
Dale Wijnand
  • 5,854
  • 4
  • 25
  • 53
James Ward
  • 28,966
  • 9
  • 47
  • 79
  • Interestingly enough that doesn't actually work. But perhaps that is just a bug or an issue with my system. – Boris Terzic Nov 21 '11 at 07:37
  • 1
    I believe right now this is only for 1.x, not yet implemented in 2.0 beta – Pere Villega Nov 21 '11 at 08:31
  • Whoops. Sorry. I missed the 2.0 part. – James Ward Nov 21 '11 at 12:21
  • Added instructions for Play 2. – James Ward Nov 21 '11 at 23:45
  • 18
    I'm running Play 2.0. When i try `play run 8080` it still runs on 9000. I'm on a Mac. What gives? **EDIT:** `play "run 8080"` works! – Jay Q. Mar 27 '12 at 07:11
  • is console play > run portnumber start Application started .. Nice. – yasaricli Dec 17 '12 at 10:30
  • is it possible to set the port by a config file? I've tried http.port=9001 in conf/application.conf and -Dhttp.port=9001 in .sbtopts, but both didn't work. – Stefan K. Jan 06 '14 at 20:55
  • 6
    @StefanK. You can't put the port in a Play config file because the config isn't read until after Play is listening on the port. You can use an env var and set the port to that. – James Ward Jan 06 '14 at 22:00
  • Is it possible that The generated .bat can't receive arguments? I've put myattr=default in the application.conf but when I start it with bin\application.bat -Dmyattr=other it doesn't work. In Linux running bin/application -Dmyattr=other works fine. Reading the generated .bat I've found this: `rem TODO - figure out how to pass arguments....` `"%_JAVACMD%" %_JAVA_OPTS% %FREEMARKER_BUILDER_OPTS% -cp "%APP_CLASSPATH%" %APP_MAIN_CLASS% %CMDS%` I'm running play 2.2.1 built with Scala 2.10.2 (running Java 1.7.0_51) – gfournier Feb 05 '14 at 19:58
  • @gfournier There could be a bug there. Can you file it? https://github.com/sbt/sbt-native-packager/issues – James Ward Feb 05 '14 at 21:07
  • 1
    @James I've submitted the following issue with a workaround: https://github.com/sbt/sbt-native-packager/issues/155. Thanks! – gfournier Feb 05 '14 at 22:45
  • Dem darn quotes! Gets me everytime, grr! Thanks! – GreenAsJade Mar 18 '16 at 10:43
  • For running Play in Intellij: Run > Edit Configurations > (your play cfg) > Environment variables > add `"http.port"` with value `` – Aaron Dec 12 '16 at 08:41
33

Play 2.0-RC4

It is important to include quotes around the play command you want to run. In my case without the quotes play would still run on port 9000.

play "run 8080"

Alternatively you could run the following from the play console (type 'play' to get to the console)

run 8080
Jonathan Dixon
  • 2,308
  • 21
  • 27
16

Hope this helps someone.

via sbt settings:

...
.settings(PlayKeys.playDefaultPort := 8855)
...
Armin
  • 1,259
  • 1
  • 11
  • 16
  • 1
    This is the correct answer! Thank you! (For the other SBT and Play novices like me, add the `.settings` line to your `build.sbt` file, after `lazy val root = (project in file("."))`. For more details on SBT settings, see here: https://www.scala-sbt.org/1.0/docs/Custom-Settings.html ) – Cameron Hudson Mar 27 '19 at 20:22
  • This is the only solution I've found that works in play 2.7.x – Coert Grobbelaar Aug 20 '19 at 14:38
7

Version 2.0.3 :

  • Go to the project directory and just say play (and nothing after that). That will open the play console.

  • Next, say run 8080. That will start play on port 8080.

I hope this helps.

Stephan
  • 37,597
  • 55
  • 216
  • 310
5

For Play 2.2.x on Windows with a distributable tar file I created a file in the distributable root directory called: {PROJECT_NAME}_config.txt and added:

-Dhttp.port=8080

Where {PROJECT_NAME} should be replaced with the name of your project. Then started the {PROJECT_NAME}.bat script as usual in the bin\ directory.

Wextux
  • 752
  • 9
  • 17
  • in windows (using dist and zip options), passing command line arguments doesn't work (play 2.2.0). But providing the same arguments in config file works like a charm. – skywalker Mar 31 '14 at 11:20
5

Play 2.2.0 on Windows

Using a zip distribution (produced using the "dist" command), the only way I was able to change the startup port was by first setting JAVA_OPTS and then launching the application.

E.g., from the command line

set JAVA_OPTS=-Dhttp.port=9002
bin\myapp.bat

where myapp.bat is the batch file created by the "dist" command.

The following would always ignore my http.port parameter and attempt to start on the default port, 9000

bin\myapp.bat -Dhttp.port=9002

However, I've noticed that this works fine on Linux/OSX, starting up on the requested port:

./bin/myapp -Dhttp.port=9002
Jason Bass
  • 51
  • 1
  • 3
5

For Play 2.3.x

activator "run -Dhttp.port=9001"

sumitarora
  • 570
  • 4
  • 17
4

You can also set the HTTP port in .sbtopts in the project directory:

-Dhttp.port=9001

Then you do not have to remember to add it to the run task every time.

Tested with Play 2.1.1.

lachdrache
  • 151
  • 1
  • 5
4

Just add the following line in your build.sbt

PlayKeys.devSettings := Seq("play.server.http.port" -> "8080")

Nitin Aggarwal
  • 201
  • 1
  • 2
  • 10
3

for Play 2.5.x and Play 2.6.x

sbt "-Dhttp.port=9002"

then

run
pme
  • 11,442
  • 2
  • 35
  • 62
Mesut Yiğit
  • 609
  • 8
  • 20
2

On Windows maybe the play "run 9001" will not work. You have to change the play.bat file. See Ticket

Paddy
  • 61
  • 3
  • It might be valuable to quickly indicate how to change the play.bat file here, in case that link ever breaks (and to more quickly help someone looking for assistance on SO). – Taylor R Nov 06 '18 at 19:19
2

From the play console, you just need to type run 8888, if you want to run it from port 8888.

play> run 8888
Siddhu
  • 377
  • 1
  • 4
  • 7
2

for play 2.5.x

Step 1: Stop the netty server (if it is running) using control + D

Step 2: go to sbt-dist/conf

Step 3: edit this file 'sbtConfig.txt' with this

-Dhttp.port=9005

Step 4: Start the server

Step 5: http://host:9005/

bobby
  • 21
  • 4
2

Specify Port in Development

By default, SBT runs the application on port 9000:

sbt run

To specify a port add -Dhttp.port flag, for example:

sbt run -Dhttp.port=8080

Using the -Dhttp.port flag, you can debug multiple applications on your development machine. Please note, you can also use the -Dhttp.port flag in test and production environments.

LineDrop
  • 181
  • 1
  • 5
2

I noticed no one has mentioned achieving this through environment variables (CI/CD friendly).

export PLAY_HTTP_PORT=1234
export PLAY_HTTPS_PORT=1235

Once set, Play will read from those environment variables to determine the port when doing sbt run, sbt start, or when running the executable for prod deployment. See the docs for more.

Wildhammer
  • 1,246
  • 1
  • 16
  • 19
  • Thank you! I tried every other suggestion before getting to yours and none of them worked. Yours did :) – Phasmid Nov 12 '20 at 22:05
2

With the commit introduced today (Nov 25), you can now specify a port number right after the run or start sbt commands.

For instance

play run 8080 or play start 8080

Play defaults to port 9000

Olivier Refalo
  • 46,213
  • 20
  • 84
  • 114
  • Thanks for the up to the minute updates! I updated James' answer to add your info, perhaps we can build a canonical answer here. – Boris Terzic Nov 29 '11 at 10:44
1

Play 2.2.1 on Windows supports a PLAY_OPTS environment variable. Play's play.bat file contains this line:

java -Dsbt.ivy.home="%~dp0repository" -Dplay.home="%~dp0framework" -Dsbt.boot.properties="%fp%framework/sbt/play.boot.properties" %PLAY_OPTS% -jar "%~dp0framework\sbt\sbt-launch.jar" %*

so to run on port 9002, do

set PLAY_OPTS=-Dhttp.port=9002
play run
IanRae
  • 283
  • 1
  • 2
1

Tested with 2.3.7 Play framework. Works well.

./{application}/bin/{executable} -Dhttp.port=5000
Venkatesh
  • 2,788
  • 7
  • 25
  • 34
1

I did this. sudo is necessary.

$ sudo play debug -Dhttp.port=80
...
[MyPlayApp] $ run

EDIT: I had problems because of using sudo so take care. Finally I cleaned up the project and I haven't used that trick anymore.

Ferran Maylinch
  • 9,556
  • 13
  • 70
  • 89
1

We are using Play version 2.5.6.

For changing the port, go to your project root folder and hit: activator "run 8008" in command prompt / terminal.

and that's it.

Swapnil Kadu
  • 267
  • 1
  • 5
  • 11
0

On windows, I use a start.bat file like this:

java -Dhttp.port=9001 -DapplyEvolutions.default=true -cp "./lib/*;" play.core.server.NettyServer "."

The -DapplyEvolutions.default=true tells evolution to automatically apply evolutions without asking for confirmation. Use with caution on production environment, of course...

opensas
  • 52,870
  • 69
  • 227
  • 340
0

We cannot change the application port from the avtivator but can change from the command line activator "~run 8080"

But to run on the poet 9000 from the activator we need to stop the application which is using this port. We can use the this application to find this and end the process https://technet.microsoft.com/en-in/sysinternals/bb897437.aspx

After this we can run and it will be successful.

Avinav Mishra
  • 532
  • 7
  • 11
0

You can set it, with other options, in a .jvmopts file inside the project root directory:

-Dhttp.port=9100

You can also add other options, like loading a different config file with

-Dconfig.file=<config_file_absolute_path>

After you set your .jvmopts file you don't have to remember to add some parameters to the command line, but just do:

sbt run
Atropo
  • 11,252
  • 4
  • 43
  • 57