简体   繁体   中英

Saving a source code while running in java

This is my own version of karel the Robot. The Janitor Robot that can only run un eclipse. My problem is when I open and i click run in a first time the robot moves according to the codes. but when i tried to edit the codes while the window is open, when i click the run button again it did not moves and need to close again.

This is my source code in the run method:

I have a janibot class that is implemented by a runnable and then. and i create its instance dynamically by this code.

Object tempJanibot = Class.forName(className).newInstance();
janibot = (Janibot) tempJanibot;
janibot.run()

where classname is the subclass of Janibot that is takingTurns in the screenshots.

It successfully created the instance of takingTurns class.

But I thought when i edit the code while the program is running and I click the run method the takingTurns class must be updated also but unfortunately it will not update and so I need to close again and click the run button.

在此处输入图片说明

What you are looking for is hot loading of the Java class. The default mechanism in Java is to load the class once in classloader reference and re-use it when required. So while Java application is running and if the class is recompiled, it will not pick up the new definition. If you wish to achieve similar functionality, you can look for solutions like JRebel or spring loaded .

"It's not a bug, it's a feature!" Eclipse cannot recompile your code while it is running and merge those changes into your currently running program. You'll have to restart the program each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM