<p>I have a remote jvm application running inside docker container managed by kubernetes:</p>

<pre><code>java -jar /path/to/app.jar
  -Dcom.sun.management.jmxremote  
  -Dcom.sun.management.jmxremote.authenticate=false
  -Dcom.sun.management.jmxremote.ssl=false
  -Dcom.sun.management.jmxremote.local.only=false
  -Dcom.sun.management.jmxremote.port=1099
  -Dcom.sun.management.jmxremote.rmi.port=1099
  -Djava.rmi.server.hostname=127.0.0.1 
</code></pre>

<p>When I try to debug using port forwarding and VisualVM, it works only when I use port 1099 on local machine. Ports 1098, 10900, or any other don't work. This one works for VisualVM: <code>kubectl port-forward &lt;pod-name&gt; 1099:1099</code>. <strong>This one doesn't:</strong> <code>kubectl port-forward &lt;pod-name&gt; 1098:1099</code></p>

<p>I use "Add JMX Connection" option in VisualVM, connecting to <code>localhost:1099</code> or <code>localhost:1098</code>. The former works, the latter doesn't.</p>

<p>Why can't I use non-1099 ports with VisualVM? </p>

<p><strong>UPD</strong>
I believe the issue is related to VisualVM, because port forwarding seems to work fine whatever local port I choose:</p>

<pre><code>$ kubectl port-forward &lt;pod&gt; 1098:1099
Forwarding from 127.0.0.1:1098 -&gt; 1099
Forwarding from [::1]:1098 -&gt; 1099
Handling connection for 1098
Handling connection for 1098
</code></pre>

<p>The full JMX URL for connecting to <code>localhost</code> is as follows:</p>

<pre><code>service:jmx:rmi://localhost:&lt;port1&gt;/jndi/rmi://localhost:&lt;port2&gt;/jmxrmi
</code></pre>

<p>...<a href="https://docs.oracle.com/javase/8/docs/technotes/guides/management/agent.html" rel="nofollow noreferrer">where</a> <code>&lt;port1&gt;</code> is the port number on which the RMIServer and RMIConnection remote objects are exported and <code>&lt;port2&gt;</code> is the port number of the RMI Registry.</p>

<p>For port <code>1098</code> you could try</p>

<pre><code>service:jmx:rmi://localhost:1098/jndi/rmi://localhost:1098/jmxrmi
</code></pre>

<p>I'd guess that both ports default to <code>1099</code> if not explicitly configured.</p>

<hr>

<p>EDIT: Per the comments, the JMX URL that worked was:</p>

<pre><code>service:jmx:rmi://localhost:1098/jndi/rmi://localhost:1099/jmxrmi
</code></pre>
