Video details

Let's make Java fun to use! by Max Rydahl Andersen

JavaScript
01.05.2022
English

For more info on the next Devoxx UK 👉 https://www.devoxx.co.uk
Why should you need to download java, set up a build system and configure your IDE to just play with java and do some scripting and automation tasks? Why is there always this overhead and everyone seem to just accept it or go use another language? http://jbang.dev questions this inherent complexity the Java ecosystem has built up over the 25 years. Come to this talk to see Java be as accessible as you ever have seen it before and how with a little bit of Java we make java fun again.

Transcript

Well come all go back to Riley. So yes, I'm here to talk about make Java fun to use things. We are working Red Hat. In the last 17 years I've done crazy things like working at Core, doing native SQL, doing Harm tools repository, a lot of clips, tooling, even did some Python home assistant. And lately I work on Corpus and I mentioned this to say that I've had my share of crazy things with Java. I enjoyed it the full way. But a couple of years ago I took a break away from it to try to use Go and Node and other things and even that process even took a year off and got to see a few other things. And out of that I came back and needed to reload Java. I've been gone for about two years and out of that came a thing called J. Bank which has this nice logo. And the best thing I can do to kind of show what it is or explain what it is is to show what it does. So here I have an empty directory, I have Jvanginstalled which is the CLI I can run and I can just go and say init Hello Java and I get a file on disk here, Hello Java, I can run it and it prints out hello world, nothing fancy. You could do this with normal Java today, at least from Java 1011 upwards. But the thing the consumption is if you use a bit more, if you want to use dependencies, what do you do then? With plain Java you have to go and download a Java et cetera with J bang. Here's a similar example. Let me just clear this a little bit more. There we go. Same idea, but I'm using an external library called Secret and it will print a little banner. I'll do this and call it DevOps and it prints it out and you go like hey wait, what just happened here? I have a little cash, let me just clear it and let me get this back up again so you can see my screen on command there. Run it again and you'll see the first step. If it doesn't have the cache, it will fetch the dependencies, build a jar and run it out. Print out this input. So what's different between this and single file scripting that does exist in Java is that this works from Java eight and upwards. It works for all kinds of dependencies and you can build anything which I'll get into in a few seconds. The other thing we can do is how do you edit these things? If I open this file and open up an ID, it won't know about dependencies. So you have a command for that called J. Bank edit. I'm going to type it again and this will start up an editor. Here we go. With this project loaded, I can run this as I want or I can go and say I can do this, fix what font. So I have Content assistant here. This is Vs code. If you know Vs code, there are. You can see the product here all loaded and ready. The IDE knows that this is a Java product. There we go. So basically I can get started with Java very quickly, explore things immediately. And this basic stuff about having a script and run it was the first idea of your bank, but it's evolving a bit more. We actually have something called aliases. So I can do stuff like alias lists via bank there which is a catalog and I can see things that are here. These are not on my disk right now. For example, here's a little bouncing logo thing. I'm just going to try and run it. And this is actually a Java Vex app which is being downloaded and run. And now we have most boring fancy DVD JBang thing. But the fun is this is just a little script loader uploaded and I can run it directly and I have this alien to refer to it. Other thing you can do is I have here. So if you heard in the new version of Java 18, I think they're going to build in what's it called Http server. So you've used Python and Otis. There's like a single line you can run ASP server to serve out content. So with J bang, you can do a Java eight and upwards. You don't have to wait for Java 18. So for example, if I do that here in this product I have an index. Html CSV file and some Java code. I'm just going to serve this up. So again it's going to run this jar and I can now there you go. And this is actually just showing like people using Jaybang to check update JBang. But the idea is that with Java, with Jabang you have these kind of small utilities you can write and make accessible to anyone out there. And that's the core of Jbank. Having these tools available to you ease to get to. If you know this about JBang, you know most. But I want to get back to a bit about how we got here. Basically I was in a dark place, but imagine you have a door sitting out there that is everyone knows how to use the door, but it's in the back alley and it's a bit hard to get to versus a more inviting door where you can see what's going on. Let's say the people you know is green is light. I kind of felt that was the way Java is in the industry. Of course everyone who knows and love Java knows that. It's not that gloomy, but it's that kind of perspective you get if you go to the other environments where things are just a bit more accessible and those other environments are stuff like Python and Node or Go, they kind of spend a bit more time on this direct first impression environment. And what I want to do is basically make Java have the same kind of thing. And I believe with JB and we kind of got there. So just to explain what's going on here's again the same examples I had before, you'll see here on the top, this is slashlasherbinv that is both valid Java. It's a comment and a shell script. These are just running Env Javang and that command. This is what allows you to run it directly in Linux and Mac. You don't need it, but it is the thing that Javen got his name from. Then you have these commands like slash last steps and you can have any amount of them to get dependencies. There are other commands to set option, that kind of thing we'll see later. But this is kind of the key to get these dependencies and you can run the code and the magic is which doesn't sound like much, but you can just compile it and run it in one go. You don't need to know about a compiler, you don't need to know about runtime, you just need to know hey, I can run this script and that's it much easier to tinker and play with. There's no need for any VM or Java set up. Jbank will take care of that for you. And multiple files is also not a problem. This on the screen here is like the smallest micro service on the planet, a hello world written in corpus. And you'll see stuff like that is referring to auto classes. You can do that. You can include files into your jar saying hey, I have an index Sml that goes to a specific amount point and you can include different sources. So you can have basically any kind of single jar file kind of structure in J Bank. And of course this means that you can write small scripts which is what jbank started out with, or full blown applications, which is what I've been doing the last one and a half year. Okay, so what does that bring us? Besides I can get to run this thing very quickly. Well this is a typical example, like a small example, how many files are here like 2020? And what it does is it's a Cube cuddle plugin that is written in Gradle that creates a jar and nothing special if you already know about how it is how Java works. But with Jvank the starting point instead of being this is the convention you use for in Gradle or Maven. In Jabang we can remove stuff like Gradle and Jebang. We can remove all these layers of indirection that we've had, we can also have them, but we don't have to have them. And then you can also just start removing all these extra classes which if you look into them are just small inner classes that doesn't need to be in the public spoke. They can just be in a single file. Again, Jvan will work with that complex scenario you had before. But my point is that with Jaybang you kind of start from being able to be at this level of just a single file and a README and that's it. Jaybank even can do this where it doesn't have to be named main. Java will just be cubecutler that's example. So if you're on Linux or Mac and you have Kubectl, you can run this directly. And this is what this example does, cubectl example list. It will look up Cube. Cuddle example and run the thing you see. This also like if you want to extend Git or GitHub or Docker similar things you can do this way. And my point is that what I managed to do with Jamming is I don't have to deal with these layers like this massive depending on how you say it. But all this is what we present to newbies out there in the world today. And all this even if you're advanced Java user, you're like oh I have to set all up all this just to write ten lines of code. Jvan kind of switched out on the head and just say no, you just need one file and you're good to go. All that stuff is taken care of for you. And then the other part by one thing is just being able to write code and get to it. You can use Notepad if you want to. But what if you want to set up an IDE? So we have a J Bank edit command which only exists because not all ideas in the world has picked up on Japan and supported natively. But what we do is when we run Javang edits we understand how this will look for an idea and we kind of on behind the scenes to create a project that has a built Gradle file and all the files that Eclipse needs for Eclipse. Java needs to understand its code. And why do you do those two things? It means that basically any IDE that supports Gradle which is all like Eclipse, Intelligent, netballs, et cetera will just work. But also all the IDs who understand just Eclipse which is everyone who's using the Java Language Server protocol, they will work. It also has a benefit in Eclipse it will load faster than anything else because Eclipse supports natively. You don't have to analyze the Gradle stuff. But in any case the idea is that if you start as a new user with Jbank you can create projects, run them and edit them without anything else configured. Jvan will take care of it for you. And what we do is we install the thing called Vs code which is a free version of Visual Studio coder. Visual Studio code. But if you have another editor we will use that too. It's not like we're forcing owner, it's just to give you the option. Oh, I forgot the netball logo here. But Eclipse and cell gay vs code Emax vim revelets, even code spaces codebase and GitHub and Eclipse Cha will work with a J Bank project. It is, after all, just pure Java. And that's my point is that this closed door we have in the backyard in the back with JVM becomes a bit more open and more accessible. And you can imagine one thing is you can get started quickly. So when I built this, we had some Kotlin scripts in our building system in Corpus that I said this is like 99% Java. Why do we need to download Kotlin to do this? But then when I had it, this J Bank thing, any extreme that it does, I just start with J Bank. I don't have to use it, I can just explore it. Even I can run it on any laptop out there. I don't have to have anything set up so I can explore a new API, I can focus on the code, I can use it to report bugs, I can give it one file example in docs to run or yeah, it's much simpler than the typical Maven Gradle setup. Okay, so just to again, we've seen some of your stuff, but just to kind of compare how things work in Java typically versus how it works in Javang and behind the scenes, what Javang does is just translate this to what Java. So if you want to run something in Java, you can run sync files. You can run Java shell but not a command and not a flag. It can't handle arguments. Java jar is not what you have to do to run jars. And you have mavenx Java, Maven Java, X run. And they all have different ways of configuring themselves. And except for Maven, they don't support dependencies unless you manually set them up. But yeah, that's just the same. Like whether you're running a Java JSL, even a Kotlin or Jar, it's the same way you can pass parameters and you see here XYZ JSheld doesn't support parameters natively J bang enabled for it and debugging is nothing else but just adding a dash debug. You don't have to remember all the arguments or have an ID that can do it for you, you just add it and connect with an ID and off you go. The shell is just struck out because JShell in its inherent form doesn't support debugging. So that's a Java limitation that we can't work around with JV. Yeah. So what I want to go is like the next level where until now we've just kind of been wrapping stuff around normal Java in Jvan you can also run artifacts directly. So here I'm running at corporate CLI. This could also be any other CLI you've written in Java and published Domain Central or some auto repository. You can run it this way. You can run a gist so you can just upload code to adjust and jvang will extract the right code and run it for you. Same thing that GitHub. Or if you're into Carbon now this nice little rendering thing that can print Code, J Bank will understand that you are too. I even had Twitter support at some point just for fun, but Unfortunately Twitter removed their access to you can't look at public statuses anymore without having JavaScript, which is a bit of overkill. But you guys run a single jar. So the difference between a single jar and a Maven artifact is that the Maven resolve of the penalties. Whereas it might be assumed that the jar is self contained, but it can be hosted anywhere, so it doesn't even have to be on a main central. It could be a site like here, or it could be like a GitHub release page or something like that. Another alias concept I can add here. I'm saying add an alias called Corkers that refers to this runner thing. Now I can run it as Jbank Corkers, or I can say J Bangkok Corpse IO which is a catalog. I'll get into that in a few seconds. And my point is that with J bang and it's run feature, which is not limited to just script files, but anything else that is like typical Java jars you can run. So even if you don't buy into hey, it's nice to have scriptable files and these built from source kind of thing. If you have any jar out there, any CLI you want to do with J Bank, it becomes super easy to just distribute and run it. Okay, coming back to the built tools conversation, I have nothing against Maven or Gradle and Ant or whatever built tool you have out there. I just feel it should not be needed or you're not going to need it. At least not usefully. With jvang you can do multiple files, you can put it all in and you can run it explicitly. So you can have a pre built jar, or you can run it and it will build if necessary. Or even if you have gravel VM and has that native, we will try and make a native jar for it. And if you have a native jar, we'll run that native binary. We'll run that instead. So you get the speed up all kind of transparently handled for you with the Jeeping. Other things you can do. So since Jpang knows about the source files and it knows about the structure of your jars, it can make them too. So here I've taken Minecraft. This could also be Wildfly or Tomcat or any other application server that has a jar that needs to be run or packaged. J Vanity Poor will just create a jar with a manifest that refers to the jars on your disk, and this will work in Minecraft. You can even run Minecraft with Vax. But the point is that this could be any kind of app server out there. Same thing if you're into Lego or other stuff where you have to deploy stuff to a robot or container somewhere. So we can create a jar that is portable and by portal means a jar where the penalties are next to each other in a Lip folder. And then you can just cover that somewhere over. And again, I'm using mine stuff just because it's more fun to talk about. But this could be any kind of Jr deployments out there. Okay. And then I mentioned Corkers also before, but the point is that you can write full enterprise microservices with J Bang using Cockers. It could also be Springboard or Micronaut and others. But the extra beast there is between J Bang and Corpus is Corker has this notion of build time enhancements and Jaybang has an Existence API that Corpus hooks into. So that all the stuff you can do with Corkers, like build a native binary and build containers at the volume to open shift or is possible for anything that is driven by J Bank. And the idea is that again, make it accessible, made it easy to use, don't have to have 20 different files for something that we've over the last 2025 years have found out. There's a good convention to do. And right now, with all these microservices and deployable jars, that's possible today, right? It's still experimental. Not everything is enabled yet, but all the basics are there. So do please try it out and let us know if something is missing. So yeah, anyway, for building stuff you don't need, just don't repeat yourself, keep it simple. That's the philosophy behind J Bank compared to, let's say, the Java ecosystem. And again, comparing Java to J Bang. When you develop something, you have to code it, commit it somewhere, build it, publish it and ship it. And then as a user, you have to download Java, install Java, download app, install the app and run it. And these are reasons for doing that for some cases, but for basic stuff or even full blown applications, you don't need all of these. So how does this play out in Gear Bank? First of all, to get Gerbank, it's available in almost any kind of package form for Windows, Mac and Linux, even for Raspberry Pius. The last thing we had, like some older central machines. Or you can just use Curl to download and set it up. Or if you're on PowerShell with Windows, you run this kind of command so you can get it in any kind of form of shape you want to get it running. And once you have that, and by the way, those commands actually works on Raspberry, Pi and like it runs on Docker container, it's all there. If you want to use a specific version of Java, J Bang has an option that says that's Java 17 or JavaScript 14, et cetera, it will get it for you if it's not available. So if you already have a Java on your machine, jbank will use it. If it has like that 17 and you have it, it will use it. But if it doesn't have it it will download it for you. So we try and use the thing you have configured by default. But if that doesn't fit then we will get the specific version for you and it uses adoption or via Fuji. So we can download any binary that has been built there adoption and adopt. So all from Java Eight up to the later versions. And just imagine this means that if you're writing a tutorial or a guide or something, you can just tell people to install Jabbing and run these examples you have and you can tell them to run with a command line and it will just work. Or you can put it into source file. So this is an example. Before JavaScript was a Ga. If you wanted to enable records you would have to enable preview at least 14. This would work. You put that in the file and then JBang will you don't have to say Java 17, you say J bang XY set Java and this works for anything, whether it's an alias, URL, maybe not Fax, that kind of thing. It will just kind of be there. Okay, let me go back again. So the minimal step to write a Deerbank app is the init command here I'm using. Desteybank also has support for templates. I don't have time to show them, but they're there and it will create an application and you can code away and you commit it. You have it then in GitHub or bitpocket or somewhere else and you just give the URL. And now any user can run this simplest way to dispute a Java example or a project that I know of. At least you can also do app install of ul and artifacts or that kind of thing. And then you can just run my app. So this is like if you use NPM install a Pip install, but just for Java. And yeah, it works in somewhat the same way, at least in the store. The same that you can install anything and have it available in the past. So any day to day you can just don't have to know there's a J bang behind this is the Java world in jvang coding for developers. It's just code and commit and user can just run it directly with that Ul I talked about. And if you don't have user don't have JBang, here's one way to get it on any system that has a Bash terminal. So Windows, Mac, Linux, this will all work. Windows like VSL or Git, bash will have that. Or you can do the install step which says app install it and then you run it. Okay, so it's just just for you to understand how it works. We have a convention for looking up in GitHub and big bucket and GitHub. So J. Ruby. We look up the organization JRuby. It says J bankalog. We will read that. And here you see they set up a way to run the latest JRuby version and that enables you to do things like this like app install or the Quakers, or in this case Gap search, which is a little utility to search for GVS and main repository. Okay. And all of this, we actually also made available little app stores. So if you want to see what people have published, if you go to JB and Dev and click on App Store, there is a list of these things. And one thing that notable, one that got added lately is Microsoft has this library called Playwright to test different browsers. And there's a lot of utility you can use to record your interaction with a browser. And it will give you a Java file that you can use to replay and modify and screen scrape whatever you want. And then you can just run that with Japan. But I find it really cool that the companies like Microsoft and others who are trying it out can publish ideas and small utilities this way. And something that I think we've been missing in Java for a while. And I just want to say that J banks make it easy to get started. We can run anything. It doesn't have to be a J Bank script, it can be anything you build with Maven Gradle, it will just be fine. You can create projects without having to use Maven Gradle. Just everything kind of reduced to its absolute minimum. And the way you deploy is much simpler than anything else I'm aware of. At least there's a lot of stuff I haven't covered we don't have time to do. But J Bank supports almost everything in the Java ecosystem, at least stuff that I'm aware of. And I wanted to give a little deer in people's head is what I'm going to show you. Doesn't exist yet, but it's been talked about and suggested by multiple people is what if you can do Jbank alias, add some catalog, and then suddenly you can run JVM builders or test X or export like something that does custom build and test for your kind of project. These areas can be global, they can also be local on your disk. So you can be kind of anything again, something that exists in NPM world in some form. Or like Duncation, I do believes. And when I say release I do want to mention Jay releaser done by Amari who was here yesterday which actually took some ideas in J Bang on how we do build for every platform or every packaging system and generalize them. And I find it funny that he built Jerusalem based on Gear Bang. I'm now using Gabriel's in Gear Bank to release and I might also build a release tool in all kind of meta. Anyway, that was very quick way through and trying to say that Java is a better Java, at least in my opinion. I love to hear what you think. And if you want to help any of the biggest fight I have at the moment is for people to realize that it exists and it's alternative and it works with both Maven and Gray. It doesn't ignore them, but it enables you to make it easier to just try out any kind of Java stuff out there and yeah, that kind of concludes it there's a ton of different ways you can get JBang and I can only say thank you for anyone who's contributed over the last four years and yeah, I think that's it. With any questions left.