Woohoojin
Woohoojin

Reputation: 724

How can I make it so Eclipse automatically updates my code in a window as I edit it?

How can I make it so Eclipse automatically updates my code in a window as I edit it? I've seen the feature before in youtube videos but I cannot find it. For example : I change a JApplet rectangle width from 20 to 10, I want to see it update immediately.

Upvotes: 1

Views: 680

Answers (1)

Bill K
Bill K

Reputation: 62759

I've seen Notch do this on development videos (Minecraft), it is awesome but I don't know exactly how he does it.

-- EDIT --

This has been bugging me so I went and googled "how does notch code" and found this on a blog page https://gun.io/blog/what-i-learned-from-watching-notch-code/. It doesn't say exactly how it was done but gives a good hint (HotSwap) and makes it seem like he set it up himself without external software. Here's the most relevant section:

Incredibly Fast Testing He began by building the engine, and to do this he used the ‘HotSwap’ functionality of the Java JVM 1.4.2, which continuously updates the running code when it detects that a class has changed. When building the engine, Notch wrote a function which would continuously pan the camera around and clip through the walls and keep the view on top, so he could make changes to the code and see the effects they made in real time. I’m used to testing by writing a function, building it, installing it on the device I’m testing on, and then seeing the result, which can take up to a minute at a time, so it’s easy to see how HotSwapping could save a lot of development time.

--- ORIGINAL POST CONTINUED ---

I get a similar effect by using groovysh though, works smoothly and can use all your java classes as is.

What I'll usually do is write all my code in java, then go and fire up "Groovysh" where it will give you a little window to enter commands (You may have to ensure the classpath works correctly outside of eclipse). I can then "new" any of my classes and call methods on them one line at a time. When you do myFrame.setSize([100,100]) you will see it change immediately.

A good test is to just run groovysh and type something like:

import javax.swing.*
f=new JFrame()
f.setVisible(true)
f.setSize(100,100)

or the groovier version:

f=new JFrame(visible:true, size:[100,100])

and you will see your frame resize on the screen. You can even drag it bigger and then do something like:

println f.getWidth()

to show your new width. It's fun to interact this way but it's more complicated if you want to actually change your class definition and see it pick up the change, I have no idea how Notch did that. I looked into it a little--it's possible he was using something like JRebel

It requires something special since you would have to dynamically reload the classfile into your running system on every save--something that should have serious classloader issues.

By the way there is also a way to get your Java program to throw out a little GroovyConsole which will allow you to inspect and modify all the variables in your running code (but again you can't replace definitions of existing classes).

Also see answer here: Change a method at runtime via a hot swap mechanism

Upvotes: 1

Related Questions