“I wonder how I can use VScode to run Java?”
And it wasn’t so easy to figure it out. Why? Because Java has things going on behind the scenes, that are abstracted away by the Java IDE’s.
If you do a google search, you can find this VScode article for more info.
We are going to use purely the CLI to run java here. No extensions. We’re doing this to understand how Java actually runs code.
Here I explain how to do it, for those java newbies and developers looking for deeper understandings of how Java works.
1. Install the JDK 11 (Java Development Kit)
Java 11 is a Long Term Support (LTS) version, so that is a good one to download. You need the JDK installed in order to write Java code on your machine. There are a myriad of options of which JDK flavor suits your fancy.
But because I’m a fan of Open Source as well as AWS, I went with Amazon Coretto, which is Amazon’s open source offering of the JDK.
After downloading and installing the JDK, open up a terminal window and check to see if your JDK is installed correctly.
A successful installation should show the following :
2. Create a new java file, in any folder of your choosing. Write some code.
My folder is called java-lambda. and in it, I created the file Medium.java.
I created a simple Java class, that has a main method, which prints out a message.
You see that little yellow squiggly line under keyword class? Well that message says:
Let’s ignore that warning for now, and try to get it to run.
3. Run the java file.
And to our delight, we get output.
Well that was that easy!
4. Create a package with multiple Java Classes
To run a single Java file in isolation, it’s as simple as running the java file using the java command.
But let’s try running a set of java classes, in a package, and see if we get off this easy.
Let’s create the folder medium, and add three classes:
- Main (entry point)
Here is how the 3 files look:
It should be as simple as adding the statement package <folder_name> to each java class, right?
Now when we try to run the Main class, here is what we get:
It looks like java doesn’t understand the java code! Well why might that be?
5. Set the classpath to the package location
If we take another look at the java code we wrote, we will find a yellow squiggly line under the keyword package. Here are the messages:
And we now learn about this concept called the classpath. Basically, java needs to know the location of java classes in order to run them. The classpath variable needs to be set. We can find this out by doing an echo command.
After running this command in the terminal, we get absolutely nothing.
This tells us that there is no classpath set. To set the classpath, export the path to the java package. To get the current path, you can type pwd in the terminal. Also, make sure to be one directory ABOVE your package folders.
Now, when we echo the $CLASSPATH once again, we should get back the path we just set.
If we now try to run Main.java in the terminal, we still get the same errors as before. What is missing?
6. Compile the Java source code into byte code
Long story short, because Java is a compiled language, we must compile our java source code into java byte code which is interpreted by the Java Virtual Machine (JVM). The JVM is included as part of the JDK. What is source code you might be wondering? Well it simply is the original code that we as developers have written.
To compile our source code we run the javac command.
The asterisk is a wildcard, selecting all files with the .java extension.
After doing this, we see a set of new .class files generated.
Now, let’s try running the Main.java file again, after moving up to the parent folder.
We get another error, but at least it is telling us that there is a class found on the class path. That class looks like it is in the format of Java, so let’s try to run it that way.
And voila! All the classes in our java package folder are now running!
7. Archive the bytecode into a JAR file
It does become a hassle to deal with a bunch of .class files, especially if we want to allow other people to run our code. To account for this, java allows us to package all our stuff neatly in a JAR file, similar to a ZIP file for java files.
Let’s try JARing up our java code. The JDK we installed gives us a jar command.
We need to Create the jar File, specify the Entry point, and specify the .class files the JVM needs to use.
cfe stands for :
C — create a jar file
F — specify the filename for the jar file
E — specify the entry point of the package.
Running this command generates the jar file. When we run the jar file, we get the same output.
**note that you can add the flag -v to get Verbose output when the jar is being created. for example : jar cvfe …
So is it a hassle to go through this everytime we make a change to our code? Most would probably say yes.
This is why those Java IDE’s like IntelliJ and Eclipse do this stuff behind the scenes, everytime you press the Big green Run Button.
You can even make this process automated in VSCode as well, by installing a Java Extension.
Then you can click the little “run” button on top of any executable main method.
TLDR (too long, didn’t read)
- In summary, to run a single Java file using JDK 11 or higher, you can simply execute : java <filename>.java
- When running a package, the two hurdles we had to cross over to run Java in VSCode were:
- Setting the CLASSPATH
- Compiling the source code into byte code
Doing these two steps are necessary to both understanding and executing java code without the abstractions and handholding provided by full fledged java IDE’s.
- modularize the byte code generated by compilation into a JAR file that can be executed locally or by other developers