- How is Java program created/built/executed?
- Create Hello.java.
- Create class Hello.
- Inside the Hello class, create a static method main with the signature:
- public static void main(String[] args)
- Inside the main method, use System.out.println("Hello world!"); to write to standard output.
- Use javac Hello.java to create Hello.class.
- Use java Hello to run Hello.class, notice no .class extension when using java to execute a class.
- Write a Java program that defines/uses package.
- Assume current directory is hello.
- mkdir -p a/b/c && cd a/b/c
- Inside a/b/c, create a file Add.java.
- Inside Add.java file, declare the package it belongs to:
- package a.b.c;
- Inside Add.java file, create a public class Add.
- Inside class Add, create a public static method add with the signature:
- public static int add(int a, int b)
- Go back to hello directory: cd hello.
- Create a file AddTest.java.
- Inside the AddTest.java file, import class Add from package a.b.c:
- import a.b.c.Add;
- Inside the AddTest.java file, create a public static main method with the signature:
- public static void main(String[] args)
- The main method:
- Read arguments from the command line.
- Use Integer.parseInt to convert them to int.
- Call Add.add to compute the sum.
- Call System.out.println to print the result to standard output.
- In directory hello, compile the AddTest.java file by javac AddTest.java, it generates AddTest.class.
- Run the program: java AddTest 1 2, should see the result 3 printed to the standard output.
- Java's member access rule:
Modifier | Class | Package | Subclass | World ————————————+———————+—————————+——————————+——————— public | y | y | y | y ————————————+———————+—————————+——————————+——————— protected | y | y | y | n ————————————+———————+—————————+——————————+——————— no modifier | y | y | n | n ————————————+———————+—————————+——————————+——————— private | y | n | n | n y: accessible n: not accessible
- Download the library .jar file x to folder p.
- In the `Package Explorer', find libs folder (if not exists, create one: New -> Folder).
- Right click libs, Import -> General -> File System.
- Navigate to folder p, find x in the right panel. Check the check-box. Click Finish.
- If libs is already in the build path, we are done. Otherwise, Right click the project, Build Path -> Configure Build Path..., select Libraries tab. Click Add JARs..., find the x, click ok.
- How to setup jni? (TODO)
- How to call c/c++ procedure from Java through jni? (TODO)
- How to call Java procedure from c/c++ through jni? (TODO)