Java on linux hello world

Java Hello World Example: How To Write and Execute Java Program on Unix OS

Question: I would like to understand the basics of how to write, compile and execute a Java program on UNIX / Linux OS. Can you explain it with a simple example? Answer: In this article, let us review very quickly how to write a basic Hello World Java program and how to compile *.java program on Linux or Unix OS.

1. Write a Hello World Java Program

$ vim helloworld.java /* Hello World Java Program */ class helloworld < public static void main(String[] args) < System.out.println("Hello World!"); >>

2. Make sure Java Compiler (javac) is installed on your system

$ dpkg -l | grep java ii java-common 0.30ubuntu5 Base of all Java packages ii sun-java6-bin 6-16-0ubuntu1.9.04 Sun Java(TM) Runtime Environment (JRE) 6 (ar ii sun-java6-jdk 6-16-0ubuntu1.9.04 Sun Java(TM) Development Kit (JDK) 6 ii sun-java6-jre 6-16-0ubuntu1.9.04 Sun Java(TM) Runtime Environment (JRE) 6 (ar ii sun-java6-plugin 6-16-0ubuntu1.9.04 The Java(TM) Plug-in, Java SE 6 $ whereis javac javac: /usr/bin/javac /usr/share/man/man1/javac.1.gz $ which javac /usr/bin/javac

3. Compile the helloworld.java Program

Compile the helloworld.java using javac command as shown below. This will create the helloworld.class file.

$ javac helloworld.java $ ls -ltr total 1760 -rw-r--r-- 1 ramesh ramesh 149 2010-01-23 09:51 helloworld.java -rw-r--r-- 1 ramesh ramesh 426 2010-01-23 09:52 helloworld.class

4. Execute the Java Class Program (helloworld.class)

$ java helloworld Hello World!

Источник

Java on linux hello world

This document instructs you on how to setup a Java programming environment under the Debian flavor of Linux, and provides a step-by-step guide to creating, compiling, and executing a Java program. Red Hat, SuSE, Mandrake, Debian, Gentoo, and other Linux distros should be similar. All of the software is freely available on the web.

  • Download the Java 2 standard edition J2SE SDK v 1.4.2. Be sure to choose SDK, not the JRE. You don’t need the NetBeans IDE cobundle. Download either the Linux RPM in self-extracting file (if you have root access) or the Linux self-extracting file (if you don’t have root access and want to install for a single user).
  • Install Java by opening up a shell window and enter the following commands into the shell, depending on whether you chose the RPM or single-user installation option. While it’s installing, you might enjoy reading Neal Stephenson’s light-hearted essay In the Beginning was the Command Line.
    • Linux RPM installation. Run the installation program by typing
    chmod +x j2sdk-1_4_2-linux-i586-rpm.bin ./j2sdk-1_4_2-linux-i586-rpm.bin rpm -iv j2sdk-1_4_2-linux-i586.rpm
    chmod +x j2sdk-1_4_2-linux-i586.bin ./j2sdk-1_4_2-linux-i586.bin
      Bash. If you are using the bash shell, append the following line to your .bashrc file. If it doesn’t exist, create a new file.
    export PATH=$PATH:~/j2sdk1.4.2/bin
    setenv PATH ~/j2sdk1.4.2/bin:$PATH
    [bzankel@silver:~/] mkdir introcs [bzankel@silver:~/] cd introcs/ [bzankel@silver:~/] mkdir hello [bzankel@silver:~/introcs] cd hello/ [bzankel@silver:~/introcs/hello]
    • Download and install the latest stable version of JEdit using the Linux RPM package if you have root access or the Java-based installer if you don’t. Install it using the default options.
    • We recommend the following JEdit customizations.
      • Change the default indentation to four spaces via Utilities -> Global Options -> Editing -> Indent Width and Utilities -> Global Options -> Editing -> Tab Width.
      • Change the default tab width when printing to four spaces via Utilities -> Global Options -> Printing -> Tab width when printing
      • Add line numbers by checking Utilities -> Global Options -> Gutter -> Line Numbering
      • Allow at most 80 characters per line by Utilities -> Global Options -> Editing -> Word wrap -> hard and Utilities -> Global Options -> Editing -> Wrap margin -> 80.
      • Change the default line separator to Unix via Utilities -> Global Options -> Loading & Saving -> Default Line Separator
      • Remove the annoying . that marks the end of a line by unchecking Utilities -> Global Options -> Text Area -> End of Line Markers

      It is now time to convert your Java program into a form more amenable for executing on a computer.

        From the command line, type

      [bzankel@silver:~/introcs/hello] javac HelloWorld.java
      [bzankel@silver:~/introcs/hello] javac HelloWorld.java [bzankel@silver:~/introcs/hello]

      Now it is time to run your program. This is the fun part.

      [bzankel@silver:~/introcs/hello] java HelloWorld
      [bzankel@silver:~/introcs/hello] java HelloWorld Hello, World

      How do I configure vi to replace tabs with four spaces? Edit your .exrc file and add the commands «:set ts=4» to display the tabs as four spaces and «:set expandtab» to translate the tabs into spaces.

      Источник

      Lesson: The «Hello World!» Application

      The sections listed below provide detailed instructions for compiling and running a simple «Hello World!» application. The first section provides information on getting started with the NetBeans IDE, an integrated development environment that greatly simplifies the software development process. The NetBeans IDE runs on all of the platforms listed below. The remaining sections provide platform-specific instructions for getting started without an integrated development environment. If you run into problems, be sure to consult the common problems section; it provides solutions for many issues encountered by new users.

      «Hello World!» for the NetBeans IDE These instructions are for users of the NetBeans IDE. The NetBeans IDE runs on the Java platform, which means that you can use it with any operating system for which there is a JDK 7 available. These operating systems include Microsoft Windows, Solaris OS, Linux, and Mac OS X. We recommend using the NetBeans IDE instead of the command line whenever possible.

      «Hello World!» for Microsoft Windows These command-line instructions are for users of Windows XP Professional, Windows XP Home, Windows Server 2003, Windows 2000 Professional, and Windows Vista.

      «Hello World!» for Solaris OS, Linux, and Mac OS X These command-line instructions are for users of Solaris OS, Linux, and Mac OS X. Common Problems (and Their Solutions) Consult this page if you have problems compiling or running your application.

      Источник

      «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

      Источник

      Читайте также:  Linux две вертикальные черты
Оцените статью
Adblock
detector