Java: Difference between revisions

From 太極
Jump to navigation Jump to search
No edit summary
Line 55: Line 55:
3.0822070014844885
3.0822070014844885
</pre>
</pre>
= Set the Java version for a project =
To change the Java version for a project if a newer version becomes available: right click on a project, then select properties
* Libraries - Java Platform - change the version from the drop-down list. I only see JDK 1.7.
If you want to use the new features of that version and you don't need the project to run under earlier versions of Java.
* Sources - Source/Binary format - change the version from the drop-down list. I can see a lot of choices.


= Swing =
= Swing =

Revision as of 17:08, 21 June 2014

Install openjdk

See http://openjdk.java.net/install/. On Ubuntu, I can use

sudo apt-get install openjdk-7-jdk

Some projects written in Java

  • FastQC The code uses Java 2D graphics APIs in awt like BasicStroke, Color, Dimension, Graphics, Graphics2D, RenderingHints and javax.swing.JPanel. It also uses java.util.Vector.

Tutorial/Books

Compile a simple Java program

javac HelloWorldApp.java # generate Example.class; bytecode version of the program
java HelloWorldApp   # run bytecode in Java Virtual Machine

Get a hello world program from http://docs.oracle.com/javase/tutorial/getStarted/cupojava/unix.html.

Note that the file name can not be arbitrary. It should match with the class name. For the above example, if we rename <HelloWorldApp.java> to <example.java>, we will get an error when we run java example on the command line.

Another example that requires command line argument.

// Sqrt.java
public class Sqrt { 
    public static void main(String[] args) { 

        // read in the command-line argument
        double c = Double.parseDouble(args[0]);
        double epsilon = 1e-15;    // relative error tolerance
        double t = c;              // estimate of the square root of c

        // repeatedly apply Newton update step until desired precision is achieved
        while (Math.abs(t - c/t) > epsilon*t) {
            t = (c/t + t) / 2.0;
        }

        // print out the estimate of the square root of c
        System.out.println(t);
    }

}

$ javac Sqrt.java
$ java Sqrt 9
3.0
$ java Sqrt 9.5
3.0822070014844885

Set the Java version for a project

To change the Java version for a project if a newer version becomes available: right click on a project, then select properties

  • Libraries - Java Platform - change the version from the drop-down list. I only see JDK 1.7.

If you want to use the new features of that version and you don't need the project to run under earlier versions of Java.

  • Sources - Source/Binary format - change the version from the drop-down list. I can see a lot of choices.

Swing

Hello World Example

For example, create a new subdirectory 'start' and put HelloWorldSwing.java there. Then we can build and run the swing program by

javac start/HelloWorldSwing.java # Or javac HelloWorldSwing.java if we are in start directory
java start.HelloWorldSwing

Quit Button Example

Note that it is necessary to create the directory com/zetcode according to package statement in java code. Also the filename must be consistent with the class name.

mkdir com
mkdir com/zetcode
nano com/zetcode/QuitButtonExample.java
javac com/zetcode/QuitButtonExample.java
java com/zetcode/QuitButtonExample