1

I am using IntelliJ, Maven, Spring-boot.

I am trying to set the port to 8001 using VM options(3rd tab(runner), uncheck the "use project settings") as given in many answers on stack overflow. These are the 2 options I have tried.

-Dmaven.tomcat.port=8001
-Dserver.port=8001

however the tomcat server starts on the default 8080 port.

However, if I state the port on application.properties :

server.port=8001

it works fine. How can I run it using VM options. I DO NOT want to give it programmatically or configure in the application.properties.

I have already tried : Spring Boot - how to configure port

and a few others.

Please note that I run using the debug/run buttons given on intelliJ

enter image description here

enter image description here

Console shows the command as :

/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:56655,suspend=y,server=n -Dmaven.multiModuleProjectDirectory=/Users/barora/gitHub/microservice/currency-exchange-service -Dserver.port=8001 "-Dmaven.home=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3" "-Dclassworlds.conf=/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/bin/m2.conf" -javaagent:/Users/barora/Library/Caches/IntelliJIdea2017.3/captureAgent/debugger-agent.jar=/private/var/folders/g5/6nfrnqnj2tz_z0q8h9ckhbqjwmtnv2/T/capture127.props -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.5.2.jar:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar" org.codehaus.classworlds.Launcher -Didea.version=2017.3.4 spring-boot:run

Where you can see -Dserver.port=8001

However it starts on 8080 :

2018-03-23 00:10:09.092  INFO 48261 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
Bhavya Arora
  • 768
  • 3
  • 16
  • 35
  • did you tried by adding port number in application.properties file ? – Udit Kumawat Mar 23 '18 at 06:10
  • the -D option is not mentioned in the docs. but --server.port=8001 is: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-use-short-command-line-arguments - could you try that? – wemu Mar 23 '18 at 06:23
  • @wemu , I did try, it gives me : Unrecognized option: --server.port=8001 Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. – Bhavya Arora Mar 23 '18 at 06:39
  • @UditKumawat Read my 5th, 6th and 7th line. – Bhavya Arora Mar 23 '18 at 06:39
  • @BhavyaArora Can you show the full line of the given command and the full output of the error... – khmarbaise Mar 23 '18 at 07:01
  • @khmarbaise there is no error, it starts on 8080. I have attached the screenshots. of the options that I give. – Bhavya Arora Mar 23 '18 at 07:20
  • I would suggest to try it on plain command to get rid of involved things...like IntelliJ etc. (Haven't you wrote about that a JVM could not be created? – khmarbaise Mar 23 '18 at 07:38
  • @khmarbaise that is in response to the comment to try using --server.port=8001 instead of -Dserver.port=8001. The full output of the error is already given in the comment following it. the change is as I am stating : swap -Dserver.port with --server.port. However jvm expects it to be of the format -D... so it gives the error. – Bhavya Arora Mar 23 '18 at 07:45
  • @khmarbaise I want to run it through intellij. java -jar -Dserver.port=8001 artifactId.jar starts it on 8001 port, but I want to run it through intellij. Which also brings up the question that why does mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8001' does not start it on port 8001. and if we have to how do I start it using mvn parameters. Which maybe will give more understanding why is intellij not able to start it on port 8001. – Bhavya Arora Mar 23 '18 at 08:04
  • are you sure you are starting the spring boot app? the screen shots suggest you run some maven goal. If maven spawn its own VM in the goal you run the properties are not passed along. You could just try the command line with the uber-jar created (java -jar uberjar.jar --server.port=8001). In IntelliJ you should also be able to simply run the main class directly not going through maven. – wemu Mar 23 '18 at 08:43
  • @wemu have you run the command mvn spring-boot:run ? see my comment above your comment. I can run it with that command. maven plugin includes the run goal. – Bhavya Arora Mar 23 '18 at 18:26
  • well as mentioned in the comments of the answer that was here. the docs of the plugin https://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html tell you the jvmArguments user property is "spring-boot.run.jvmArguments" not the one you are using. for convenience you could add the plugin to your pom and configure it there to avoid repeating the config for maven and the intellij run configuration – wemu Mar 23 '18 at 22:24
  • @wemu as I said in one of my earlier comments : mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8001' does not start it on port 8001 – Bhavya Arora Mar 25 '18 at 07:27
  • 1
    that does not work because you are using the wrong property. See for example here for an example: https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html - the maven plugin docs state: https://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html#jvmArguments - user property for jvmArguments for the run goal is "spring-boot.run.jvmArguments" - so on the command line using -D options: mvn spring-boot:run -Dspring-boot.run.jvmArguments="..." - your options are not picked up when using wrong parameter names. – wemu Mar 26 '18 at 07:21

2 Answers2

2

ok, this is an old question, but it was the first result I got in google. This is what I've done, based on the answer in https://stackoverflow.com/a/37215726/1922026:

instead of using -Dserver.port=8090 I used -Dspring-boot.run.jvmArguments='-Dserver.port=8090'.

Note that this argument only works when fork=true: https://github.com/spring-projects/spring-boot/issues/7588

1

I have had the same problem as you. Then I realized inside .idea/ folder, exists a file called workspace.xml and there is a tag how said:

<option name="VM_PARAMETERS" value="-Dserver.port=8081" />

Changing this option, server port will change (at least it works in my case).

I hope this will help someone.

GSAN
  • 648
  • 6
  • 29