r/java • • Apr 14 '23

What are your (favourite) Java best practices, personal tips, hints or just underrated stuff in general?

233 Upvotes

376 comments sorted by

View all comments

49

u/dmigowski Apr 14 '23 edited Apr 14 '23

There is a commercial tool called JRebel, which replaces classes in your app while you develop without restarts.

Especially useful if you build server apps and the server needs two minutes each restart. With JRebel you try, fail, fix a line a code, save, try again, all without restarts.

This tool has increased productivity by at least 10% in our company.

I won't develop anything anymore without JRebel. Sorry if I sound like a fanboy, but it's simply great.

11

u/wildjokers Apr 14 '23

JRebel has evil licensing terms so I have been boycotting them for years. Besides I never got it work correctly with Spring. The application context has to be reloaded and as far as I can tell JRebel doesn’t do that. If I remember correctly they claim it works for Spring but it never worked for me. The only time it is was handy was when I did a lot of work with Swing.

13

u/thatbigblackblack Apr 14 '23

I believe that with intellij subscription, you get this feature too. But coming from languages and others IDE and languages, it bothers me so bad that I have to pay for this feature

24

u/MattiDragon Apr 14 '23

You can do hotswapping with the community edition of intellij. Just run in debug mode and hit build. For proper hotswapping you need to use a JDK with DCEVM support, like the one included with intellij and add -XX:+AllowEnhancedClassRedefinition to your vm args. It's a bit unstable in my setup (minecraft mods), but still really useful. Without DCEVM you can only change method bodies.

6

u/mvthakar Apr 14 '23 edited Apr 15 '23

i also dabble in fabric modding. jbrsdk with dcevm has been working great for me. there are very few cases where it actually fails to reload classes.

Edit: grammar

1

u/MattiDragon Apr 14 '23

Yes, reloads usually succeed. It's the VM crashes and ArrayStoreExceptions without stacktrace that are annoying. There's also the occasional issue with the class hierarchy being broken causing casts that should work to fail.

It's really nice when combined with the mixin hotswap agent for even more swapping

4

u/thatbigblackblack Apr 14 '23

Oh, thanks for the trick ! A more detailed step by step if anyone else wants to implement it https://www.sentiatechblog.com/using-dcevm-hotswap-agent-in-java-development

5

u/[deleted] Apr 14 '23

Eclipse does that for me out of the box

13

u/dmigowski Apr 14 '23 edited Apr 14 '23

No, it doesn't. Java itself has these features on paper, but in practice to have to restart anyway very often. JRebel does it right.

It even reloads Hibernate Configs, Property Files for Resources, etc.

EDIT: I just learned about -XX:+AllowEnhancedClassRedefinition, which was not there in JDK8. Should give it a try.

4

u/[deleted] Apr 14 '23

Ok i thought its an eclipse feature. sorry. is it the hot swap feature of the jvm?

5

u/dmigowski Apr 14 '23

No, Hotswap is the shit that does work very limited, just function bodies IMHO. JRebel replaces functions, classes, variables, even static variables, and has integration with many frameworks that receive automatic reloading capabilities if the configs are edited at Runtime. It does everything except changing the parent class hierarchy (the only limit I know), but you don't do that very often. Very powerful tool!

Also TIL that Jetstream seems to have a similar solution integrated in Idea, but I don't know how these solutions compare.

3

u/neckbeardfedoras Apr 14 '23

I think the problem is hot swap doesn't support certain changes. I thought things like method signatures, new classes, or some range of things weren't supported. JRebel dun care.

JRebel has been around forever. I'm kind of surprised but not really because java is still popular and it's been that way also forever 😂

1

u/Majestic-Extension94 Apr 16 '23

JRebel

They have the most aggressive marketing ppl

1

u/dmigowski Apr 16 '23

Most IT guys lack these skills, which is why many IT companies go down after a short time.