Home | Login and Logout | Basic Command Line Commands | File System | File Editing Through Terminal | Java Programs | Input/Output Redirection | Grep | Works Cited

Why does Java work on Linux?

You may have noticed that whenever you compile a java class, a new file is created: a .class file. What's happening is that java is creating a bytecode file (.class) from your source file (.java) when it compiles the source file. This bytecode file helps the java program run on any platform, including Linux, by using the java virtual machine to execute it.

The Source File
.java example

The source file (.java) is the file which contains all the java source code. This is the file programmers edit whenever they modify a java program.

The Bytecode File
The bytecode file (.class) is the compiled version of the java source file. Whenever we compile a java source file, we turn the code we've written into something that is understood by the java virtual machine. This means the bytecode is usually just binary instructions for the java virtual machine to execute, which is not readable by humans. However, compiling the source file into a bytecode file ensures that the java program in our source code can be run on any platform once it has been compiled into a bytecode file since it uses the java virtual machine to execute it.

Writing and Editing Programs Using the Desktop Environment

Using gedit
To use gedit, you must right click on your source file, hover over "Open With", and select "gedit". Once you have opened your file, write it just as you would any other normal java file. Once you are done writing your program, save it by going to the upper left corner of the screen and hovering over "File" and selecting the "Save" or "Save As" option. To compile your code, you will need to use the terminal, which will be discussed later in the compiling section of the tutorial.


Using Eclipse
Eclipse can also be used to write and edit java programs. To open up Eclipse, use linux's search button on the upper left to search for Eclipse by typing "Eclipse".


Click on the Eclipse icon and write your java programs as you normally would on Windows or Mac. You can also compile and run your files using Eclipse just as you would on Windows or Mac, so you don't need to use the terminal.

Compiling a File

To compile a file in java, you must first head to its directory in the terminal. This is easily done by heading to the directory in the file explorer and right clicking and selecting the option "Open in Terminal".

Before Compile

After you open the terminal, to compile your source file, simply type in "javac [filename].java" where filename is the name of your source file. By doing this, you will generate a .class file (bytecode file) which will be used to run your program on the java virtual machine.

After Compile

Running a Java Program

To run a java program, you head to the directory of the file in the terminal. When you are in its directory, to run the program type "java [filename]" where filename is the name of your bytecode file (.class). This should run the program in the terminal.

help pic 2

(Sedgewick and Wayne, 2017)

Written by: Daniel Amusin, Derek DeCramer, Nikhil Thammadi