Java start
Use in DCU (Win and Linux)
- Java is already installed on your DCU machine.
- To see version:
$ java -version
- You can compile and run programs
on the command-line.
Use in DCU (Win)
Use on your own machine
If you want to install on your own machine:
- Download Java Development Kit (JDK)
- JDK install instructions
If you are on Windows:
- Just run the downloaded EXE to install.
- Add this to PATH:
C:\Program Files\Java\VERSION\bin
To change
PATH on Win
- PATH is a list of directories separated by ";"
- To change
PATH on Win:
- Change system-level environment variable:
My Computer - Properties - Advanced - Environment Variables
PATH (directories);C:\Program Files\Java\VERSION\bin
- If this is restricted (as at DCU), change user-level environment variable:
PATH C:\Program Files\Java\VERSION\bin
and this gets appended to system-level PATH.
- If all else fails, change PATH at command-line:
path=%path%;C:\Program Files\Java\VERSION\bin
- You can put the previous line in a
.BAT file
called SETPATH.BAT.
Then to set the path you just type
SETPATH.
To write a Java program
What to write code in
Any text editor will do,
but you may prefer an
IDE.
Some of these installed at DCU.
Some not.
- Text editors (Win):
- Text editors (Linux):
- Java development environments (Win):
- Java development environments (Linux):
"Hello World" program
public class Hello
{
public static void main ( String[] args )
{
System.out.println("Hello World"); // comment
for ( int i=0; i < args.length; i++ )
{ System.out.println ( args[i] ); }
}
}
- Get it working:
- Save as Hello.java
- file name = class name
- case matters
- the main() method accepts
command-line arguments
- run it as:
$ java Hello arg1 arg2
- Notes:
- case matters
- System.out
- println
-
command-line arguments come in as
a single
array
of elements of type
String
- array is indexed args[0] .. args[n-1]
where n = args.length
- For meaning of "public" read here:
- For meaning of "static" read here:
- Getting started
Books
- Java in a Nutshell,
David Flanagan, 2005.
- Library section - 005.13 - Programming languages