- How To Compile and Run Java Program in Linux
- How to Compile Java Program
- How to Run Java Program
- Hello World in Java on Linux
- Starting Java Programming in Linux Mint
- The Tools
- Installing Tools
- Configuring Tools
- Start Programming
- Building and Running
- How to install Java on Linux | Opensource.com
- Embrace Java applications on your desktop, and run them on all of your desktops.
- Subscribe now
- Do I need a JVM or a JRE or a JDK?
- What’s the difference between OpenJDK, IcedTea, and OracleJDK?
- Which Java should I install?
How To Compile and Run Java Program in Linux
Today I’ll explain how to compile and run Java code on Linux environment.
Personally, I like Ubuntu, that’s why I’ll show you how to do it there.
First of all, you have to be sure that Java is installed on your machine.
Correct output is:
If it’s not installed you can do it easy:
Now change directory to your project source folder.
Inside of my project I have a class Car, that I wrote when I talked about the transient keyword in Java.
It looks like this:
How to Compile Java Program
To compile this class I call Linux Java compiler:
If everything was OK you should find a new file Car.class near Car.java.
Our class is compiled, the next step is to run compiled Java class on Ubuntu.
How to Run Java Program
In the previous section, I compiled Java code in Linux.
Now I’m going to show how to run Java program.
Just point Java to compiled class like this:
Note that you should run Java class from the source folder.
For example, if I change directory to project (one level upper than src folder).
and try to run the same class:
So you have to be sure that you’re running your Java code on Ubuntu from the source folder.
For compiling process it doesn’t matter.
As you can see it’s easy to compile and run Java on Linux.
Personally, I prefer Java programming on Linux instead of Windows.
I think Java developers should know at least basic Linux commands.
Источник
Hello World in Java on Linux
This DrJava-based Java programming environment is no longer being supported (because DrJava in no longer being actively developed and DrJava is incompatible with Java 11). It has been replaced by the following IntelliJ-based programming environment for Linux.
This document instructs you on how to setup a Java programming environment under Linux. It also provides a step-by-step guide for creating, compiling, and executing your first Java program using either DrJava or the command line. We assume some familiarity with the command line. All of the software is freely available on the web.
0. Install Java |
You will use the Java Platform, Standard Edition Development Kit (JDK 8).
Log in to the user account in which you will be programming. Your account must have Administrator privileges and you must be connected to the Internet.
Launch your shell. We’ll assume that the command prompt looks like the following (though yours will likely differ):
/ is shorthand for your home directory.
Install the Java Platform, Standard Edition Development Kit (JDK 8), either from Oracle or OpenJDK. Most Linux distributions provide their own mechanism for installing software. For example, here are the commands on Ubuntu for Oracle JDK 8:
To confirm that the Java compiler is installed, type the command in boldface below and check that the results match:
To confirm that the Java interpreter is installed, type the command in boldface below and check that the results match:
1. Install a Programming Environment |
These steps install and configures our textbook libraries, DrJava, Checkstyle, and Findbugs. If you do not want DrJava, Checkstyle, or Findbugs, simply skip the corresponding step.
Create a directory /usr/local/introcs.
Download the textbook libraries from stdlib.jar and the Java wrapper scripts from javac-introcs and java-introcs.
Download DrJava from drjava.jar, the wrapper script from drjava, and the configuration file from .drjava.
Download Checkstyle 8.2 from checkstyle.zip; our Checkstyle configuration file from checkstyle-introcs.xml; and the Checkstyle wrapper script from from checkstyle-introcs
Download Findbugs 3.0.1 from findbugs.zip; our Findbugs configuration file from findbugs.xml; and the Findbugs wrapper script from findbugs-introcs.
2. Create a Program in DrJava |
Now you are ready to write your first Java program. You will develop your Java programs in an application called DrJava. DrJava features many specialized programming tools including syntax highlighting, bracket matching, auto indenting, and line numbering.
If you use a file manager such as Konqueror or Nautilus, you can launch DrJava by double-clicking the drjava.jar file. Otherwise, launch DrJava from the command line by typing:
If you copied the .drjava file to your home directory, these customizations should already be included:
Display line numbers by selecting Edit -> Preferences -> Display Options -> Show All Line Numbers.
Set the indentation level to 4 by selecting Edit -> Preferences -> Miscellaneous -> Indent Level -> 4.
Set the Java classpath by selecting Edit -> Preferences -> Resources -> Extra Classpath -> Add and add the following entry:
In the main DrJava window, type the Java program HelloWorld.java exactly as it appears below. If you omit even a semicolon, the program won’t work.
3. Compile a Program from DrJava |
It is now time to convert your Java program into a form more amenable for execution on a computer. To do this, click the Compile button. If all goes well, you should see the following message in the Compiler Output pane at the bottom:
If DrJava complains in some way, you mistyped something. Check your program carefully, using the error messages in the Compiler Output pane as a guide.
4. Execute a Program from DrJava |
Now it is time to run your program. This is the fun part.
Type the following in the Interactions pane at the bottom. By convention, we highlight the text you type in bold.
5. Compile a Program from the Shell |
You will use the javac command to convert your Java program into a form more amenable for execution on a computer.
From the shell, navigate to the directory containing HelloWorld.java, say
/hello, by typing the cd (change directory) commands below:
6. Execute a Program from the Shell |
You will use the java command to execute your program.
- From the shell, type the java command below.
To make our textbook libraries accessible to Java, use the command java-introcs instead. For example, type the following two commands to test standard drawing and standard audio:
7. Checkstyle and Findbugs |
You can use Checkstyle and Findbugs to check the style of your programs and identify common bugs.
To run Checkstyle, type the following command in the Terminal:
To run Findbugs, type the following command in the Terminal:
Troubleshooting |
My distribution of Linux is < Gentoo, Debian, Ubuntu, Fedora, Red Hat, SuSE, Mandriva, or Slackware >. How should I modify the instructions? We haven’t tested out these instructions on all flavors of Linux, but the instructions should be identical except for installing Java. We recommend using your distribution’s package manager (such as portage, apt, emerge, or yum) to install Java. Here are some instructions for installing OpenJDK.
Can I use a different version of Java? Any version of Java 8 (either Oracle or OpenJDK) or later should work fine.
I had to manually enter the location of tools.jar in DrJava, but it doesn’t seem to have any effect. Any suggestions? This setting doesn’t take effect until you restart DrJava.
Can I use a different IDE? Yes, feel free to use another IDE (such as Eclipse) but you will have to configure the IDE properties yourself (such as the classpath).
How do I determine which shell I’m running? Type the following command:
How can I check that /usr/local/bin is in my PATH? From the shell, type the following command:
How can I add /usr/local/bin to my PATH? It depends significantly on your Linux distribution, shell, and user configuration. In the bash shell, this generally means adding the following line to either your .bash_profile, .profile, or .bashrc file. For other common shells, this could mean updating your .cshrc, .tcshrc, or .zcshrc files.
When I compile or execute a program from the shell that uses one of the textbook libraries, I get an error. How can I fix this? First, make sure that you are using the javac-introcs and java-introcs wrapper scripts. Next, verify that you have the file /usr/local/introcs/stdlib.jar. Reset Interactions; from the shell, type Ctrl-c.
When using standard input, how do I signify that there is no more data? If you are entering input from the keyboard, type Ctrl-d for EOF (end of file) from either DrJava or the shell. —>
Last modified on August 14, 2019.
Copyright © 2000–2019 Robert Sedgewick and Kevin Wayne. All rights reserved.
Источник
Starting Java Programming in Linux Mint
This tutorial gives a brief example how to install and setup things to develop Java on Linux Mint system. We will use Geany as the text editor and OpenJDK as the tools. You will also try to create first Java program and run it. I intended this tutorial to help students and beginners who use GNU/Linux with their first days in learning Java. Happy coding!
The Tools
Installing Tools
$ sudo apt-get install geany default-jdk
Configuring Tools
No configuration needed in Geany. It automatically recognizes that your typed source code is «Java».
Start Programming
Here’s the traditional Hello world program. Save this as program.java. Notice the file name must be the same as class name.
public class program <
public static void main(String[] args) <
System.out.println(«Hello world!»);
>
>
How the source code looks like in Geany |
Building and Running
Press Compile button, then press Run button. If your source code is OK, it prints out text «Hello world!» in a new terminal.
Источник
How to install Java on Linux | Opensource.com
Embrace Java applications on your desktop, and run them on all of your desktops.
Subscribe now
Get the highlights in your inbox every week.
No matter what operating system you’re running, there are usually several ways to install an application. Sometimes you might find an application in an app store, or you might install it with a package manager like DNF on Fedora or Brew on Mac, and other times, you might download an executable or an installer from a website. Because Java is such a popular backend for so many applications, it’s good to understand the different ways you can install it. The good news is that you have many options, and this article covers them all.
The bad news is that Java is big, not so much in size as in scope. Java is an open source language and specification, meaning that anyone can, in theory, create an implementation of it. That means, before you can install anything, you have to decide which Java you want to install.
Do I need a JVM or a JRE or a JDK?
Java is broadly split into two downloadable categories. The Java Virtual Machine (JVM) is a runtime component; it’s the «engine» that enables Java applications to launch and run on your computer. It’s included in the Java Runtime Environment (JRE).
The Java Development Kit (JDK) is a development toolkit: you can think of it as a garage where tinkerers sit around making adjustments, repairs, and improvements. The JDK includes the Java Runtime Environment (JRE).
In terms of downloads, this translates to:
- If you’re a user looking to run a Java application, you only need the JRE (which includes a JVM).
- If you’re a developer looking to program in Java, you need the JDK (which includes JRE libraries, which in turn includes a JVM).
What’s the difference between OpenJDK, IcedTea, and OracleJDK?
When Sun Microsystems was bought by Oracle, Java was a major part of the sale. Luckily, Java is an open source technology, so if you’re not happy with the way Oracle maintains the project, you have other options. Oracle bundles proprietary components with its Java downloads, while the OpenJDK project is fully open source.
The IcedTea project is essentially OpenJDK, but its goal is to make it easier for users to build and deploy OpenJDK when using fully free and open source tools.
Which Java should I install?
If you feel overwhelmed by the choices, then the easy answer of which Java implementation you should install is whichever is easiest for you to install. When an application tells you that you need Java 12, but your repository only has Java 8, it’s fine to install whatever implementation of Java 12 you can find from a reliable source. On Linux, you can have several different versions of Java installed all at once, and they won’t interfere with one another.
Источник