Page 1 of 3 123 LastLast
Results 1 to 10 of 30

Thread: FAQ: Compiling and Running Java Programs

  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.

  2. #2
    Join Date
    Oct 2007
    Location
    Maine
    Beans
    86
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: FAQ: Compiling and Running Java Programs

    Wouldn't it be this?

    Code:
    javac Main.java
    Whenever I compile a file without the extension the compiler spits at me.

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

    Re: FAQ: Compiling and Running Java Programs

    Quote Originally Posted by petzworld999 View Post
    Wouldn't it be this?

    Whenever I compile a file without the extension the compiler spits at me.
    Quite right, thanks. It has been a while...

  4. #4
    Join Date
    Jun 2006
    Location
    Gwangju, Korea
    Beans
    3,479

    Re: FAQ: Compiling and Running Java Programs

    Just a small suggestion: Blue text looks like a link (even if the site in question doesn't use blue links). Changing your text highlight color to something other than blue would improve the usability of the page. (useit.com has some info on this, though I don't have the exact link handy.)

  5. #5
    Join Date
    Dec 2006
    Beans
    92

    Re: FAQ: Compiling and Running Java Programs

    Thanks!!! This would be very good for those who are beginning with programming! ^_^

  6. #6
    Join Date
    Dec 2007
    Location
    El Dorado
    Beans
    170
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: FAQ: Compiling and Running Java Programs

    One more thing:
    In order to set Sun Java SDK's tools as the default for your Ubuntu, run the following command:
    sudo update-java-alternatives --set java-6-sun

  7. #7

    Re: FAQ: Compiling and Running Java Programs

    Thanks!
    Page one instructions worked for me - no probs!
    I got another question: Does Linux(Ubuntu) have it's
    developing tool like DrJava or something similar?

    Thanks.

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

    Re: FAQ: Compiling and Running Java Programs

    Quote Originally Posted by Vladislav Mkrtychev View Post
    Thanks!
    Page one instructions worked for me - no probs!
    I got another question: Does Linux(Ubuntu) have it's
    developing tool like DrJava or something similar?

    Thanks.
    http://drjava.org/

    It works on Linux.

  9. #9
    Join Date
    Feb 2007
    Location
    Heaven
    Beans
    486
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: FAQ: Compiling and Running Java Programs

    3. Useful tips
    After the user gets comfortable with compiling and running the hello world app, would be good to let him know that nowadays every modern Java program is inside (let's call it) a package namespace. Anonymous packages (like the one in LaRoza's example) should be avoided since it's considered to be a bad habit (more precisely since JDK 1.4). This is especially true when sharing/using third-party libraries to avoid class name collisions.
    Since this is probably gonna scare/confuse the user, I think the explanation/example should be very short and comprehensive.
    http://en.wikipedia.org/wiki/Java_package
    only shows how to import packages, not how the user can create/compile/run them.

  10. #10
    Join Date
    Aug 2006
    Location
    Pittsburgh, PA
    Beans
    424
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: FAQ: Compiling and Running Java Programs

    You may also want to add links to the major Java APIs these are really helpful and I always have them open anytime I am writing Java.

    Here is the page that leads to the various APIs:
    http://java.sun.com/reference/api/

    Also, java ranch has some great cattle drives for beginers at:
    http://www.javaranch.com/java-college.jsp

    Otherwise it's a pretty good intro to Java.

Page 1 of 3 123 LastLast

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
  •