1

I'm working on a Swing based app and made a change to to the source. I would have thought that the changed code be recompiled by the JVM but this did not occur, should it have ?

Comping from a webapps background when change java source file the JVM recognizes the change and is recompiled within the Tomcat container, so I don't need to reload by classpath (by restarting the Tomcat server). If the change is significant then this does not always work.

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • 2
    That depends on the tools you are using (the IDE, for example). The JVM itself does not automatically recompile source code for you. – Jesper Feb 15 '16 at 15:16
  • nope, you'll have to re-start the app – Marius Manastireanu Feb 15 '16 at 15:19
  • See also [*How do I programmatically compile and instantiate a Java class?*](http://stackoverflow.com/q/2946338/230513), [*et al*](http://stackoverflow.com/search?tab=votes&q=%5bjava%5d%20javax.tools). – trashgod Feb 15 '16 at 17:20

1 Answers1

1

I made a change to to the source. I would have thought that the changed code be recompiled by the JVM but this did not occur. Should it have ?

No. Assuming that you are using the standard Java tool-chain, your source code is only compiled when you run the javac. Certainly, the JVM (i.e. java) neither compiles source code, or notices when the source code changes. Indeed, it won't even notice if ".class" files have changed1. On the other hand, if you are using an IDE to develop / compile / run, then it (not the JVM) will typically notice when the source code changes and automatically recompile it. But if you happened to be running at the same time as you are changing your application, the changes typically won't affect the running program2.


1 - It is possible to code your application so that it notices changes and attempts to reload recompiled classes, but there are a bunch of issues and potential traps in doing that. (Loading a class multiple times gives you a different type each time ... as far as the runtime type system is concerned. It gets really awkward unless you can reload the entire application.)

2 - Some IDEs (e.g. Eclipse) notice when you make a change to a class that is being used by a class that you are debugging, and offer you the choice of relaunching the application being debugged.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216