Recently I kept hitting an issue of Eclipse not recognising my imports (even though they were there). I was always getting the message:

1
import ClassName cannot be resolved

.

Where ClassName was my imported class. I’m using the MyEclipse Workbench 5.5.1 on Eclipse 3.2. Here are a few tips on how you can fix this (some worked for me, some didn’t):

  • ‘Clean’ Your Eclipse Project: Go to Project > Clean in Eclipse [This seems to work for me]
  • Refresh your project folder (right click on your project > refresh)
  • Re-build your project
  • Clean your builds (If using Ant or Maven – clean your builds)
  • Recreate your project in Eclipse
  • ‘Switch’ Workspace – then Switch back (Eg Change to Debug, then switch back to Java)
  • ‘Switch’ Workspace – then Switch back (Eg Change to Debug, then switch back to Java)
  • Remove and re-add your JRE:
    1. Right Click on your project > properties
    2. Click on the Libraries tab
    3. Click on the JRE
    4. Click remove, then OK
    5. Repeat 1-3 again, but add the JRE again

Hope at least ONE of those tips help!


Development, Quick Tips - Posted by admin on August 18, 2011

No Comments

If you get this boring message “firefox is already running” when you want to start it, follow these steps to solve the problem.

1. Goto profile folder. (it is under .mozilla/firefox)

2. Delete the files .lock and .parentlock

3. When you restart firefox , if you get a red bar with a message something like “..bookmarks system not available..”,

first backup your “places.sqlite” (it is under profile folder). (Backup is not to loose bookmarks)

Delete the files places.sqlite and places.sqlite-journal.

4. Restart firefox.

You can copy your places.sqlite back to recover your bookmarks.


Quick Tips - Posted by admin on May 8, 2011

No Comments

What is the difference between Struts 2 FilterDispatcher and StrutsPrepareAndExecuteFilter ?

In some tutorials, the declaration of Struts 2 in web.xml is through FilterDispatcher:

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

And some tutorials are using StrustPrepareAndExecuteFilter:

<filter>

<filter-name>struts2</filter-name>

<filter-class>

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

As a beginner you could ask this question as you go through the tutorials: What is the difference between Struts 2 FilterDispatcher and StrutsPrepareAndExecuteFilter ?

The asnwer is simple: The FilterDispatcher (org.apache.struts2.dispatcher.FilterDispatcher) is deprecated since Struts 2.1.3. If you are using Struts version >= 2.1.3, it’s always recommended to declare the new filter class as org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter


Struts 2 - Posted by admin on April 25, 2011

No Comments

Before talking about RMI it is better to mention the general concept named CORBA (Common Object Request Broker Architecture),which is an RPC technology and enables separate applications written in different programming languages and running in different computers to work each other like a single application or set of services. (source: wikipedia)

The corresponding technology in Java world called RMI, introduced in JDK 1.1, provides interaction between applications which are running in separate JVMs, possibly remote ones. It is easy to use but very powerfull and composed of two separate applications, a server and a client and can be basically defined as an object oriented RPC mechanism.  There are three differences between CORBA and RMI:

  1. CORBA is a language independant standart.
  2. CORBA includes many other technologies in its standart and so is complex.
  3. There is no such concept “object request broker” in RMI.

The motivation of RMI design was to enable developers to write distributed Java applications with the same semantics and syntax used for standalone applications. It allows programmer to invoke a remote method as if it were a local method call. Methods can even pass objects that a foreign virtual machine has never encountered before, allowing dynamic loading of new classes as required. Dynamic code loading means ability to download the definiton of an object’s class.This can be called as distributed object computing, which consists of three basic tasks:

  1. Locate remote objects.
  2. Communicate with remote objects.
  3. Load class definitions for objects passed around.

Objects with methods that can be invoked across JVMs are called remote objects. An object becomes remote by implementing remote interface.

By the way, because all transport tasks are handled by RMI system, the programmer does not need to strive low level communication details like sockets. The communication layer is based on TCP/IP connections between machines.

The RMI architecture is based on one important principle: the definition of behavior and the implementation of that behavior are separate concepts. RMI allows the code that defines the behavior and the code that implements the behavior to remain separate and to run on separate JVMs.This fits nicely with the needs of a distributed system where clients are concerned about the definition of a service and servers are focused on providing the service. Specifically, in RMI, the definition of a remote service is coded using a Java interface. The implementation of the remote service is coded in a class. Therefore, the key to understanding RMI is to remember that interfaces define behavior and classes define implementation. (source:java.sun.com)

RMI consist of following components:

  1. Server (Service Interface)
  2. Server (Service Implementation) (Skeleton)
  3. Client (Service Proxy) (Stub)
  4. RMI Registery

As stated, service interface is the definition of the behaviour. This remote interface must be public and extend java.rmi.Remote. Each declared method must declare that it throws RemoteException.

The remote class (service implementation) must implement the remote interface, should extend the java.rmi.server.UnicastRemoteObject (there are other ways to define a remote class but it is the easiest). The objects of this class can be used as remote objects so their methods can be invoked remotely. It also may have methods which are not declared in remote interface, in this case they can be called locally.

Stub is the local representative of remote object, responsible for carrying out the method invocation on the remote object. In other words, it is responsible for sending the remote call which passes over to the server side skeleton. The stub is opening a socket to remote server, mashalling the object parameters and forwarding the data stream to skeleton. A skeleton contains a method that receives the remote calls, unmarshalls all the parameters and invokes the actual remote object implementation.

A stub implements the same interface, that the remote object implements, however only those methods defined in a remote interface are available to be called from the client.

A client program makes method calls on the proxy object, RMI sends the request to remote JVM, and forwards it to the implementation.  Any return values provided by the implementation are sent back to the proxy and then to the client program.

The RMI registry is used to store a list of available services. A client uses the registry to make it’s proxy object, and the Registry is responsible for giving appropriate information to the client so that it can hook up with the server that implements the service.

Steps to implement RMI:

  1. Define remote interfaces
  2. Implement remote objects
  3. Implement clients
  4. Compile sources (javac)

NOTE: If your Java version is 5.0 (or upper) stubs and skeletons are generated as well.

  1. Generate stub and skeleton class files from implementations (rmic)

NOTE: This step is required only for the Java versions prior to Java 5.

  1. Start application (start rmiregistry, server and client)

Example will be added.


Java - Posted by admin on April 20, 2011

No Comments

To configure the Java Plugin follow these steps:

1. Exit Firefox browser if it is already running.

2. Uninstall any previous installations of Java Plugin. Only one Java Plugin can be used at a time. When you want to use different plugin, or version of a plugin, remove the symbolic links to any other versions and create a fresh symbolic link to the new one.

3. Create a symbolic link to the libnpjp2.so file in the browser plugins directory
* Go to the plugins sub-directory under the Firefox installation directory
cd <Firefox installation directory>/plugins

* Create the symbolic link
ln -s <Java installation directory>/lib/i386/libnpjp2.

so

Example

* If Firefox is installed at this directory:  /usr/lib/<Firefox installation directory>
* And if the Java is installed at this directory:   /usr/java/<Java installation directory>
* Then type in the terminal window to go to the browser plug-in directory:  /usr/lib/<Firefox installation directory>/plugin
* Enter the following command to create a symbolic link to the Java Plug-in for the Mozilla browser.
ln -s /usr/java/<Java installation directory>/lib/i386/libnpjp2.so
4. Start the Firefox browser, or restart it if it is already up.

In Firefox, type about:plugins in the Location bar to confirm that the Java Plugin is loaded. You can also click the Tools menu to confirm that Java Console is there.


Quick Tips - Posted by admin on April 7, 2011

No Comments

Today, when I wanted to start my eclipse I got following error. The environment is Windows XP, Eclipse for EE Developers (Galileo) with Java 1.6 SDK.

JVM Terminated. Exit Code=-1

JVM Terminated. Exit Code=-1

Intuitively I removed the content of Eclipse.ini and it seemed to work.  However, I did not want to remove ini configs just to be able to run Eclipse again (especially memory allocation parts).

Reducing the -Xmx512m  to -Xmx256m solved the problem.

Niether I know why Eclipse did not start today (itworked yesterday and there is no notable change in system config  since yesterday) nor I have a reasonable explanation for solution (and problem). Does anyone have an idea?


Quick Tips - Posted by admin on April 7, 2011

No Comments

File uploading is a very common task in web applications and it is frequently necessary to upload a file from client to server. In HTML forms the input type “file” allows the visitor browse the local file system to select the file. When the file is selected it is sent to server as a part of a POST request. During this there are two mandatory restrictions applied to the form with input type file: it must contains attribute enctype set to value multipart/form-data and its method should be POST. Thus, entire request in sent to server in encoded form. JSP container does not parse the content of requests with type multipart. That is why JSP or servlet processing incoming file data has to use own means to handle request and extract a file from there.

We will use Apache commons FileUpload to handle the file on server.
Once it is downloaded, we should copy the jar files under WEB-INF/lib of our application. If we put a library into lib directory newly, we should be aware of the fact that we need to restart Tomcat. >> Continue Reading


Development, JSP & Servlets - Posted by admin on April 18, 2010

No Comments