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.|
How do I tell Java where to find jar files and classes?
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):
This example loads the commons-codec-1.3 library on the java classpath.
How do I increase Java runtime memory?
Use the Java application launcher java with options to set the maximum Java heap size. Example:
How to write an Ant task?
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:
For more information on Ant see Apache Ant Manual.
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|
|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
|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.|