background preloader

Java practices

Facebook Twitter

WebSphere - Creating a Framework. WebSphere - Creating a Framework Authors: Lloyd Hagemo & Ravi Kalidindi Contributed by Candle Corporation Many patterns have been published for J2EE applications. By developing and connecting multiple patterns, developers can create a framework that improves the stability, performance, and scalability of their J2EE application architectures. Because the number of patterns continues to expand, it can be difficult for developers to select the best combination of patterns to create frameworks that optimize J2EE applications and fulfill specific IT or business requirements. Similar to individual patterns, frameworks serve as development process templates that enable organizations to streamline development while ensuring high performance levels. The ability to create a solid blueprint is critical to J2EE application development success.

This paper includes a summary of common J2EE patterns to use as a guide when developing applications. Summary of J2EE Patterns Connecting J2EE Patterns 1. Assemble executable JAR with dependencies using Maven. Problem description: As a result of the build process I need following artifacts to be generated and packed into single ZIP archive:JAR with all compiled classes and resourcesManifest file with properly configured main class and generated class pathall dependencies This archive could be unpacked and executed with double click on the application jar file or with simple java -jar command. This problem could be easily solved with Apache Maven build manager. We need to use and configure following plugins:org.apache.maven.plugins/maven-jar-pluginorg.apache.maven.plugins/maven-dependency-pluginorg.apache.maven.plugins/maven-assembly-plugin Configuration of the maven-jar-plugin: This plugin creates a jar file for your application.

This jar file contains all compiled classes and resources. One very important part of this file is a manifest. To make it possible to be executed without additional configurations it has to have classpath and mainclass entries. Create an executable jar from Maven « Thomas Sundberg. Create an executable jar from Maven Creating an executable jar from Maven is not really hard. This example will show you how you can include stuff in a jar and make it executable while not including stuff that is needed only during the build.

The job is done in the plugin maven-assembly-plugin that will include all resources that is available in the runtime classpath generated by Maven. <? Xml version="1.0" encoding="UTF-8"? The depenency commons-math will be included in the final distribution. The main method that will be executed is implemented in se.sigma.educational.Main To run it, execute java -jar executable-example.jar Maven – A build system for building Java codeAssembly – The Assembly plugin for MavenThomas Sundberg – The author Like this: Like Loading... Java - Building a runnable jar with Maven 2. Adding Classes to the JAR File's Classpath (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)

You may need to reference classes in other JAR files from within a JAR file. For example, in a typical situation an applet is bundled in a JAR file whose manifest references a different JAR file (or several different JAR files) that serves as utilities for the purposes of that applet. You specify classes to include in the Class-Path header field in the manifest file of an applet or application. The Class-Path header takes the following form: Class-Path: jar1-name jar2-name directory-name/jar3-name By using the Class-Path header in the manifest, you can avoid having to specify a long -classpath flag when invoking Java to run the your application. Note: The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes.

Warning: The text file must end with a new line or carriage return. Debugging java.lang.NoSuchMethodError (Java Technology Community) Debugging java.lang.NoSuchMethodError- What is java.lang.NoSuchMethodError ? This is an Error. It means that when the code was compiled, the method existed in the class. But when the code was run, the method didn't exist. Why java.lang.NoSuchMethodError happens ? This happens when the referenced class used to compile the code and the class in the class path used to run the code are different.

This error is caught by the compiler. this error can only occur at run time if the definition of a class has incompatibly changed. How to debug java.lang.NoSuchMethodError ? User should check for this possibility whether definition of a class has incompatibly changed. JVM option - -Dibm.cl.verbose= <class name > , and -verbose:dynload can be used to trace the way Class Loaders find and load application classes. To enable -Dibm.cl.verbose tracing ,run the java program with this commandline option - <JAVA_HOME>/jre/bin/java -Dibm.cl.verbose=<Class expression><Classfile> [priyanka@flibble9 priyanka]$ . Java - What is the difference between NoClassDefFoundError and ClassNotFoundException. Design Patterns in Java - Singleton. Design Patterns in Java - Singleton - Tutorial Copyright © 2009 - 2011 Lars Vogel Singletons This article describes the Design Pattern "Singleton" and its usage in the programming language Java.

A singleton in Java is a class for which only one instance can be created provides a global point of access this instance. The singleton pattern describe how this can be archived. Singletons are useful to provide a unique source of data or functionality to other Java Objects. The possible implementation of Java depends on the version of Java you are using. As of Java 6 you can singletons with a single-element enum type. Package mypackage; public enum MyEnumSingleton { INSTANCE; } Before Java 1.6 a class which should be a singleton can be defined like the following. public class Singleton { private static Singleton uniqInstance; private Singleton() { } public static synchronized Singleton getInstance() { if (uniqInstance == null) { uniqInstance = new Singleton(); } return uniqInstance; } } 2. 2.2.

Abstract Factory. Using references to interfaces instead of references to concrete classes is an important way of minimizing ripple effects. The user of an interface reference is always protected from changes to the underlying implementation. The Abstract Factory pattern is one example of this technique. Users of an Abstract Factory can create families of related objects without any knowledge of their concrete classes. (A typical business application would usually not need to use this technique, at least as applied to Data Access Objects.) Example An Abstract Factory is a major part of the full Data Access Object (DAO) scheme. There are two distinct families of items here: the various datastore implementations (in this case, text files, or a relational database)the various business objects which need persistence (in this case, User and Device) Let's take the example of storing Device objects. ...with an implementation for a file system: ...and an implementation for a relational database:

Linux - Error java.lang.NoClassDefFoundError on org.springframework.webflow.util.RandomGuid.