Java programing on linux

A Step-by-Step Guide on Running Java Programs in the Terminal on Linux

Learn how to run Java programs in the terminal on Ubuntu and other Linux distributions. Follow our step-by-step guide to install and verify JDK, compile and execute Java programs, run command-line Java programs, and enable and view Java Console on Linux or Solaris.

  • Installing and Verifying JDK Version in Linux
  • Compiling and Executing Java Programs in Terminal
  • How to Run Java Program in Terminal Ubuntu Linux
  • Running Command-Line Java Programs on Linux
  • Enabling and Viewing Java Console on Linux or Solaris
  • Other helpful code examples for running Java programs in the terminal on Linux
  • Conclusion
  • How to run java in terminal Linux?
  • How to run java on terminal?
  • How to run a java program in Linux?
  • How to run java in terminal Ubuntu?

Java is a popular programming language that is widely used for web development, mobile development, and app development. One of the main advantages of Java is its cross-platform compatibility, which means that Java programs can be executed on different operating systems, including Windows, Mac, and Linux. In this guide, we will provide a step-by-step guide on how to run Java programs in the terminal on Ubuntu and other Linux distributions. We will cover the installation and verification of the Java Development Kit (JDK), the compilation and execution of Java programs in the terminal, running command-line Java programs on Linux, and enabling and viewing the Java Console on Linux or Solaris.

Installing and Verifying JDK Version in Linux

Before running java programs on ubuntu or any other Linux distribution, you need to install the Java Development Kit (JDK). The JDK is a software development environment that provides tools to develop, debug, and run Java programs. The following steps show you how to install and verify the JDK version in Linux:

  1. Open the terminal on Ubuntu or any other Linux distribution.
  2. Type the following command to install the JDK:
sudo apt-get install openjdk-8-jdk 

Compiling and Executing Java Programs in Terminal

After installing the JDK, you can start writing Java programs and running them on Ubuntu or any other Linux distribution. Here are the steps to compile and execute Java programs in the terminal:

  1. Open the terminal on Ubuntu or any other Linux distribution.
  2. Create a new file with the “.java” extension and write your Java program.

How to Run Java Program in Terminal Ubuntu Linux

In this video I am going to show you How to install Java JDK 10 on Ubuntu Linux ( with Duration: 11:41

Running Command-Line Java Programs on Linux

In addition to running java programs in the terminal on Ubuntu or any other Linux distribution, you can also run command-line Java programs. Here is how to do it:

  1. Open the terminal on Ubuntu or any other Linux distribution.
  2. Write your Java program and save it with the “.java” extension.
import java.io.*;public class CommandLine < public static void main(String[] args) < try < Process p = Runtime.getRuntime().exec("ls -l"); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while ((line = in.readLine()) != null) < System.out.println(line); >> catch (IOException e) < e.printStackTrace(); >> > 

Enabling and Viewing Java Console on Linux or Solaris

The Java Console is a debugging tool that allows you to view and debug Java applets and applications. Here are the steps to enable and view the Java Console on Linux or Solaris:

  1. Open the terminal on Ubuntu or any other Linux distribution.
  2. Navigate to the Java installation directory.
cd /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/ 

Other helpful code examples for running Java programs in the terminal on Linux

In Java case in point, java compiler linux terminal code example

sudo apt install default-jdk

In Java case in point, java run linux command code sample

Process p = Runtime.getRuntime().exec("ls -la /home"); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getOutputStream())); String result; while ((result = stdInput.readLine()) != null)

In Java , for instance, run java from terminal

//Run this line to compile javac programName.java//Run this line to run java programName

In Java , for instance, run java from terminal linux

//To compile, type javac filename.java//To run the generated class file, use java filename

In Java , for example, how to run a java file in terminal code sample

javac FirstJavaProgram.java

Conclusion

In this guide, we have provided a step-by-step guide on how to run Java programs in the terminal on Ubuntu and other Linux distributions. We covered the installation and verification of the Java Development Kit (JDK), the compilation and execution of Java programs in the terminal, running command-line Java programs on Linux, and enabling and viewing the Java Console on Linux or Solaris. By following these steps, you can easily run Java programs on Ubuntu or any other Linux distribution, and take advantage of the cross-platform compatibility of the Java language.

Источник

«Hello World!» for Solaris OS, Linux, and Mac OS X

It’s time to write your first application! These detailed instructions are for users of Solaris OS, Linux, and Mac OS X. Instructions for other platforms are in «Hello World!» for Microsoft Windows and «Hello World!» for the NetBeans IDE.

A Checklist

To write your first program, you’ll need:

  1. The Java SE Development Kit 8 (JDK 8) You can download the version for Solaris OS, Linux, or Mac OS X. (Make sure you download the JDK, not the JRE.) Consult the installation instructions.
  2. A text editor In this example, we’ll use Pico, an editor available for many UNIX-based platforms. You can easily adapt these instructions if you use a different text editor, such as vi or emacs .

These two items are all you’ll need to write your first application.

Creating Your First Application

Your first application, HelloWorldApp , will simply display the greeting «Hello world!». To create this program, you will:

  • Create a source file A source file contains code, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files.
  • Compile the source file into a .class file The Java programming language compiler ( javac ) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this .class file are known as bytecodes.
  • Run the program The Java application launcher tool ( java ) uses the Java virtual machine to run your application.

Create a Source File

To create a source file, you have two options:

  • You can save the file HelloWorldApp.java on your computer and avoid a lot of typing. Then, you can go straight to Compile the Source File.
  • Or, you can use the following (longer) instructions.

First, open a shell, or «terminal,» window.

When you first bring up the prompt, your current directory will usually be your home directory. You can change your current directory to your home directory at any time by typing cd at the prompt and then pressing Return.

The source files you create should be kept in a separate directory. You can create a directory by using the command mkdir . For example, to create the directory examples/java in the /tmp directory, use the following commands:

cd /tmp mkdir examples cd examples mkdir java

To change your current directory to this new directory, you then enter:

Now you can start creating your source file.

Start the Pico editor by typing pico at the prompt and pressing Return. If the system responds with the message pico: command not found , then Pico is most likely unavailable. Consult your system administrator for more information, or use another editor.

When you start Pico, it’ll display a new, blank buffer. This is the area in which you will type your code.

Type the following code into the new buffer:

/** * The HelloWorldApp class implements an application that * simply prints "Hello World!" to standard output. */ class HelloWorldApp < public static void main(String[] args) < System.out.println("Hello World!"); // Display the string. >>

Be Careful When You Type

Note: Type all code, commands, and file names exactly as shown. Both the compiler ( javac ) and launcher ( java ) are case-sensitive, so you must capitalize consistently.

HelloWorldApp is not the same as helloworldapp .

Save the code in a file with the name HelloWorldApp.java . In the Pico editor, you do this by typing Ctrl-O and then, at the bottom where you see the prompt File Name to write: , entering the directory in which you wish to create the file, followed by HelloWorldApp.java . For example, if you wish to save HelloWorldApp.java in the directory /tmp/examples/java , then you type /tmp/examples/java/HelloWorldApp.java and press Return.

You can type Ctrl-X to exit Pico.

Compile the Source File into a .class File

Bring up another shell window. To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is /tmp/examples/java , type the following command at the prompt and press Return:

If you enter pwd at the prompt, you should see the current directory, which in this example has been changed to /tmp/examples/java .

If you enter ls at the prompt, you should see your file.

Results of the ls command, showing the .java source file.

Now are ready to compile the source file. At the prompt, type the following command and press Return.

The compiler has generated a bytecode file, HelloWorldApp.class . At the prompt, type ls to see the new file that was generated: the following figure .

Results of the ls command, showing the generated .class file.

Now that you have a .class file, you can run your program.

If you encounter problems with the instructions in this step, consult the Common Problems (and Their Solutions).

Run the Program

In the same directory, enter at the prompt:

The next figure shows what you should now see.

The output prints «Hello World!» to the screen.

Congratulations! Your program works!

If you encounter problems with the instructions in this step, consult the Common Problems (and Their Solutions).

Previous page: «Hello World!» for Microsoft Windows
Next page: A Closer Look at the «Hello World!» Application

Источник

How to Run Java Programs in Ubuntu

So, you have started learning Java programming? That’s good.

And you want to run the java programs on your Linux system? Even better.

Let me show how to run Java in terminal in Ubuntu and other Linux distributions.

Running Java programs in Ubuntu

Let’s go in proper steps here.

Step 1: Install Java compiler

To run a Java program, you need to compile the program first. You need Java compiler for this purpose.

The Java compiler is part of JDK (Java Development Kit). You need to install JDK in order to compile and run Java programs.

First, check if you already have Java Compiler installed on your system:

If you see an error like “Command ‘javac’ not found, but can be installed with”, this means you need to install Java Development Kit.

java compiler check ubuntu

The simplest way to install JDK on Ubuntu is to go with the default offering from Ubuntu:

sudo apt install default-jdk

You’ll be asked to enter your account’s password. When you type the password, nothing is seen on the screen. That is normal. Just enter your password blindly. When asked, press the enter key or Y key.

install jdk ubuntu

The above command should work for other Debian and Ubuntu based distributions like Linux Mint, elementary OS etc. For other distributions, use your distribution’s package manager. The package name could also be different.

Once installed, verify that javac is available now.

java compiler ubuntu

Step 2: Compile Java program in Linux

You need to have a Java program file for this reason. Let’s say you create a new Java program file named HelloWorld.java and it has the following content:

You can use Nano editor in terminal or Gedit graphical text editor for writing your Java programs.

If there is no error, the above command produces no output.

When you compile the Java program, it generates a .class file with the class name you used in your program. You have to run this class file.

Step 3: Run the Java class file

You do not need to specify the class extension here. Just the name of the class. And this time, you use the command java, not javac.

This will print Hello World on the screen for my program.

running java programs in linux terminal

And that’s how you run a Java program in the Linux terminal.

This was the simplest of the example. The sample program had just one class. The Java compiler creates a class file for each class in your program. Things get complicated for bigger programs and projects.

This is why I advise installing Eclipse on Ubuntu for proper Java programming. It is easier to program in an IDE.

I hope you find this tutorial helpful. Questions or suggestions? The comment section is all yours.

Источник

Читайте также:  Astra linux открыть порт 3389
Оцените статью
Adblock
detector