Blog Archive

Wednesday, March 7, 2012

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

http://www.thegeekstuff.com/2010/02/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

Create the helloworld.java program using a Vim editor as shown below.
$ 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

Make sure javac is installed on your system as shown below.
$ 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)

Execute helloworld.class as shown below.
$ java helloworld
Hello World!


5. If you code give something like the following, it means your $CLASSPATH may be assigned to some other value.  Then just run: export CLASSPATH=$CLASSPATH:`pwd`; re-run, it should be fixed.


Exception in thread "main" java.lang.NoClassDefFoundError: helloworld
Caused by: java.lang.ClassNotFoundException: helloworld
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: helloworld. Program will exit.

Tuesday, March 6, 2012

数据结构与算法分析 学习笔记

数据结构与算法分析 学习笔记:

'via Blog this'

Java Programming Cheatsheet

http://introcs.cs.princeton.edu/java/11cheatsheet/

Appendix D: Java Programming Cheatsheet


This appendix summarizes the most commonly-used Java language features in the textbook. Here are the APIs of the most common libraries.


Hello, World.

Hello, World in Java


Editing, compiling, and executing.

Compiling Hello, World in Java


Built-in data types.

Built-in types of data


Declaration and assignment statements.

Assignment statements


Integers.

int data type

Integer expressions


Floating-point numbers.

double data type

double expressions


Booleans.

boolean data type

Boolean operators


Comparison operators.

Comparison operators

Comparison examples


Parsing command-line arguments.

parsing Command-line arguments


Math library.

Math library API

Expressions that use Java library methods
The full Math API.


Type conversion.

Type conversion


If and if-else statements.

If-else statements


Nested if-else statement.

Nested if-else statements in Java


While and for loops.

While loop For loop


While and for loops in Java


Break statement.

Break statement in Java


Do-while loop.

Do-while loop in Java


Switch statement.

Switch statement in Java


Arrays.

An array

Compile-time initialization.

Compile-time initialization of arrays
Typical array-processing code.
Typical array-processing code

Two-dimensional arrays.

2D array
Compile-time initialization.
2D array compile-time initialization
Ragged arrays.
Ragged arrays


Our standard output library.

Standard output API
The full StdOut API.
Anatomy of printf
Formatting codes for printf


Our standard input library.

Standard input API
The full StdIn API.


Our standard drawing library.

Standard drawing API
The full StdDraw API.


Our standard audio library.

Standard audio API
The full StdAudio API.


Redirection and piping.

Redirecting standard output Redirecting standard input


Piping


Functions.

Anatomy of a function

Example functions


Libraries of functions.

Library abstraction


Our standard random library.

Standard random


Our standard statistics library.

Standard statistics


Using an object.

Using an object


Creating an object.

Instance variables.

Anatomy of instance variables

Constructors.

Anatomy of a constructor

Instance methods.

Anatomy of an instance method


Classes.

Anatomy of a class


Object-oriented libraries.

Object-oriented library abstraction

Java's String data type.

String library API
The full String API.
String operations

Java's Color data type.

Color library API
The full Color API.


Our input library.

Input API
The full In API.


Our output library.

Output API
The full Out API.


Our picture library.

Picture API
The full Picture API.


Compile-time and run-time errors.

Here's a list of errors compiled by Mordechai Ben-Ari. It includes a list of common error message and typical mistakes that give rise to them.