How to use any terminal to compile and run Java source code.
What You Need
Java Source Code
You will need to write your Java code and save it with the .java
extension.
Java Compiler
To check if you have one type javac --version
into the terminal. Also, check that the version of javac
and that of java --version
are identical. If not you may get the following error:
Error: A JNI error has occurred, please check your installation and try again Exception in
thread "main" java.lang.UnsupportedClassVersionError: MyList has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java
Runtime only recognizes class file versions up to 52.0
Use sudo update-alternatives --config java
to pick the correct version.
Steps to Compile and Run the code
-
Open a terminal in the same directory as your
.java
file. -
Type
javac filename.java
, this will generate.class
files. -
Type
java filename
(without extension), this should run your code.