Меню Рубрики

Linux hello world java

«Hello World!» for Solaris OS and Linux

It’s time to write your first application! These detailed instructions are for users of Solaris OS and Linux. 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:

The Java SE Development Kit 8 (JDK 8)

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.

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.

A new 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:

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:

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

Источник

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

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.

  • For these wrapper scripts to work, it is important that /usr/local/bin is in your PATH environment variable. This is very likely the case. If not, see the Troubleshooting section below.
  • 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.

  • Finally, click the Save button to save the file in a directory of your choice. Name the file HelloWorld.java. The file name is case sensitive and must exactly match the name of the class in the Java program.
  • 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.

  • You may need to repeat this edit–compile–execute cycle a few times before it works.
  • 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:

  • To make our textbook libraries accessible to Java, use the command javac-introcs instead.
  • 6. Execute a Program from the Shell

    You will use the java command to execute your program.

    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:

    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.

    Источник

    Добавить комментарий

    Ваш адрес email не будет опубликован. Обязательные поля помечены *

  • Mac os переустановка с флешки
  • Mac os переустановка без потери данных
  • Mac os перенести связку ключей
  • Mac os переключить раскладку
  • Mac os переключение между окнами одного приложения