View RSS Feed

All Blog Entries

  1. Safe JAR file migration in Eclipse

    by , 05-21-2011 at 10:48 PM (My Java Tips)
    Its always a good idea to include refactoring information into the JAR file when you are exporting it from Eclipse. Eclipse’s JAR Export Wizard offers this option.

    Here is how it works:

    • File > Export
    • Select JAR file
    • Select: Export refactorings for checked projects


    This is very useful because then all the clients that are using older version of JAR will be able to migrate an old version of the JAR file to a new one using the Refactor > Migrate ...
    Tags: jar, refactoring Add / Edit Tags
    Categories
    Eclipse
  2. Searching methods with some return type in Eclipse

    by , 05-21-2011 at 10:47 PM (My Java Tips)
    Working on a project, I need to search all the methods defined in my workspace or in a package or in a class with a specific return type. Eclipse search feature (ctrl + h) provides support for this. We all have used eclipse search window a lot of times but many of us don’t know how to really use it to great affect.

    To search the methods with a specific return type, type

    Java Code:
    * String
    is used as wildcard for method name and String is the return ...
    Categories
    Eclipse
  3. Mark occurrences in Eclipse

    by , 05-21-2011 at 10:44 PM (My Java Tips)
    Mark occurrences is a nice feature in Eclipse. It simply marks/highlights all the occurrences of a variable, method or a type.

    This can be done by clicking ‘Mark Occurrences’ icon on the menu bar. If you like to use short cut keys, then use Alt+Shift+O. If you select methods return type, and mark occurrences is enabled, all the method exit points will be highlighted.

    Name:  mark-occurances.jpg
Views: 360
Size:  21.6 KB

    You may also select a super class or interface and then ...
    Categories
    Eclipse
  4. Hot Code Replace in Eclipse

    by , 05-21-2011 at 10:41 PM (My Java Tips)
    Ever heard of hot code replace in reference to debugging in Eclipse? Well, its sure is a powerful and useful feature. It enables you to change code while debugging. It simply means that you may change the code in debug mode and see its affect.

    Hot code replace is supported in 1.4 or higher VM. There are some changes like adding or deleting methods, class variables or inner classes which cannot be hot swapped. This depend on the support provided by a particular VM. This might also ...
    Categories
    Eclipse
  5. Threads and Monitors in Eclipse

    by , 05-21-2011 at 10:40 PM (My Java Tips)
    When you are working with multi threading where resources are locked and released, deadlock prevention becomes very important. To prevent starvation, proper lock and release mechanism has to be defined/implemented.

    Eclipse’s debugger provides monitor information in the debug view. To use it, use the Show Monitors action which can be found in the Debug view drop down menu. It will show which threads are holding locks and which are waiting to acquire locks. The threads in red indicated ...
    Categories
    Eclipse
  6. SessionFactory Monitoring

    by , 05-21-2011 at 10:37 PM (My Java Tips)
    There are few ways of accessing SessionFactory metrics. One of them is to use JMX to publish metrics. For that you need to enable the StatisticsService MBean. A single MBean can be enabled for your SessionFactory or one per factory.

    Following snippet presents a configuration example:

    Java Code:
    // MBean service registration for a specific SessionFactory
    Hashtable tb = new Hashtable();
    tb.put("type", "statistics");
    tb.put("sessionFactory",
    ...
    Categories
    Hibernate
  7. Hibernate logging

    by , 05-21-2011 at 10:36 PM (My Java Tips)
    Hibernate logs various events using Apache commons-logging. Hibernate logs are very interesting to read if you want to do some troubleshooting or performance improvement. Hibernate uses commons-logging service for logging. It actually sends the log to either Log4j or to JDK1.4 logging.

    If you want hibernate to log using Log4j, then put log4j.jar into class path along with log4j.properties. Since log4j has almost become a standard these days, I would advice using that.
    Hibernate ...
    Tags: hql, jdbc, log4j, logging, sql Add / Edit Tags
    Categories
    Hibernate
  8. Defining fetch strategy

    by , 05-21-2011 at 10:33 PM (My Java Tips)
    To tune fetch strategy, you have various options. Which to choose depends on the scenario. You may define the fetch strategy in the mapping document. For instance:

    XML Code:
    <set name="permissions" 
                fetch="join">
        <key column="userId"/>
        <one-to-many class="Permission"/>
    </set
    <many-to-one name="mother" class="Cat" fetch="join"/>
    ...
    Tags: hql Add / Edit Tags
    Categories
    Hibernate
  9. Remote Debugging

    by , 05-21-2011 at 10:31 PM (My Java Tips)
    Ever wanted to launch a Java program from a network computer and debug it from the workstation running the Java platform? Example scenario is that you have an a J2EE application deployed on a dedicated server and you need to do debugging on you machine. This can be done if Java VM that supports this feature.

    To do this, launch the program in debug mode on the remote machine. It means that the program on server will wait for a connection from your debugger. Now start the debugger ...
    Categories
    Java EE , JBoss
  10. Lazy select fetching for collections

    by , 05-21-2011 at 10:30 PM (My Java Tips)
    Hibernate uses lazy select fetching for collections by default. This behavior makes sense and is used as it is unless you really want to do something different for some reason.

    Hibernate will use the batch fetch optimization for lazy fetching if you set hibernate.default_batch_fetch_size. There is a problem with lazy fetching that should be considered. Remember, you should never access to a lazy association outside of the context of an open Hibernate session otherwise exceptions ...
    Categories
    Hibernate
  11. Sharing setUp() and tearDown() code for all tests - I

    by , 05-21-2011 at 10:29 PM (My Java Tips)
    You may wish to share the code written in setUp() and tearDown() methods of your JUnit tests. This clearly will indicate that you have excessive coupling in your design. Coupling is not bad always but if more tests share the same test fixture state, then this indicates that the classes under test have some undesirable dependencies.

    You may wish to remove these dependencies but its another stories. As I said, coupling or dependencies are not always bad. But if you wish to keep coupling ...
    Tags: setup, teardown Add / Edit Tags
    Categories
    JUnit
  12. Sharing setUp() and tearDown() code for all tests - II

    by , 05-21-2011 at 10:28 PM (My Java Tips)
    This post presents an example that shows how to use @BeforeClass and @AfterClass annotation.

    Java Code:
        public class SimpleTest {
        
            private Collection collection;
    	
            @BeforeClass
            public static void oneTimeSetUp() {
                // one-time initialization code        
            }
    
            @AfterClass
            public static void oneTimeTearDown() {
                // one-time cleanup code
    ...
    Tags: setup, teardown Add / Edit Tags
    Categories
    JUnit
  13. Running JUnit using Ant

    by , 05-21-2011 at 10:26 PM (My Java Tips)
    You may want to define build script using ant for your application. I will write about how to create ant script to run ant.

    Step 1: define ant properties

    XML Code:
    <property name="src" value="./src" />
    <property name="lib" value="./lib" />
    <property name="classes" value="./classes" />
    <property name="test.class.name" value="com.xyz.MyTestSuite" />
    ...
    Tags: ant Add / Edit Tags
    Categories
    JUnit
  14. java.lang.NoClassDefFound Error: org/apache/log4j/Layout

    by , 05-21-2011 at 10:22 PM (My Java Tips)
    You might have seen the following exception:

    Java Code:
    java.lang.NoClassDefFoundError: org/apache/log4j/*
    It may mean that log4j is not in your class path or you are using some outdated version of log4j.

    Get latest copy of log4j and put it in your class path. If JSP caused this, then put the log4j jar into WEB-INF/lib directory. Restart your application server (say Tomcat) and it should work now.

    Get latest version of log4j from:
    Apache log4j 1.2 - Download Apache log4j 1.2 ...
    Categories
    Frameworks
  15. JNDI - Introduction

    by , 05-21-2011 at 10:20 PM (My Java Tips)
    JNDI is a standard Java API that comes with JDK 1.3 and higher. It provides a common interface to a variety of existing naming services for example DNS, LDAP, Active Directory, RMI registry, COS registry, NIS, and file systems.

    The JNDI API is divided logically into a client API and service provider interface. Client API is used to access naming services and a SPI allows the user to create JNDI implementations for naming services.
    The naming service providers must implement ...
    Tags: jndi Add / Edit Tags
    Categories
    Java EE , JBoss
  16. The JNDI API

    by , 05-21-2011 at 10:18 PM (My Java Tips)
    JNDI API package is called javax.naming package. It is composed of 5 interfaces, 10 classes along with few exceptions. InitialContext is the key class.

    This is what Sun Java Docs say about InitialContext:
    Java 2 Platform SE v1.3.1: Package javax.naming

    In JNDI, all naming and directory operations are performed relative to a context. There are no absolute roots. Therefore JNDI defines an initial context, InitialContext, which provides a starting point for naming ...
    Tags: jndi Add / Edit Tags
    Categories
    JBoss , Java EE
  17. META-INF and WEB-INF directories

    by , 05-21-2011 at 10:16 PM (My Java Tips)
    If you're looking at a web application deployed on a tomcat server, then you might notice META-INF and WEB-INF directories.

    The META-INF directory is related to .jar files, It contains the manifest file which has list of jars. The WEB-INF directory is a vital component of your web application. Web application won't run without it. It contains a hierarchy in which you'll find the necessary configuration information for your web application, and all the class files for your servlets ...
    Categories
    JBoss
  18. Override/Implement methods

    by , 05-21-2011 at 10:14 PM (My Java Tips)
    Eclipse provides an easy way to generate stubs for the parent classes and implemented interfaces.

    Right click the your class and select Source > Override/Implement methods. You will be presented a window where you can select all the methods that you want to override/implement. This methods will be from the implemented interface or extended class.

    Name:  eclipseoverride.jpg
Views: 251
Size:  20.1 KB

    Really saves time. Do try this.
    Categories
    Eclipse
  19. Final keyword improves performance

    by , 05-21-2011 at 10:10 PM (My Java Tips)
    Its right to say that final keyword improves performance. For example, if you declare a method as final, then you cannot override it in derived classes. When this is told to compiler in advance using final keyword, it improves performance.

    Java Code:
    public final void doSomethng()
    {
    ...
    }
    Knowing that a method cannot be overridden, complier inline that method into its derived classes.
    Final variables, especially static final variables, ...
    Tags: final Add / Edit Tags
    Categories
    Performance
  20. Costs of Unit Testing

    by , 05-21-2011 at 10:09 PM (My Java Tips)
    Unit testing brings a lot of benefits but there is some cost for this. Lets talk about this.

    Unit tests require skill and time. Often managers dont appreciate unit tets because they regard it something othere than development. Ofcource client in not interested in unit tests but management needs to understand that these tests will save a lot of debugging effort in future.

    For unit testing to really deliver, all developers need to use it. Mostly due to lack of communicationm ...
    Tags: unit testing Add / Edit Tags
    Categories
    JUnit
  21. Detached Views in Eclipse

    by , 05-21-2011 at 10:07 PM (My Java Tips)
    Eclipse provides an option to detach a view so that it can be moved to the desired place. Detached views are used if you are not comfortable with the placing of a view and want it to be move to someplace that is more feasible.

    This is done as follows:

    • Right-click on the view
    • Select "Detached" from the menu
    • Place the view where you choose


    Name:  detachedview.PNG
Views: 321
Size:  6.3 KB

    Thing to remember is that one can also drag and drop ...
    Categories
    Eclipse
  22. Create SQL File in Eclipse

    by , 05-21-2011 at 10:03 PM (My Java Tips)
    SQL files can be created manually in any existing eclipse project. It's simple and useful. I'll list the required steps.

    • Open Database Development perspective
    • Select File > New > Other, expand SQL Development, select SQL File, and click Next
    • This will open the New SQL File wizard.
    • To create a new project, click "Create Project" and follow the wizard instructions. Now provide the SQL file name.
    • Select a connection profile type from the Connection profile
    ...
    Tags: eclipse, sql Add / Edit Tags
    Categories
    Eclipse
  23. Using reflection QueryRequestTag

    by , 05-21-2011 at 09:51 PM (My Java Tips)
    This post presents an example that show use of reflection in creating a tag. The created tag will call some methods of the request (HttpServletRequest) object using reflection.

    Source code for the QueryRequestTag handler is presented below:

    Java Code:
    public class QueryRequestTag extends ExTagSupport {
        static Object []params = new Object[0];
        
        static Hashtable methods = new Hashtable();
        
        static LocalStrings ls =
    ...
  24. Tags and JavaBeans

    by , 05-21-2011 at 09:42 PM (My Java Tips)
    We use tags and beans to fetch data from the bean and present it. If you have some knowledge about these, then you will love working with these.

    Bean interaction involves JSP getting the value of some property of a bean and displaying that value to the user. Another role of JavaBean is defining how events are specified. Tags can be concerned with two bean-related issues:

    • Introspecting the beans to find the properties and get the methods that these tags should call
    ...
    Tags: javabean, tag Add / Edit Tags
    Categories
    Java EE
  25. Reflection example - accessing private data

    by , 05-21-2011 at 05:28 PM (My Java Tips)
    If you think that you cannot access private data members of a class from some other class, then think again. With Reflection, this is possible.

    Using reflection, we can see / view / access a private member, private variable, private method. Some people will not like this with argument that this actually means messing up the laws of encapsulation. I use it for unit testing private methods.

    Example follows:


    Java Code:
    import java.lang.reflect.Field;
    ...
    Tags: reflection Add / Edit Tags
    Categories
    Java SE
Page 48 of 48 FirstFirst ... 38464748