Categories
discuss

Maven and GWT (and Eclipse) – Does it really work?

Over the past few days I have been trying to create/run a project in Eclipse using the gwt-maven-plugin and keep running into roadblocks (see some of my previous questions). I like to use Maven to do my builds, but I’m at the point where I’m thinking of going the Ant build route because of the complications of using Maven.

Does anyone out there have it configured/working well? Is it just me or is this harder than it should be?

Answer

After much frustration trying to get things to play nicely together, this is the setup I have that “works” for me. “Works” meaning that I can create, run and debug a GWT project with tweaks, but it isn’t the most elegant solution.

Create Project

Much of the steps are the same as Pascal’s answer in this post: Maven GWT 2.0 and Eclipse. I’ll list mine out to be clear.

In Eclipse (Helios) with m2eclipse and GWT Eclipse plugins installed:

Create a new Maven project using the gwt-maven-plugin archetype

Modify the pom.xml:

  • set <gwt.version property> to 2.0.4 (needs to be same as GWT Eclipse Plugin version)
  • set <maven.compiler.source> and <maven.compiler.target> properties to 1.6
  • remove <goal>generateAsync</goal> from gwt-maven-plugin <plugin> config
  • add maven-war-plugin to pom.xml

maven-war-plugin example:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.0.2</version>
    <configuration>
    <warSourceDirectory>war</warSourceDirectory>
        <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
    </configuration>
</plugin>

Update project Properties:

  • Google -> Web Toolkit, check the “Use Google Web Toolkit” box, and ensure “Use default SDK (GWT-2.0.4) is selected.

Run Maven “gwt:eclipse” goal on project (sets up environment and launch config)

Copy *.launch file to workspace.metadata.pluginsorg.eclipse.debug.core.launches

Restart Eclipse

Compile/Run Project

I created a Run Configuration that does mvn clean compile gwt:run. The gwt:run is necessary to copy the resources and lib jars into the war directory. However, it does not copy the web.xml from src/main/webapp/WEB-INF into war/WEB-INF/. So, I have to manually copy that file.

If I want to run my application, the above step is sufficient. However, if I want to debug the application, I launch it by choosing the Google “Web Application” configuration from Debug Configurations that was created when the .launch file was copied previously. This configuration allows for debugging (breakpoints etc.) without any other config or need for remote debugging.

Categories
discuss

x Escape in Java?

I was wondering if there is a similar hex (x) escape in Java like there is in C++. For example:

char helloworld[] = "x48x45x4Cx4Cx4Fx20x57x47x52x4Cx44";
printf("%s", helloworld);

There is no hex (x) escape in Java from what it appears so far. Is there an alternative that is just as easy to use without having to concat a bunch of hex numbers together?

Answer

Strings in Java are always encoded in UTF-16, so it uses a Unicode escape: u0048. Octal characters are supported as well: 110

Categories
discuss

Why use a prime number in hashCode?

I was just wondering why is that primes are used in a class’s hashCode() method? For example, when using Eclipse to generate my hashCode() method there is always the prime number 31 used:

public int hashCode() {
     final int prime = 31;
     //...
}

References:

Here is a good primer on Hashcode and article on how hashing works that I found (C# but the concepts are transferrable): Eric Lippert’s Guidelines and rules for GetHashCode()

Answer

Because you want the number you are multiplying by and the number of buckets you are inserting into to have orthogonal prime factorizations.

Suppose there are 8 buckets to insert into. If the number you are using to multiply by is some multiple of 8, then the bucket inserted into will only be determined by the least significant entry (the one not multiplied at all). Similar entries will collide. Not good for a hash function.

31 is a large enough prime that the number of buckets is unlikely to be divisible by it (and in fact, modern java HashMap implementations keep the number of buckets to a power of 2).

Categories
discuss

Can you provide a hint to a mobile device that an HTML input field is numeric-only?

I’ve been doing some usability testing on a website I’m developing, using an Android phone and an iPod touch. In both cases, when entering a numeric field (time, distance in metres, etc) I have to take a manual step to change the keyboard layout to show digits. As the app will only accept numeric values in the textbox, I’d like to save the user the inconvenience and tell the device “switch to the number pad for this input”.

Is this possible?

Answer

After some browsing, it seems that HTML 5 offers some answers for this, though they’re not widely supported.

Specifically, the number type input:

<input type="number"
       min="0"
       max="10"
       step="2"
       value="6">

Here’s a demo. Works on Chrome 6.0.472.51 (beta). Have yet to test on Android/iPhone but apparently it works.


(source: wearehugh.com)

Categories
discuss

How to compute shortest unique prefixes of a set of strings?

It’s a pretty common algorithm in command line parsing. Given a set of predefined long option names — compute the shortest prefix that uniquely identifies one of these options. So for instance, for the following options:

-help
-hostname
-portnumber
-name
-polymorphic

This would be the output:

-he
-ho
-por
-n
-pol

I’m thinking two possible ways of doing this — either as a tree:

               *
             / | 
            /  |  
           H   N   P
          /       |
         E   O     O
                  / 
                 R   L

Or by searching for substrings:

for (String s : strings) {
   for (int i = 1; i < s.length(); s++) {
      if (search(strings,s.substring(0,i)) == 1) {
          result.add(s.substring(0,i);
          break;
      }
   }
}

So, question is:

  1. Which would you go for?
  2. Am I missing an obvious third way?

Answer

The “tree” solution is a special case (well, actually it’s pretty general) of a Patricia trie.

The first will generally be faster to lookup. The memory considerations probably aren’t relevant to your context, since it’s not permanently used and you’re only performing the “lookup” once.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..