Most of the time when my code couldn't compile, the problem is
most probably on the class path. Attached is a sample the classpath
file from one of my typical java project. This classpath is named as
.classpath and is found under the root of the Eclipse project folder.
This file is specially for Eclipse IDE to allow the runtime to locate
the files and resources required by the project.
|Eclipse java classpath example
(.classpath, another classpath
If you are not using Eclipse to run your java project, and is using the command line prompt, the classpath will have to be indicated in your javac command. The following example is a window batch (*.bat) file that indicate the classpath and compile the java code through the command prompt. The shell script (*.sh) file is similar to the batch file, which is used in the Linux OS. The period "." is refering with reference to the executing or current directory."./bin" indicates that the subdirectory to be included in the classpath.
The last parameters "MyMain" indicates the initial class object to invoke. The Java will attempt to execute the main(String args) method that is found in this class. If the class object is under a directory call "myfolder", the last parameter will be "myfolder/MyMain"
MyMain is the class name for the program entry point.
Command prompt java
classpath (run.bat for Windows OS, run.sh
for Linux OS)
run2.bat batch file with Ant example.
Command prompt java
classpath (run.sh for Linux OS)
Shortcut file for Microsoft Windows user to open command prompt which starts at the local directory.
Usually when you open a command prompt program, open up under the default directory, example "c"\users\abc\". You can delete away the "Start in" directory found under the cmd prompt shortcut *.lnk 's properties.
If this *.lnk file is place under d:\mydir\myhome\. When you click on this lnk, it will open up a command prompt with the current directory which it is in, which is "d:\mydir\myhome\".
|cmd prompt open current dir.lnk|
Another common problem I have is to set my eclipse to the latest compiler version after installing the eclipse IDE (integrated development environment). Although I think that the installation process is quite simple, but I couldn't managed to get it configured without help from my senior teacher. Perhaps I am still unclear about the java environment.
I wanted to get it cleared once and for all. Therefore I put
in this effort to write down the steps here for the future. I know that
I will probably forget once more.
|To change the Java compiler
version 6 for the project in eclipse
|Import existing Java project into
- File>Import...>General>Existing Projects into Workspace
- Select the project folder, then click Finish
- click the file where your main() is, then click on run (Ctrl+F11)
The project can be previously compiled without any error and might still encounter error after importing the project to the new PC.
You might encounter the error for example:
"Implicit super constructor Object() is undefined".
Most issue is basically classpath set incorrectly.
- On the left project navigation panel, right click on the project's Properties (Alt+Enter)
- Select the option Java Build Path (left panel), and Libraries (right panel)
You should see a list of classpath as from your previous project.
There should be some icon with the red cross. Click on them and click Edit.
Resolving the error will solve the issue.
Very often the JRE version install on the new PC is different from the old.
Sometimes the path of the libraries are wrong after the import (due to absolute path in the classpath).
Importing *.jar and *.dll files to Eclipse Java project
1) copy the *.jar, *.dll to the folder "/project/lib"
2) right click the java project folder, and click on the properties
3) go to "Java Build Path>Libraries"
4) add the *.jar to the libraries
5) click on the *.jar package and link the required *.dll for the jar packages.
6) under Native Library location, select the directory path for the dll.
|Installing plugin for Eclipse
1) Run Eclipse program.
2) Go to Help>Install New Software...>
Work with: --All Available Sites--
Key in the name of the plugin in the filter text field.
3) Select and install the plug-in.
|Installing plugin for Eclipse
1) Copy the plugin to eclipse/plugins
2) Run Eclipse program with an option -clean, "D:\eclipse\eclipse.exe -clean"
3) The plugin feature should be installed when the Eclipse is running.
4) To check if plugin is installed, go to Help>About Eclipse>Installation Details>Plug-ins
Managing plugins using links
1) Create a folder "eclipse/links" in the eclipse installed directory.
2) Create a file *.link
3) File will contains the path to the plugin outside the eclipse installation directory.
4) The path should point to a directory which has a “eclipse” folder which in turn has “features” and “plugins” as sub-folders
path for WindowXP, Vista
for Windows operating
system environment variables. eg. %APPDATA%, %PROGRAMFILES%,
%SYSTEMDRIVE%, %SYSTEMROOT%, %TEMP%, %USERPROFILE%
-Environment Variables in WinXP.pdf
|Setup Eclipse src folder tree, package presentation to Flat or Hierarchical.
For easy viewing of the package in a folder view.
|At the Package Explorer on the left panel, press Ctrl+F10. Select "Package Presentation", then choose "Hierarchical".
|Useful Shortcuts for Eclipse||
Ctrl+F11 -Run console
Good to know
Ctrl+ -expand a method
Ctrl+Shift+T search for Java class
Alt+Shift+(up) -select chunck of codes within the brackets
Ctrl+Q go to the previous location of edit.
F3, go to declaration.
Unable to use, not so useful
Ctrl+F2 -Stop console
more amazing shortcut key,http://theshyam.com/2009/07/eclipse-productivity-shortcuts/
for your Java codes
Eclipse comes with it's standard coding style. I am not used to the way the brackets appears on the code. Eclipse allow customization for the coding style. And I have attached it for my reference.
To customised coding format
2011-01-18 Eclipse template. What I have just learned today. They are the shortcut keyword to auto generate codes in Eclipse. After programming Java in Eclipse for so long, I started to become lazier and lazier, looking for shortcut. As short as possible. You may get boring typing the same while loop, delay function, data iterator. In fact, most of the time, I copy the codes from my old programs and do a bit of modification to fit the new program. If you are starting to get bore with typing all those codes again and again, I am sure you are going to like what I have learned today; Eclipse template.
Let's say you need to code your main method that we always have as our program entry. Type in the word "main" and then press Ctrl+Space. You will see a menu pop up indicating a text icon with the text "main - main method". Double click on this option and pop, your main method is completely generate in an instant.
type "main" & press Ctrl+Space
The main method is generated immediately .
That's cool. There are many code generating keyword which are already built in as default. Keyword for example, while, if, etc... The loops variables are auto generated. The best part is being able to customise your own code generation. I enjoy using those code generation using my way of writing the Java codes.
To do the customisation, you have to change the preference.
Click on the button New, to start adding your own shortcut code generation.
For example, to generate the logger statement when I type "log"
Name: info or log, warn, error
Inserting a variable within a literal string.
Inserting a variable within a literal string enclosed within <>.
Inserting a variable within a literal string enclosed within "".
Highlighting a variable to check if it is null.
Get the name of this class.
There are many example in the default template for your reference. The settings can be export/import. I have saved my template here for use.
The code template is a bit similar to the editor templates. When eclipse help us to auto generate the setter or getter methods, it is actually using the code templates to do the job.
To do the customisation, you have go to.
My code templates is as follows,
Making a *.jar library file from Eclipse
Consolidating all your library into a *.jar file from Eclipse.
Go to the project folder in Eclipse. Right click on the folder
and select Export.
Running a Java program
- To run a java class file "myJavaClass.class"
- To run a java jar file "myJavaJar.jar"
Ensure that the program point of entry is specified in the
> java -cp myJavaJar.jar MyPackage.OtherClass
This allows you to run various main entry points which is
written for each of your class.
> java -cp
Using IKVM to convert Java into *.exe or *.dll file.
IKVM is a virtual software written in C# to runs java bytecode. Its main purpose is to convert allow Java source code to be reuse by the native Windows OS. This is achieve by converting the Java program into *.exe or *.dll executable which can be access from Visual C++/C#, Visual Basic, etc... programming language. The API provided by Java can also be available for access by other programming languages.
Running a java class or jar file using IKVM virtual machine is like running the same java program from a JVM (Java Virtual Machine).
|Eclipse will copy properties files from the resource folder to the bin folder. To prevent Eclipse from copying to the output bin, you can set from the preference,
Window->Preference, ->Java->Compiler->Building-> Output folder-> Filtered resources "*.properties, *.jar"