reddit / emacs

Vanilla Emacs to Java IDE in 5 minutes

torsteinkrause · 2022-05-15 12:07
jsled · 2022-05-15 12:44
> "in 5 minutes" > video: 11:25. XD
torsteinkrause · 2022-05-15 13:09
Haha, yes, time ran faster than I anticipated. I could still argue, though, that 5 minutes into the video, there's a working Java IDE and the remaining 6:25 is just icing on the cake ;-)
notoriouslyfastsloth · 2022-05-15 13:45
really helpful thank you
maxc01 · 2022-05-15 13:56
2x speed playing:)
faldborg · 2022-05-15 14:23
Heads up, looks like the link to the org file is wrong.
torsteinkrause · 2022-05-15 15:31
Sorry about that. These return `200` now: - https://talks.skybert.net/r/2022-emacs-java-setup/emacs-java.html - https://talks.skybert.net/r/2022-emacs-java-setup/emacs-java.org
torsteinkrause · 2022-05-15 15:32
Thanks!
shryzr · 2022-05-15 16:20
Thank you for making this And I have enjoyed your earlier posts. I think there is very less material out there on using Emacs effectively with Java projects and going beyond basic examples. One question I have desired guidance on is that given the very nested folder structure of Java and naming conventions - how do you quickly create a class and ‘get started’ testing some idea in your head? Netbeans allows module and class creation via right click contextual menus. Another question if I may : for a beginner in Java, would you say that it would be more productive to use a main stream IDE rather than Emacs with LSP, until some comfort is gained with Java?
NoisyFrequency · 2022-05-15 17:15
If you want to quickly try ideas in Java, you can use `jshell` but honestly it's not really made to pop a REPL.
NoisyFrequency · 2022-05-15 17:16
This is really cool but without Maven interactions to update dependencies, create new projects easily (like JavaFX projects); it gets pretty hard and I always find myself falling back to another IDE in the case of Java :/
deerpig · 2022-05-15 17:50
Vanilla, as opposed to chocolate or perhaps strawberry? Vanilla extract is an added flavor; it is not what you get when you take away everything else. If you take away Doom or whatever layers of abstraction you have applied, what is left is Emacs, warts and all. Emacs is the baseline. Love it or hate it, it is what you get out of the box. Everything built on top may be glorious, it might be subjectively or ideologically superior, but at the end of the day it is all frosting and fairy dust sprinkled on the Emacs doughnut.
AegisCZ · 2022-05-15 18:39
emacs speedrun
Trout_Tickler · 2022-05-15 19:12
Looks good, this would be a good candidate for a [tutorial](https://emacs-lsp.github.io/lsp-mode/tutorials/CPP-guide/) on lsp-mode itself (we need more tutorials over here...)
BigRiverBlues · 2022-05-15 21:23
Cool, thanks for sharing. I doubt the available emacs packages are enough to replace Eclipse, like sometimes the incremental compilation or whatever it is that makes building so fast is absolutely needed... but I think I should set this up to at least use when I'm trying to write a bunch of code, because the Eclipse editing experience can be very frustrating in multiple ways, so I should try something else out, and of course I enjoy emacs!
lovdev · 2022-05-15 21:32
I am trying to figure out how to reach this vanilla level... Ps. btw, thanks for this!
[deleted] · 2022-05-15 22:37
[deleted]
redguardtoo · 2022-05-16 00:53
Minor improvement on `compilation-mode` setup, ;; ANSI-escape coloring in compilation-mode ;; {{ http://stackoverflow.com/questions/13397737/ansi-coloring-in-compilation-mode (ignore-errors (defun my-colorize-compilation-buffer () (when (eq major-mode 'compilation-mode) (unless (featurep 'ansi-color) (require 'ansi-color)) (ansi-color-apply-on-region compilation-filter-start (point-max)))) (add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer)) ;; }}
torsteinkrause · 2022-05-16 03:07
> This is really cool but without Maven interactions to update dependencies In case you want to hear my workflow: I do `C-c p f`, then type `pom`, the fuzzy finder gives me a list of available POMs and I select the one I need, and add the dependency (copy and paste from either a different project or a Maven repo web interface. That's fast enough for me, but do you mean the convenience that the IDE asks you "I see you use `org.foo.Bar`, do you want me to add this dependency to your project? If so, I don't want this since the IDE will often add the dependency to the wrong POM and that becomes a waste of time when I'll have to move it around and have to rebuild the whole shebang in order for the boat load of Maven plugins to work correctly. > create new projects, easily projects (like JavaFX projects); JavaFX! I haven't heard about that for a while. What do you use it for? As for creating new projects, I suggest creating snippets for the types of projects you need, so you can do `pom/javafx <TAB>` to get the appropriate `pom.xml`. > it gets pretty hard and I always find myself falling back to another IDE in the case of Java :/ I understand. IntelliJ is a great program.
[deleted] · 2022-05-16 03:08
[removed]
torsteinkrause · 2022-05-16 04:05
> Cool, thanks for sharing. You're welcome :-) > I doubt the available emacs packages are enough to replace Eclipse, like sometimes the incremental compilation or whatever it is that makes building so fast is absolutely needed... When running `lsp-java-build-project`, that calls out to the running LSP server, which again uses Eclipse's Java compiler AFAIK. The `lsp-java-build-project` takes an optional parameter which, if it's `false`, should make an incremental build instead of a full one. Might be worth checking out. I'm interested to know, how large is your project and if you use Maven, how many Maven modules does your project have? Speaking of Maven, it has incremental compilation too, but we haven't been able to use it due to https://issues.apache.org/jira/browse/MCOMPILER-209. Still, compile speed hasn't been an issue for me, but this is probably a matter of what you're used to, unless you have a humongous project where you constantly touch many different modules (or else, I'd just do `mvn -f path/to/submodule <goal>`). As a yard stick, my Emacs/LSP/Eclipse workspace has ~`4,400` Java files and `204` Maven modules. The largest Maven project therein is comprised of `62` modules.
NoisyFrequency · 2022-05-16 09:01
I use mostly Java for school rn so I'm in the situation where I'm creating lots of somewhat short lived repos that'll have to change depending on the course (thus the JavaFX thing). Right now I use Emacs mostly for Python and C/C++ when I can avoid the dreaded QtCreator. I'm pretty interested in JVM languages though so in the future I'll probably invest the time to make something to create projects, ideally a `transient` interface.
i_like_peace · 2022-05-16 10:42
u/torsteinkrause how'd you get the toolbar to look like that?
BigRiverBlues · 2022-05-16 13:44
Awesome, I guess that's more encouragement to set this up and give it a try.. How exciting! I'm working with similar amount of files, but maybe half the number of Maven modules. One feature we use a lot is Maven Profiles. So we have a common library that we publish to our repository, and of course multiple different apps depend on it. But we leave the version of the common library as 1.0 in the pom, only when we publish it to the repository do we change the version to something else. So then, when we make changes in the common library, and are testing locally, we turn on a Maven profile that uses 1.0 (which of course is a version that is installed locally by building the common project) instead of the version that is published for production use. I'll have to take a look and see if I can find that, otherwise I could do some workaround. Do you ever find yourself using other IDEs alongside your Emacs setup for any reason? One other thing that I don't think was included in your video was running the applications from Emacs. Do you do that? I assume there's support for that haha, yeah I actually hate the launch and run experience in Emacs. Starting and stopping takes multiple clicks.. often I have a tomcat server that tries to go into some kind of graceful shutdown, and then when I hit the stop button more I end up with multiple windows about "failed to terminate" and I have to close those little windows. The experience of switching between multiple consoles to view the different logs you have open is terrible too. Yes, thanks again, maybe I will get back to you once I set it up, and I will let you know how it goes.
dzecniv · 2022-05-17 00:48
Nicely done. Is your command log command-log-mode (https://github.com/lewang/command-log-mode/blob/master/command-log-mode.el) or something else? Thx
torsteinkrause · 2022-05-17 03:47
Thanks. No, it's interaction log mode. I've created a wee screencast about it here: https://youtu.be/QfN0gNaxE2U
torsteinkrause · 2022-05-17 03:48
😊
torsteinkrause · 2022-05-17 03:50
Nice, I saw that one too, but it wasn't obvious to me what problem it solved over the simpler snippet I used, so I went for the simpler one.
redguardtoo · 2022-05-17 03:54
better performance, I guess. When debuging enterprise application, the output could be huge.
torsteinkrause · 2022-05-17 11:05
If you mean the theme, it's `sweet-theme` that's installed with: [https://talks.skybert.net/r/2022-emacs-java-setup/emacs-java.html#org67b8150](https://talks.skybert.net/r/2022-emacs-java-setup/emacs-java.html#org67b8150) If you meant some of the icons that show up (like the light bulb), it comes from the `lsp` setup. I started off with a clean `.emacs`/`.emacs.d/init.el`, so everything's in: * [https://talks.skybert.net/r/2022-emacs-java-setup/emacs-java.html](https://talks.skybert.net/r/2022-emacs-java-setup/emacs-java.html) * Or if you prefer just `.el`: [https://talks.skybert.net/r/2022-emacs-java-setup/init.el](https://talks.skybert.net/r/2022-emacs-java-setup/init.el) (extracted from the `.org` with [https://talks.skybert.net/r/2022-emacs-java-setup/create-init.el-from-org.sh](https://talks.skybert.net/r/2022-emacs-java-setup/create-init.el-from-org.sh))
torsteinkrause · 2022-05-17 11:07
Feel free to link to it or embed it :-)
torsteinkrause · 2022-05-17 11:32
>Thank you for making this And I have enjoyed your earlier posts. I think there is very less material out there on using Emacs effectively with Java projects and going beyond basic examples. Thanks. That motivates me to create more content. What kind of things would you like to see? &#x200B; >One question I have desired guidance on is that given the very nested folder structure of Java and naming conventions - how do you quickly create a class and ‘get started’ testing some idea in your head? Netbeans allows module and class creation via right click contextual menus. I open a new file with `C-x C-f <fiilename> RET`, then type `class TAB` and `lsp` will expand its builtin snippet that inserts the relevant Java skeleton, including correct `package <groupo>;`. That's fast enough for me. If you'd like it even faster, you can of course wrap that in function that you bind to your own shortcut. You could also create YASnippets for different kinds of classes you often create so that you do e.g. `rest <TAB>` and get a JAX-RS webservice endpoint. What I often do, though, is to test my idea out in an existing JUnit test class (since that gives me all 3rd party libraries on the classpath) or as u/NoisyFrequency suggests, use `jshell`. Another feature I use, if I'm debugging something, is to use DAP's REPL, `dap-ui-repl`. &#x200B; >Another question if I may : for a beginner in Java, would you say that it would be more productive to use a main stream IDE rather than Emacs with LSP, until some comfort is gained with Java? Brace yourself, here comes a rant: I would recommend all beginners to have as little help as possible. I'd almost say Emacs with LSP is too comfortable. Don't know if this is a controversial stance, but there you go. I came to Java after 6 months of using Visual Basic 6 and thought I could code. In VB6, I created apps with multimedia, lots of GUI elements, a web browser, an editor and an SQL interface. I coded, pressed `F5` and my app started in full colour. And then I came to Java and spent 2 weeks just getting "hello world" to work in a browser applet (good thing those are not around anymore). Anyways, I'm grateful for that rocky start as it taught me important things of Java, what is and how does the classpath work, how does the editor find the correct JDK, what's the difference between JRE and JDK, how do you build your own app with packages (not null package) so that the JRE finds it and not to forget, it's 3rd party dependencies, and so on. I believe using a simple editor improves your memory, it lets your brain breathe and reason about a problem, it doesn't intrude. Then you compile and get all the errors and can sort them out one by one. Then you learn and move on, become better and so on. Once you've mastered the basics, you can start appreciating what an IDE does for you, what problems it solves and so on. Now to the point of my story. When something breaks, you are much better equipped to fix it. And things *do* break. Either it's Jenkins not building your code, or it's your `Dockerfile` which fails, or it's Openshift or it's Tomcat that barks at you. So which one is more productive in a short term perspective? If you're fluent in Emacs and use it for everything else, you may be faster in Emacs/LSP, but normally most folks will be faster in a mainstream IDE (of which I'd definitely recommend IntelliJ IDEA).
torsteinkrause · 2022-05-17 11:42
> only when we publish it to the repository do we change the version to something else We do the same, except that the version in Git is `develop-SNAPSHOT` and don't use/need profiles for that (use `mvn versions` at build time to set the `1.2.3-4` version). > Do you ever find yourself using other IDEs alongside your Emacs setup for any reason? I've always had IntelliJ as a backup, especially for larger refactorings and debugging. As lsp and dap has become better and better, I use IntelliJ less and less, though. I re-installed my computer `2021-12` and six months later, I haven't used IntelliJ even once, so things are definitely improving. Still, I keep it around, just in case. > running the applications from Emacs. Do you do that? Yes. I have my own build wrapper which compiles and runs my application. I run it `compilation-mode` so that when I hit `g`, it'll kill the running process, compile what needs to be and start it anew. I've bound `compile` to `C-x C-c` since I never quit Emacs. This leads to funny experiences when I use other people's Emacs ... > maybe I will get back to you once I set it up, and I will let you know how it goes. Please do!
Trout_Tickler · 2022-05-17 16:48
I don't have one, I'm saying this would be a candidate for one :p
noman_032018 · 2022-05-17 20:13
I don't know if that's the case with Maven, but with [Gradle](https://docs.gradle.org/current/samples/sample_building_java_applications.html#run_the_init_task) and [Leiningen](https://github.com/technomancy/leiningen/blob/stable/doc/TUTORIAL.md#creating-a-project) (Clojure), the job of creating new project skeletons is expected to be done by the build/dependency/project tool's subcommands rather than your IDE. edit: It appears [Maven has something like that too](https://maven.apache.org/archetype/index.html), but it seems a bit less user-friendly.
NoisyFrequency · 2022-05-17 21:18
There are a bunch of archetypes with maven but every command like running the program or the tests typically involve a lot of arguments. I've played only a bit with Clojure but lein is clearly an improvement in that regard.
noman_032018 · 2022-05-17 21:22
Leiningen & Gradle were effectively my introduction to the notion you *could* have a JVM development experience that wasn't like pulling teeth (`ant` is a great example of this). Before them, I just assumed the JVM was a loss and nothing save large IDEs could give you a halfway decent experience on it. At some point motion reversed and having IDE-agnostic tooling that does the work is now (rightly) seen as the proper way to do things.
NoisyFrequency · 2022-05-17 21:30
Yeah, there's nothing worse than tying the build mechanics with the IDE. Like, in C++ it's definitely not a given that a project can build easily in all IDE's if you're not using the command line (make or CMake mostly). In Java, I've still had problems this **year** with some project working on Intellij and NetBeans at the same time. It's pretty infuriating.
noman_032018 · 2022-05-17 21:33
I definitely agree. [Visual Studio](https://en.wikipedia.org/wiki/Microsoft_Visual_Studio) is also a major offender in this regard.
NoisyFrequency · 2022-05-17 21:40
Yep sadly :/
shryzr · 2022-05-25 20:38
Thank you for the awesome response and sharing your thoughts. Well, it so happens that I've just switched jobs and very likely do not need to use Java anymore ! In terms of content, I think I was probably looking for a more fundamental and general context to programming (Java). For example to be a fly on your shoulder to watch a full working example of using Emacs in handling different contexts in a typical Java project , like working on and running say an existing Maven /Spring app, perhaps something larger than a toy app, and then refactoring, understanding project structure and all the aspects you have described. I had a non-dev Python/R background and was working in a Java shop. IIRC it was actually a lot easier to get started with ESS for R and emacs-jupyter compared to Java, but maybe thats me. I enjoyed your rant and agree with your point of view and how the editor doing less can help you learn more. I think I have seen this first hand and it has certainly occurred to me in the past. The little I managed to learn in Java was pretty much the way you described! That is encouraging. I've mostly used Emacs for everything, (drawn by Org mode), and only started recently with LSP when possible for Python, R and Java.
shryzr · 2022-05-25 20:47
I often felt whatever I approaches I used to get stuff done with Emacs for Java (with Netbeans open in another window) felt... crude or hacked together. They lacked a methodical elegance or purpose driving the actions. Perhaps that comes with time and knowledge, but weirdly I did not have such in depth misgivings about 'workflow' and editor efficiency while working with R and ESS for example.
torsteinkrause · 2022-05-26 15:45
I could do a live coding session, time boxing it for an hour, and then you could ask questions and steer the coding session where you want it. Never done that before, but I guess YouTube Live is a thing, just have to figure out how to use it. Would that work? And would more folks be interesting in joining?
BigRiverBlues · 2022-06-07 16:38
Hey thanks, I stole a bunch of your emacs config, not just Java stuff, and in the process added some nice new features to my Emacs setup :) Regarding my team's workflow around Maven profiles... It doesn't seem like java-lsp has any support for setting Maven profiles, and I guess lsp-java uses mvnw, which apparently doesn't honor the .m2/settings.xml file, so even by setting the profile there I wasn't able to use that workflow exactly. Which is all a little concerning, unless I'm missing something, but I will raise an issue with them if I ever need to, because I did find the way to do it. The way to do what I wanted was to lsp-folders-workspace-add I just add the local version of that dependency and then it is used instead of the version that is specified in the pom.xml. So now I can run tests against my spring-boot application, and I assume debugging the tests will work fine. But let me get this straight, in order to just run the application, you wrote custom code that mvn builds it and starts it? I also have multiple other non-spring-boot projects. One runs on tomcat, and one is just an executable jar. Any tips on the best tooling to use to get those running? I'm kind of expecting to have to script something to mvn build and run the stuff, and that is fine. Of course using the incremental compiler would be nice, but Eclipse already has multiple bugs and inneficiencies that occur when I try to run my app on Tomcat, for example, such that I'm ok with a little bit of slowness and stuff here in exchange for reliability. [EDIT: I'm assuming you use the compile command with compile-mode or maybe the run command for projectile-run-project?] ~~Finally, as you're using the various java modes, you end up with quite a specific and elaborate window setup. Any tips for managing this? Maybe I can look around a bit more about this, or maybe I missed it in your config. But for example, maybe I want to automatically snap open the various views. Maybe just writing an emacs function that invokes all those modes will do the trick... but then let's say I want to go back to 1 or 2 windows as I use other Emacs mode. Would there be an elegant way to bring back up all of the Java windows in the configuration I want? Or maybe I should just use 2 different frames?~~ [EDIT: Feel free to not answer this, I can find this info online.] Thank you!