Rage Against the Android – Nearly solved! #eclipse #netbeans

After ranting against Android in my previous post I have mainly solved now the untestable situation Android is producing.

Thanks to Robolectric all my tests are finally fast and do not unpredictable hang or similar!

The tests execute nearly as fast as in a normal Maven project – plus additional 2 seconds to bootstrap (dex?). Robolectric was initially made for maven integration on IntelliJ. But it works without Maven and under Eclipse as described here. I didn’t get robolectric working via Maven under Eclipse – and it was overly complex e.g. you’ll need an android maven bridge but when you use a normal Android project and a separate Java projects for the tests then it works surprisingly well for Eclipse.

Things you should keep in mind for Eclipse:

  • Eclipse and Robolectric description here
  • In both projects put local.properties with sdk.dir=/path/to/android-sdk
  • Android dependencies should come last for the test project as well as in the pom.xml for the maven integration (see below)

But now even better: when you are doing your setup via Maven you can use NetBeans! Without any plugin – so even in the latest NetBeans version. But how is the setup done for Maven? Read this! After this you only need to open the project with NetBeans (or IntelliJ).

Things you should keep in mind for Maven and NetBeans:

  • Use Maven >= 3.0.3
  • Again: put the android dependencies last!
  • Specify the sdk path via bashrc, pom.xml or put it into your settings.xml ! Sometimes this was not sufficient in NetBeans – then I used ANDROID_HOME=xy in my actions
  • Make sure “compile on save” is disabled for tests too, as I got sometimes strange exceptions
  • deployment to device can be done to:
    mvn -DskipTests=true clean install      # produce apk in target or use -Dandroid.file=myApp.apk in the next step
    mvn -Dandroid.device=usb android:deploy # use it
    mvn android:run
  • If you don’t have a the Android plugin for NetBeans just use ‘adb logcat | grep something’ for your UI to the logs

Thats it. Back to hacking and Android TDD!

Advertisements

Rage Against the Android #eclipse

Developing Android applications on Linux with Eclipse sometimes can get really ugly. Sadly neither NetBeans which has a really nice Android plugin, but cannot execute a single test nor IDEA can rescue me or make me switching 😦 but probably they wouldn’t rescue me due to problems of Android development kit itself – I’m not sure.

Update: Have a look at my solution

So, I’ve collected some of the most common problems I encountered while developing an Android app and how to ‘solve’ them:

  • Problem: Eclipse says ‘Your project contains error(s), please fix it before running it.’ and you cannot find a problem.
    Solution:
    1. Open the problem tab. fix the described errors.
    2. Make sure that you included all necessary jars in your build path
    3. Sometimes even this can help: rm ~/.android/debug.keystore
  • Problem: you cannot debug your application
    Solution:
    1. check if debuggable = true in application tag of manifest xml
    2. if that does not help or if you are getting “Can’t bind to local 8601 for debugger” in the Console tab then read this and make sure you use only the line
    127.0.0.1       localhost
    in your hosts file. if not, change the file and restart adb (see below)
  • Problem: Error “AdbCommandRejectedException: device not found”
    Solution: restart adb (see below)
  • Problem: you cannot select one test case to execute
    Solution: run the whole (android) juni test or a package and then select via right click to debug one single test

If nothing seems to help then try one or all of the following steps:
1. restart device
2. restart eclipse
3. restart adb: sudo adb kill-server; sudo adb start-server

Please add your problems and solutions in the comments 😉

Java Application Frameworks (not only client side …)

In an old post I listed all Java libraries,where only two application frameworks were listed.

Today it is time to list some client side Java application frameworks, because I discovered some new ones while reading the W-JAX announcement. Some of the listed frameworks will make developing application with DB easier. And some of them are real 3 tier architectured frameworks. Some of them even allow you to develop RIA’s and web frameworks at the same time.

Here is now the list of open source Java application frameworks especially for the desktop. Feel free to add some more (via comment):

  1. NetBeans RC Platform, my IDE is build on this 😉
  2. Eclipse RC Platform, has an interesting ‘subproject’ called Riena
  3. Spring RC, at the moment my favourite used in TimeFinder
  4. AppFramework which won’t be in JDK 7, but has a lot of derivatives
    1. Swing application framework fork
    2. Guice Utilities & Tools Set
    3. Better Swing AppFramework
    4. with OSGi
  5. JVx, looks very nice! Makes fast development of Swing applications possible (with db support)
  6. OpenXDev a framework which could be used as a base for your next Swing project
  7. Genuine is a client framework for Java Swing applications for which it provides basic infrastructure
  8. Genesis with Swing and SWT binding; Easy, transparent remoting; etc
  9. GWT (although only intented for javascript widgets it could theoretically being used as a rich client running in the jvm)
  10. OpenSwing Framework is an open-source suite of advanced graphics components based on Swing toolkit
  11. Leonardi Framework
  12. Jspresso is a framework for building rich internet applications
  13. Viewa framework
  14. XUI is a Java and XML RIA platform
  15. Swing + XUL = SwiXAT looks interesting but dead, the same for the next:
  16. Swing + XML = SwiXml a small GUI generating engine.
  17. buoy built on top of Swing. Xml serialization of UI possible
  18. But why xml if you have groovy: Griffon 😉
    Now, a nice approach would be to send/receive groovy code and build the clients’ GUI on fly… this would be like replacing the browser+html+javascript system with rock solid JVM+Groovy 😉
    Another Comment from AndresAn addtional tidbit about Griffon, it can be seen as a polyglot programming desktop/RIA framework as it supports 5 JVM languages at the moment: Java, Groovy, Scala, Clojure and JavaFX. It also lets you embed JavaFX components on Swing containers.
  19. JMatter is a software framework for constructing workgroup business applications based on the Naked Objects Architectural Pattern.
  20. Metawidget is a ‘User Interface widget’ that populates itself, at runtime, with UI components to match the properties of your business objects.
  21. Pivot a platform for building rich internet applications in Java

Especially JVx with a webstart demo looks very promising! It even feels better and faster than an ordinary flash application!

Commercial:

  1. Aviantes-Business-Application-Framework
  2. Jide Desktop Application Framework
  3. Jazz

I listed only frameworks which help developers to easier build client side desktop application and only if they run in the JVM. So frameworks where the client is browser-based (aka web frameworks) are not listed here.

For a good list of J2EE frameworks go to java-source.net or to wikipedia. (Or here, or there, or even here)

Update: For additional comments look at dzone

Plugable Spring RC? (OSGi with Spring Rich Client)

My last post was about pure Swing, which could be easily OSGified. OSGi technologie can be e.g. used to hotdeploy or undeploy jar files to your application server (at the moment only Spring’s server is suggested) or simply to update your Swing application at runtime or write plugins for them. I don’t know for sure, but it seems to me that Eclipse is the most prominent user of OSGi (equinox is one implementation of the specification…) as desktop application, although NetBeans can now use OSGi bundles.

(BTW: do you know a funny myth where the small “i” from OSGi could come from? No? Read the comments here!)

In the last post I show you several swing applications, which were successfully OSGified, today I would like to list Swing- or Eclipse-based applications which use Spring Dynamic Modules:

Then today, in contrast to my last post, I would like to show you an identical solution for the Spring Rich Client project.

Why should I use another framework on the top of OSGi? This is simple: Spring DM provides several nice features, like no dependency on the OSGi implementation (felix, equinox, knopflerfish) and even no one on OSGi at all. Everything (and think) is a bean, like always! But why Spring DM? Why not Declarative Services from OSGi itself or Apache iPOJO? This is simple: I don’t know the others and Spring Rich Client cries for the Spring solution 😉

But to make this short story very short: after struggling the last two days to get Spring Dynamic Modules integrated with Spring Rich Client I gave it up and tried the pure OSGi solution – and – it works. It might be an ugly solution for all those of you who like dependency injection, but this can be optimized later. Today this might be the first plugin for Spring Rich Client – ever:

osgified-springrc

The button “say hello” with its JOptionPane message was installed through the dynamically loaded plugin. The source code is nearly identical to the one in my previous post. Just add the Spring RC startup before you grab the menu from the application to create a MenuTracker, which calls the MenuService(=plugin):

@Override
public void start(BundleContext context) throws Exception {
 startup.start();
 menuTracker = new MenuTracker(context, Application.instance().getActiveWindow().getControl().getJMenuBar().getMenu(1));
 menuTracker.open();
}

The source is available from my TimeFinder project via:

svn checkout -r 595 https://timefinder.svn.sourceforge.net/svnroot/timefinder/trunk timefinder

Be sure to start the application from de.timefinder.core.osgi.Activator (main method!).

I hope this post inspired someone to be the first one with Spring DM + Spring RC marriage … or sb. suggests working examples how Spring DM could be started from command line or from NetBeans.

Hints for Mavenization of Java Web Applications

I discovered that it is not that easy to transform a web application from a native eclipse/netbeans project to a maven project.

Here are some hints which can make your life easier:

  1. To create the project structure for the web application use this command:
    mvn archetype:create -DgroupId=de.mycompany.app123 -DartifactId=mywebapp -DarchetypeArtifactId=maven-archetype-webapp
    Be sure you replaced the 2.3 header with
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    this was helpful for fixing this issue:
    "The selected Java EE version does not support selected JavaServer Faces version" + restart netbeans!
  2. To use jdk 1.6 do
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.1</version>
    <configuration>
    <source>1.6</source>
    <target>1.6</target>

    <encoding>UTF-8</encoding>
    </configuration>
    </plugin>
    Maybe for you it is necessary to use tomcat 5.5 for myfaces 1.1. Therefor you probably have to change the <source> element to 1.5 … Here you can determine the class format version
  3. use
    <repositories>
    <repository>
    <id>java.net</id>
    <name>java.net</name>
    <url>http://download.java.net/maven/2/</url&gt;
    </repository>
    </repositories>
    for the missing dependency in hibernate org.hibernate:hibernate:jar:3.2.1.ga: javax.transaction:jta:jar:1.0.1B
  4. All libraries which you need at compile time but not on the server (like the following) should get an scope=provided
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
    </dependency>
  5. All libraries which you need for your tests (e.g. junit or hsqldb) should get an scope=test
    <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.1.102</version>
    <scope>test</scope>
    </dependency>
  6. If your tests are temporarly broken or if you want to speed up testing do:
    <plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.3</version>
    <configuration>
    <skip>true</skip>
    </configuration>
    </plugin>
    (See this post for a better solution.)
  7. To invoke the tomcat plugin (‘mvn tomcat:run’) the following code can be necessary
    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>tomcat-maven-plugin</artifactId>
    <configuration>
    <url>http://localhost:8080/webapp/</url&gt;
    </configuration>
    </plugin>
    This plugin uses tomcat5.5, to use tomcat 6.0 compile it for yourself or run the project from within your IDE with maven plugins.
  8. To invoke the jetty plugin (‘mvn jetty:run’) the following code can be necessary
    <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <version>6.1.10</version>
    </plugin>
  9. You get an exception like the following in NetBeans??
    [ERROR]Runtime Exception thrown during execution
    [ERROR]null

    Solution 1 -> use external maven (command line)
    Solution 2 -> try to recompile the project from command line
    Solution 3 -> try to recompile another maven project + restart the IDE (works only sometimes)

  10. if you want to mavenize jar files -you can use this
  11. to set up you own maven repository you can try this tutorial
  12. And last but not least: Be Warned!

Read the following hints if you are an IDE-guy

  • Install the NetBeans plugin. Installation of this is easy -> Options->Plugins->Install Maven Plugin. To use this plugin simply open the pom.xml directly.
  • Installation of the EclipsePlugin:
    • install tomcat 6.0 server (>6.0.16)
    • you will need WTP >2.0.2
    • add http://m2eclipse.sonatype.org/update/ to your update pages; I didn’t tried q4e
    • and install m2eclipse
    • select the necessary packages (scm and mylyn didn’t worked for me …)
    • if not already done add a jdk as default virtual machine to eclipse
    • specify the same java exe in the eclipse.ini file:
      -vm
      C:\Program Files\Java\jdk1.6.0_07\jre\bin\javaw.exe
    • restart eclipse

Some people want to avoid the stuff related to IDEs – they can!

  • Install maven2
    • Under debian you can simply ‘apt-get install maven’.
    • Under windows you have to add the bin folder of maven to the PATH variable.
  • Type the following in the root directory
    1. mvn install
    2. mvn tomcat:run
  • Then go to http://localhost:8080/yourapp/ with you favorite firefox 😉

Now, my two questions to the readers are:

  • How can change the default tomcat-users.xml under target/tomcat? (for the command ‘mvn tomcat:run’)
    If I would require the user to login (security-constraints in the web.xml) I cannot login …
  • How can I use a master (or parent) module with web applications? I need an example! I already have one for a desktop application.

Encoding issues. Solutions for linux and within Java apps.

Puh, encoding! Did you ever have trouble with it? No? You must be a lucky guy!

Even none-developers should have problems with it e.g. if they use different operating systems.

What is encoding? And what’s so difficult with the encoding?

First encoding (better: character encoding) defines how characters have to be saved to be displayed correctly in your editor or look at the wikipedia definition to be correct. Update: here is a nice introduction.

For example if your editor only reads ASCII files all is very simple: it will use every 8 bits of the bitstream to get a number. Then it will interpret this number according to the ASCII-table. So, if it finds a 97 (this is 0x61 in hexadecimal) it prints ‘a’.

(BTW: look at this nice ASCII-art.)

But what if the encoding is another one? Or if even the bitstream should be splitted into 16-bits-packages instead of 8-bits-packages?

Then the user won’t see the correct information!

Second: On linux everything is in UTF-8. Windows uses CP 1252. and so on. Not good!

(With everything I means: clipboard, default file encoding, …)

How can you (as an end user) handle this under linux?

There are at least 4 programs that helps you with encoding issues under linux:

  • There are command line utilities in linux where you can determine automatically the encoding of a file: enconv and enca or open the file in firefox and go to View -> Encoding and view the detected encoding!
  • To change the encoding of file-content the editor kate is really great:
    Go to extras -> encoding and try it out.
  • Change the encoding of the content of several files which come from windows and you want to have them in linux then use recode:
    recode CP1252..UTF-8 *
    recode ISO-8859-1..UTF-8 *

    do the following to backup the original files:

    mkdir test && cp * test/ && cd test
  • Another command line utility is iconv (or here)
  • Change the encoding of the filenames with convmv (files e.g. from windows).
    To preview the change do:

    convmv -f cp1252 -t utf8 *

    To do the change:

    convmv --notest -f cp1252 -t utf8 *

How does Java handle encoding?

Java is platform independent one should think, but it isn’t regarding to the encoding.

For example: if you read a file correctly under linux, this could fail if you don’t specify the encoding explicitly, because it assumes it is utf8 and under windows it will use another default!

To override the default use: ‘java -Dfile,encoding=UTF-8’ or be explicit with the encoding! E.g read characters from a stream with the following lines:

BufferedInputStream iStream = new BufferedInputStream(urlConn.getInputStream());
InputStreamReader reader = new InputStreamReader(iStream, "UTF-8");

Another issue could be Java source files. They can have different encoding. You should use UTF8, because this is the encoding Java uses for its Strings.

In NetBeans 6.1 change it in the project properties (right-click on the project->properties)->Source->Encoding

In Eclipse 3.4 go to the preferences (menu Window) -> General ->Workspace->text file encoding

But this is only useful for desktop applications like my open source timetabler. But what if you do web development? All fine there? No not really. Then you might get additional problems with url encoding or xml parsing. For the latter one the fix is simple:

  • XML: <?xml version=”1.0″ encoding=”UTF-8″?>

But for url encoding the following does not really work:

  • JSP: <%@page contentType=”text/html; charset=UTF-8″ language=”java”%>

Apropos JSP – I had an encoding issue with the request. Try the following:

<% out.print(“RESPONSE character encoding=” + response.getCharacterEncoding() + ” “);
out.print(“REQUEST character encoding=” + request.getCharacterEncoding() + ” “);
out.print(“JVM encoding ” + System.getProperty(“file.encoding”) + ” “);

//EVEN here we get request parameter in wrong encoding
bean.setRequest(request);
%>

You will see that the request is null if I am not wrong. And then Java will use utf8? NO!

It will use ISO-8859-1! Why? It is written in the standard!

A simple request.setCharacterEncoding(“UTF-8”); would help if all browsers would send its request according to the header of the jsp. But this isn’t actually working for my use case. So I grabbed the strings from the request via this helper method:

private String toUTF8(String str) {
        try {
            return new String(str.getBytes("8859_1"), "UTF8");
        } catch (UnsupportedEncodingException ex) {
            return str;
        }
}

Update 1: Read this or this to get a better workaround with a javax.servlet.Filter, webserver parameters and jsp configs.

Update 2: The following snippets could be useful if you are using maven and want to make the application UTF-8 aware:

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

Update 3:

A good side with a lookup table for Unicode characters

http://unicode.coeurlumiere.com/

Summary

I invite you to post all your experiences with encoding problems in java.
E.g. how to force jboss or jetty to use utf8?

Real Time Editing with Eclipse and NetBeans

Do you know what real time editing is?

No, then look at this fantastic video to see this feature in Eclipse.

Or browse through this tutorial. It is really great.

I love NetBeans. So is there something similar for NetBeans? Yes! There is a collaboration plugin, but I tried it for myself and, sorry, it is really bad.

Update: You need a private server and then it should work

Here you have my bug list:

  • if I remove a contact, it will be listed the next login
  • if a disconnection happens then one can choose ‘don’t show this dialog again’. This does not work

So the previous bugs are not bad. But then when I tried it with a developer (of timefinder.de) it is only useable for a normal chat, because:

  • the editor will not being updated (after some minutes)!??
  • if you remotely execute the program you will do it several times, because you won’t get a feedback, wether starting was successful. thats awful.

But please proof me wrong and contact me via

  • Install the Developer Collaboration plugin in NetBeans 6.1 via: MenuBar->Tools->Plugins
  • Follow this collab-instructions. It is important to use the server: share.java.net and then add timefinder or peatar to your conversations.