Java Tips for Beginners

Here I list some problems I encountered when using Java and their solutions. I hope to save other beginners the time I spent on them.
Question:
How do I tell Java where to find jar files and classes?
Answer:
1) javac has the -cp option to specify the path to your imported classes
2) A more convenient option are the environment variables. You can set them temporarily or you can specify them in your shell configuration file. In this case, you only need to set them once. See my shell tips for beginners for environment variable setting. Here is an example (for bash):
export CLASSPATH=/Users/karoline/Documents/dev_workspace/org.apache.commons.codec/lib/commons-codec-1.3.jar
This example loads the commons-codec-1.3 library on the java classpath.
Question:
How do I increase Java runtime memory?
Answer:
Use the Java application launcher java with options to set the maximum Java heap size. Example:
java -Xmx512m application The option -Xms512 sets the maximal java heap size to 512 MB.
Question:
How to write an Ant task?
Answer:
Ant is make for Java. With Ant, you can automatize certain tasks, like organising imports, creating jar files for deployment and publishing jar files on update sites.
Particularly useful are the Ant tasks, which allow to run a Java program given the jar file(s), a property file and the Ant build file.
Here is the code for the definition of an example Ant task:
<taskdef name="exampletask" classname="be.ac.exampletaskclasspath.ExampleTask">
  <classpath>
    <path refid="exampletask.classpath" />
  </classpath>
</taskdef>
The classname gives the name of the Java class that implements the task. The classpath gives the location of the jarfiles on which the ant task is depending. The task definition can be used in an Ant buildfile (build.xml) like that:
<target name="launch_exampletask" description="launches the example task">
  <exampletask argx="${exampletask.argx}" argy="${exampletask.argy}"/>
</target>
The property file contains default values for the parameters of the program. These default values can be overridden by the user. The property file "example-default.properties" consists of the following two lines:
exampletask.argx=10
exampletask.argy=25
The Java class implementing the Ant task could look like that:
import org.apache.tools.ant.Task;

/**
* ExampleTask.java
*/
public class ExampleTask extends Task{
private Integer _argx;
private Integer _argy;

public void execute(){
  Integer z = this.getArgX()+this.getArgY();
  System.out.println(z);
}
/**
* @param _argx
*/
public void setArgX(String argx) {
  this._argx = Integer.parseInt(argx);
}
/**
* @return _argx
*/
public Integer getArgX() {
  return _argx;
}
/**
* @param _argy
*/
public void setArgY(String argy) {
  this._argy = Integer.parseInt(argy);
}
/**
* @return _argy
*/
public Integer getArgY() {
  return _argy;
}
}
The Ant task can be called as follows:
ant launch_exampletask -Dexampletask.argx=10 -Dexampletask.argy=20
For more information on Ant see Apache Ant Manual.

Remark
Before you write fancy code, check the Java documentation. Java provides many basic functions, notably set and string operations (intersection, union, regular expression matching), basic mathematical functions and random number generation. If you don't find a certain function in the basic Java language, check whether a library containing that function has been published already. Don't re-invent the wheel.

Eclipse Tips for Beginners

My Top Ten Eclipse shortcuts (Mac OS)
1) Ctrl+Space: auto-completion. One reason why to use Eclipse.
Remark: Auto-completion applied to sysout and syserr gives standard.out.println and standard.err.println respectively.
2) Apple+1: Quickfix (suggestions for error corrections, another reason why to use Eclipse)
3) Alt+Apple+Arrow down: copy selected line
4) Alt+Arrow Up or Arrow Down: move selected line up or down
5) Alt+Apple+J: generate package, class or method comment
6) Apple+I: correct indentation
7) Apple+/: add or remove comment
8) Apple+Shift+O: organize imports
9) Double click on variable name will automatically highlight all occurrences of that variable in the code
10) Ctrl+Shift+Space in a method gives more info on its parameters
Further Eclipse tips:
1) Use "Refactor" to change class, method or variable names or to encapsulate fields.
2) Eclipse contains a debugger. Setting breakpoints (right mouse click) and launching the java program with the debugger (using the "beetle" button) will open the debugger perspective. The debugger perspective allows to inspect variable values at the breakpoint and to go through the program step by step.
3) Eclipse supports JUnit. For Java classes extending JUnit the option Run As... JUnit Test can be chosen. This will open the JUnit perspective, which lists test cases together with their status (successful, failure) and traces failures.
4) Eclipse supports Ant. Ant files can be opened with the Ant editor (Right click -> Open With...). The Ant run configuration can be specified using the button with white triangle on green ground and red symbol -> clicking on black little triangle -> External Tools. Note that specifying the -d argument in Main -> Arguments will run Ant in debug mode.
5) You can configure the Java build path by right-click on Package Explorer View, selecting Build Path and configure Build Path. The "Projects" submenu allows to add Java projects to the build path and the "Libraries" submenu allows to add Jar files.
6) If you add "TODO" to a comment, the text following it will be added to the task list (if not there, go to Window -> Show View and select "Tasks").
7) Double click on a window will enlarge it. Double click on enlarged window will restore its normal size.
8) Manually create jar files: Right-Click on Navigator or Package Explorer, select Export... -> Java -> Jarfile
9) Increase Java Runtime in Eclipse: button with white triangle on green ground -> little black triangle next to it -> Run...
-> (x) arguments: Enter for example -Xms512m -Xmx512m
10) Note that you can customize the comments that are automatically added: Right-Click on project, select properties, select Java Code Style and go to Code templates.
11) You can filter the elements shown in the Package Explorer by clicking on the white triangle at the top of the Package Explorer View. White triangle -> Configure Working Sets... allows to divide projects in separated sets.