Results 1 to 10 of 30

Thread: FAQ: Compiling and Running Java Programs

Threaded View

  1. #1
    Join Date
    Apr 2007
    Beans
    14,781

    FAQ: Compiling and Running Java Programs

    Working on the rest, post suggestions as well. I am not skilled or experienced in Java

    So you're willing to write your own Java programs, or simply compile someone else's code? This guide will get you started...

    This FAQ assumes you're trying to compile error-free code. To actually learn the Java languages, see the How to start programming thread.

    0.a. Make sure the third party repositories are enabled

    The easiest way to do this is open Applications->Add/Remove and select "All Available Applications".

    0.b. Make sure the sun-java6-jdk package is installed

    This package installs the essential tools needed to write and run Java programs.

    Code:
    sudo aptitude install sun-java6-jdk
    Also run this to set the Sun Java as the default.
    Code:
    sudo update-java-alternatives --set java-6-sun
    1. Compiling your first Java program

    Copy & paste this code into a new file. Save the file as Main.java
    PHP Code:
    //First Java Program
    class Main
    {
        public static 
    void main (String[] args
       { 
           
    System.out.println ("Hello, world.");
       }

    Open a terminal, go to the directory where you saved Main.java, and type:
    Code:
    javac Main.java
    If all went fine, nothing is printed and you get back to the shell. Now, run it:
    Code:
    java Main
    "Hello World!" gets printed in the terminal.

    Explanations:
    • javac is the Java compiler
    • obviously, Main means that the compiler must compile this file.
    • java Main runs the newly compiled executable.



    2. Paranoid programming is good for your health!

    Many beginners want to avoid compiler errors and warnings at any cost, which often leads them to ignore the "cryptic" messages the compiler outputs and to tinker anxiously with the relevant line until the error goes away.
    Don't fall in that trap! The compiler is your friend, the messages it outputs are kind advices rather than punishments. You should really take the time to try and understand those messages, this is a very good way to improve your code's quality.

    As a matter of fact, most seasoned programmers set the compiler's warning level as high as possible, so that it catches the little errors they make when they aren't paying much attention.

    **Help needed** Post in thread to recommend content or deletion of this section.


    3. Useful tips

    **Help needed** Post in thread to recommend content or deletion of this section.

    4. Read the manual!

    Plain old manpage:
    Code:
    man java
    Naturally, the Documentation and Tutorial on the Sun site is a great place to get answers: http://java.sun.com/docs/books/tutorial/


    Thanks to so many people @UF for their very useful help, this FAQ wouldn't be the same without them. In particular, this thread is based on aks44's thread on C and C++ (see link in sticky). Not only is this thread based on it, it is blatently copying it, I hope he doesn't mind...
    Last edited by LaRoza; April 25th, 2008 at 04:20 AM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •