Java programming with linux

«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:

Читайте также:  Создать iso файл linux

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).

Читайте также:  Kali linux проверить wifi адаптер

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.

Читайте также:  Firewall для сервера linux

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.

Источник

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.

You will use the Java Platform, Standard Edition Development Kit (JDK 8).

[username:~/] sudo add-apt-repository ppa:webupd8team/java [username:~/] sudo apt-get update [username:~/] sudo apt-get install oracle-java8-installer [username:~/] sudo apt-get install oracle-java8-set-default 

If you use another Linux distribution, use that distribution’s package manager (see the first Q+A under Troubleshooting).

[username:~/] javac -version javac 1.8.0_144

It’s important that you see the number 1.8 (or later) for the Java version number, but the rest is not critical.

[username:~/] java -version java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

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.

[username:~/] cd /usr/local [username:/usr/local] sudo mkdir introcs [username:/usr/local] sudo chmod 755 introcs 
[username:/usr/local] cd introcs [username:/usr/local/introcs] pwd /usr/local/introcs
[username:/usr/local/introcs] sudo wget http://introcs.cs.princeton.edu/java/code/stdlib.jar [username:/usr/local/introcs] sudo wget http://introcs.cs.princeton.edu/java/linux/javac-introcs [username:/usr/local/introcs] sudo wget http://introcs.cs.princeton.edu/java/linux/java-introcs [username:/usr/local/introcs] sudo chmod 755 javac-introcs java-introcs [username:/usr/local/introcs] sudo mv javac-introcs /usr/local/bin [username:/usr/local/introcs] sudo mv java-introcs /usr/local/bin 

The command wget downloads files from the web; if your system doesn’t have wget, try curl -O instead.

[username:/usr/local/introcs] sudo wget http://introcs.cs.princeton.edu/java/linux/drjava.jar [username:/usr/local/introcs] sudo wget http://introcs.cs.princeton.edu/java/linux/drjava [username:/usr/local/introcs] sudo wget http://introcs.cs.princeton.edu/java/linux/.drjava [username:/usr/local/introcs] sudo chmod 755 drjava [username:/usr/local/introcs] sudo mv drjava /usr/local/bin [username:/usr/local/introcs] sudo mv .drjava ~ 
[username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/checkstyle.zip [username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/checkstyle-introcs.xml [username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/checkstyle-suppressions.xml [username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/checkstyle-introcs [username:/usr/local/introcs/] sudo unzip checkstyle.zip [username:/usr/local/introcs/] sudo chmod 755 checkstyle-introcs [username:/usr/local/introcs/] sudo mv checkstyle-introcs /usr/local/bin 
[username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/findbugs.zip [username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/findbugs.xml [username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/findbugs-introcs [username:/usr/local/introcs/] sudo unzip findbugs.zip [username:/usr/local/introcs/] sudo chmod 755 findbugs-introcs [username:/usr/local/introcs/] sudo mv findbugs-introcs /usr/local/bin 

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.

Источник

Оцените статью
Adblock
detector