1Z0-895 ( Java EE 6 EJB Developer Certified Expert Exam )

I just passed the  the EJB SCBCD/OCPJBCD  exam with 86%. I’m happy with that!
Tools for passing the exam were:
* Mannings EJB in action Book
* O’Reillys EJB 3.1 Book
* Parts of JEE6 tutorial
* Enthuwares testexams. Recommended
*The notes found in the link section for certifications on coderanch , thanks Ivan and Fritz, Very good!!
* and… some coding and testing and coding and testing and… yes, you get it.

I worked a bit with EJB before, and will continue to do so. Next book for me is Adam Biens Enterprise patterns, but first… a well deserved rest

Got Java properties

Some notes on Javaproperties that i gathered from different sources (thanks btw)

Java Properties:

* Properties can be saved to a stream or loaded from a stream.
* Each key and its corresponding value in the property list is a string.
* Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead.
* Properties use ISO 8859-1 character encoding. Unicode escapes can be used; however, only a single ‘u’ character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.
* Generally properties should be read by classpath, not by file.

Example of syntax in propertiesfile!

# You are reading the ”.properties” entry.
! The exclamation mark can also mark text as comments.
website = http://www.eff.org
language = English
# The backslash below tells the application to continue reading
# the value onto the next line.
message = Welcome to \
# Add spaces to the key
key\ with\ spaces = This is the value that could be looked up with the key ”key with spaces”.
# Unicode
tab : \u0009

How to read properties file in web application?

In a webapplication you don’t wan’t to use the current working directory i.e. /home/Tomcat/bin or something different, but thus not home/Tomcat/webapps/contextname as you would like. (Too see current working directory: System.out.println(new File(”.”).getAbsolutePath()));

However, if still using file-related properties loading use absolute paths. E.g. /full/path/to/file.ext.

Note: Paths in Java
absolute like “/full/path/to/file..xml”;
relative like “path/resource.xml”;

Relative means, relative to the location, where the method was called. The path will be appended if needed.. Absolute will be used as is, only first / will be removed before the search.

In a WAR and etc, The normal practice is to place those kind of resources in the classpath, or to add its full path to the classpath (in an IDE like Eclipse that’s the src folder and the ”build path” respectively). This way you can grab them with help of the ClassLoader by ClassLoader#getResource() or ClassLoader#getResourceAsStream(). It is able to locate files relative to the ”root” of the classpath. In webapplications (or any other application which uses multiple classloaders) it’s recommend to use the ClassLoader as returned by Thread.currentThread().getContextClassLoader() for this, ie ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

This returns the parentmost classloader which has access to all resources. The MyService.class.getClassLoader() will only return the (child) classloader of the class in question which may not per se have access to the desired resource. It will always work in environments with a single classloader, but not always in environments with a complex hierarchy of classloaders like webapps.

In Thread#getContextClassLoader(), remove the leading /.

getResourceAsStream() will load a file path relative from your application’s classpath.

Another alternative in webapps is the ServletContext#getResource() and its counterpart ServletContext#getResourceAsStream(). It is able to access files located in the public web folder of the webapp project, including the /WEB-INF folder. The ServletContext is available in servlets by the inherited getServletContext() method.

Class.getResourceAsStream() method or the ClassLoader.getResourceAsStream() method?

There are ClassLoader.getResource() and Class.getResource() and they work differently!!!

java docu:

The methods in ClassLoader use the given String as the name of the
resource without applying any absolute/relative transformation (cf. the
methods in Class). The name should not have a leading “/”.

Using getClass().getResourceAsStream(”pos_config.properties”) is dangerous because you’re using a path that’s resolved relative to the given class, and a subclass could change the location against which it’s resolved. Therefore, it’s safest to name an absolute path within the jar:


getResourceAsStream() method or the getResource() method?

getResourceStream uses getResource underneath the hood.

3 Choices:

Put it in the classpath, load it by ClassLoader#getResourceAsStream() with a classpath-relative path:

Properties properties = new Properties();

Here filename.properties is supposed to be placed in one of the roots which are covered by the default classpath of a webapp, e.g. Webapp/WEB-INF/lib, Webapp/WEB-INF/classes, Appserver/lib or JRE/lib. If the propertiesfile is webapp-specific, best is to place it in WEB-INF/classes. If you’re developing a project in an IDE, you can also drop it in src folder (the project’s source folder).

Put it somewhere outside the default classpath and add its path to the classpath of the appserver. In for example Tomcat you can configure it as shared.loader property of Tomcat/conf/catalina.properties.

Put it somewhere in web folder (the project’s web content folder), so that you can load it by ServletContext#getResourceAsStream() with a webcontent-relative path:

Properties properties = new Properties();

Note that I have demonstrated to place the file in /WEB-INF folder, otherwise it would have been public accessible by any webbrowser. Also note that the ServletContext is in any HttpServlet class just accessible by the inherited GenericServlet#getServletContext().

Put it somewhere in local disk file system so that you can load it the usual java.io way with an absolute local disk file system path:

Properties properties = new Properties();
properties.load(new FileInputStream(”/absolute/path/to/filename.properties”);

I personally prefer putting it in the classpath outside the project (add new path to the classpath), so that I can manage it from outside and so I don’t need to hardcode an absolute disk file system path in my Java code.

How to convert properties file into XML file?

java.util.Properties class come with a storeToXML() method to convert existing properties data into a XML file.


props.setProperty(”test”, ”C:/dev/sdk/test.dat”); Use forward slashes (or \\ or \u005c) even on Windows but rember some occasions on Windows must have backlashes – ie NTLM login where there exists DOMAIN\USERNAME

How to read properties file placed outside war?

Provide the file name using context param or java system parameter.

1.Context Param


2.System Parameter

java -Ddao.filename=D:\daofilename.props server and String propPath = System.getProperty( ”my.app.properties” );

ou should give property file location as java runtime property to your web container..

Such as, java -DmypropertyFile.location=c:/propertyfile.properties -jar yourContainer.jar

If you are using Tomcat you can add any properties you like to the context, look at the documentation for context.xml. All the application servers has their own way of doing this…

Or use JNDI-resources

Apache Commons has a Library Called Apache Commons Configuration , check it out.


Välj Java JVM version i Ubuntu

Ibland har man behovet av att ha flera Java JVM:er installerade, till exempel om man vill ha Oracle och en gammal SUN-JVM installerad. För att snabbt byta mellan dessa, använd kommandona:

sudo update-alternatives –config java
sudo update-alternatives –config javac

i terminalen. De visar en lista på vilka JVM: som är installerade, samt låter dig välja vilken du vill använda för tillfället.

Jfokus 2011

Förra veckan var jag på Jfokus, en konferens för Java och tillhörande teknologier och språk. Det var helgrymt. Intressant saker på gång, roliga samtal med andra utvecklare, och en del tänkvärda seminarium. Även om det fanns många som var inne på Android,Scala, Html 5, osv så valde jag främst att följa spåret Java EE 6, Maven , Arkitektur och Git. Alla värda en egen bloggpost som kommer vad det lider. En sak jag kan säga direkt är att jag är grymt imponerad av EJB 3 och Java EE 6. Vilken lättnad att kunna börja kasta ut Spring och minska andelen externa ramverk. Den som var med på EJB 2 tiden kan tänka om direkt. Det är mycket, mycket enklare idag.

Sedan gillar jag också den framtidsoptimism som fanns. Tyckte att Oracle gav en bra presentation även om man inte svarade på alla frågor.

Kan redan säga att 2012 är givet att boka in.

Efter att gått från Java-världen till .Net och sedan tillbaka till Java så har jag fått perspektiv på saker. Det är naturligtvis helt subjektivt att säga att jag tycker det är större fokus på saker som designmönster, och större valfrihet inom Javavärlden. Det beror på vilken arbetsplats man hamnar på. Men objektivt är att jag faktiskt kan köra Java på många fler plattformar än de som tillhandahåller fullt .Net stöd. (nej, alla delar av .Net ramverket är inte med i Mono, även om mycket stöds). Dessutom får jag nu sitta i Linux, Mac Os eller Windows – det väljer jag själv – bara det:)

UTF-8 i netbeans

Genom en mängd skumma problem då vi delade filer i olika miljöer genom MAC/Subversion ,Windows 7 upptäckte jag till min förvåning att netbeans spar sina filer i ANSI-format per-default. Trots att projektets inställningar är satta till UTF-8!
Även om man följt instruktionerna här.

Gör ett tillägg i filen /etc/netbeans.conf
I raden ”netbeans_default_options”-> Lägg till -J-Dfile.encoding=UTF-8
(På Mac: Contents/Resources/Netbeans/etc/netbeans.conf)

Enkel lösning, irriterande problem,,, som gav mig upphov till några timmars ANSI->UTF-8 konvertering av filer manuellt.:/

From Java to C# and back to Java again

Recently i had to manage with a big rewrite project. The system i’m working on is not ”rocket science”, but it has it’s history and it’s quirks (plenty). Some of the things missing were AuditTrail’s, multiplatformsupport, and easy handling of adding new sources of information.
The system is written half in C#/Java. Why?
Because there never was an long-term strategy toolchoice, different developers have been using their favourite tools at different points in time, or in different parts of the architecture. The positive thing about the project i’m in, is that the people doesn’t seem particulary cemented in any of the enviroments, which is good for avoiding ”religionwars”

So, this would be a major rewrite.Yes.
I havn’t myself been doing Java-development since somewhere around JDK 1.4 a couple of years ago. Since then i’ve been mostly working in .NET/C#/ASP.NET.(I even took the certifications MCPD for Enterprise Application Developers – Amen).

But, now we needed among other things, a stable ORM and a good AuditTrail to cope with.
First we looked at LINQ for SQL,, but it’s a bit too lightweight, and it’s future seems to be unsure. So, we headed for the EF. framework… well. NO.. Maybe the next version… but we couldn’t wait for the next version.
That lead us too Hibernate/nHibernate… and look and behold. JPA was what we been looking for..
Now all we needed was a AuditTrail… and again, there it WERE in eNvers.Does C# have an eqvivalent? I found this so far.

Ok, so EJB 3 and JPA is a major, major lift from the old complicated models. Good!

But what about the working-enviroments,,,, one thing i like about .Net is that it’s soooo easy to get started in VS. Last time i tried Eclipse and Netbeans, they sadly needed a bit more effort to get into.(Altough very good when they were configured)
So i downloaded the latest NetBeans — *wow*
They HAVE been inspired by the ”bundle it” approach from Visual studio.

For me, it’s a strength knowing ”both” worlds of Java and .Net, and using them were it seems appropiate. As of now, i tend to use Java for backbone-functionality and were longterm architecture is important, and .Net for GUI/lightweight/RAD development.
This may change, i looking forward to next version of JSF etc.

I will follow up this blogpost.

Use the tool that’s right for you, don’t stagnate in any special tool, and have an open mind. If you’re a .Net developer, try out some stuff from the Java-world, and if you’re into Java, try some .Net. It gives you better credit if you can be neutral and know how to use the right tool for the right job

Right now,I’m quite a Java-fan again:) – altough missing some of the syntactic sugar from C#.