Compile to bytecode
javac HelloWorld.java
It outputs a .class
file which is the bytecode
that the JVM
understands.
To execute:
java HelloWorld
The above command executed the compiled class
not the .java
file. Notice that it is ran
without the .class
extension, in fact java
looks for the class in the current directoy or
CLASSPATH
. The class must have a main
method.
We need a pom.xml
to define the project structure and dependencies. Then we run
mvn install
and maven
will install the dependencies in ~/.m2/repository
.
maven
is much like cargo
, it manages the project entirely. You need a specific
structure for maven
to execute properly: you need to have your code in ./src/main/java
and then execute it, not through javac
, but rather
mvn exec: java -Dexec.mainClass="EnvToken"
If a method within a class is static
then that means that we can invoke the method
without having to instantiate the class itself.
Install latest java
development-kit:
sudo pacman -S jdk-openjdk
To check the default version:
java -version
javac -version
Since I first installed jdk17, this is set as the default. To see all available versions,
archlinux-java status
I set the latest java
as default
sudo archlinux-java set java-23-openjdk
Install maven
to have the java
language server. Maven is used for managing
dependencies and building projects.
sudo pacman -S maven
maven
requires jdk18 and upwards.
Then install the java-language-server
in nvim
's Mason
.