Best java questions in March 2011

Is this a JVM bug or "expected behavior"?

48 votes

I noticed some unexpected behavior (unexpected relative to my personal expectations), and I'm wondering if something if there is a bug in the JVM or if perhaps this is a fringe case where I don't understand some of the details of what exactly is supposed to happen. Suppose we had the following code in a main method by itself:

int i;
int count = 0;
for(i=0; i < Integer.MAX_VALUE; i+=2){
  count++;
}
System.out.println(i++);

A naive expectation would be that this would print Integer.MAX_VALUE-1, the largest even representable int. However, I believe integer arithmetic is supposed to "rollover" in Java, so adding 1 to Integer.MAX_VALUE should result in Integer.MIN_VALUE. Since Integer.MIN_VALUE is still less than Integer.MAX_VALUE, the loop would keep iterating through the negative even ints. Eventually it would get back to 0, and this process should repeat as an infinite loop.

When I actually run this code, I get non-deterministic results. The result that gets printed tends to be on the order of half a million, but the exact value varies. So not only is the loop terminating when I believe it should be an infinite loop, but it seems to terminate randomly. What's going on?

My guess is that this is either a bug in the JVM, or there is a lot of funky optimization going on that makes this expected behavior. Which is it?

Known bug. Related to

http://bugs.sun.com/view_bug.do?bug_id=6196102

http://bugs.sun.com/view_bug.do?bug_id=6357214

and others.

I think they're considered low-priority to fix because they don't come up in the real world.

Final arguments in interface methods - what's the point?

31 votes

In Java, it is perfectly legal to define final arguments in interface methods and do not obey that in the implementing class, e.g.:

public interface Foo {
    public void foo(int bar, final int baz);
}

public class FooImpl implements Foo {

    @Override
    public void foo(final int bar, int baz) {
        ...
    }
}

In the above example, bar and baz has the opposite final definitions in the class VS the interface.

In the same fashion, no final restrictions are enforced when one class method extends another, either abstract or not.

While final has some practical value inside the class method body, is there any point specifying final for interface method parameters?

It doesn't seem like it. According to the Java Language Specification 4.12.4:

Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.

However, a final modifier on a method parameter is not mentioned in the rules for matching signatures of overridden methods, and it has no effect on the caller, only within the body of an implementation.

What's the advantage of having public static inner classes of an interface/class?

Asked on Tue, 22 Mar 2011 by DR java
29 votes

I've noticed the following code pattern while browsing through some sources of my project's 3rd party libraries:

public interface MyInterface {
    public static class MyClass1 implements MyInterface { ... }
    public static class MyClass2 implements MyInterface { ... }
    public static class MyClass3 implements MyInterface { ... }
}

Or this one:

public class MyBaseClass {
    public static class MyClass1 extends MyBaseClass { ... }
    public static class MyClass2 extends MyBaseClass { ... }
    public static class MyClass3 extends MyBaseClass { ... }
}

Real life examples:

  • SwingX: org.jdesktop.swingx.decorator.HighlightPredicate (Source)
  • Substance: org.pushingpixels.substance.api.renderers.SubstanceDefaultTableCellRenderer (Source)

What's the advantage of having a code structure like this?

My first thought was "aggregation", but the same thing could be achieved using plain old packages. So when/why is it better to use public inner classes instead of a package?

I think this is reasoned by aggregation, maybe they're also not worth it to create a top level class. I do this sometimes if something is to small to create a package (to separate them from others) but the corresponding classes should only used within the context of the top level class. In my opinion this is a design decision.

The decorator pattern may be a nice example, they can be applied on the top-level class but are maybe so simple they're not worth it to be also top-level. You can easily show the ownership by using them as inner classes.

That's not that visible at first glance with packages. You directly see the dependent class/interface.

Further it's possible to access the private fields of a class, this could be useful and is more fine-grained than the package private scope.

Why isn't LinkedList.Clear() O(1)

25 votes

I was assuming LinkedList.Clear() was O(1) on a project I'm working on, as I used a LinkedList to drain a BlockingQueue in my consumer that needs high throughput, clearing and reusing the LinkedList afterwards.

Turns out that assumption was wrong, as the (OpenJDK) code does this:

    Entry<E> e = header.next;
    while (e != header) {
        Entry<E> next = e.next;
        e.next = e.previous = null;
        e.element = null;
        e = next;
    }

This was a bit surprising, are there any good reason LinkedList.Clear couldn't simply "forget" its header.next and header.previous member ?

The source code in the version I'm looking at (build 1.7.0-ea-b84) in Eclipse have this comment above them:

// Clearing all of the links between nodes is "unnecessary", but:
// - helps a generational GC if the discarded nodes inhabit
//   more than one generation
// - is sure to free memory even if there is a reachable Iterator

That makes it reasonably clear why they're doing it, although I agree it's slightly alarming that it turns an O(1) operation into O(n).

How can I ensure the destruction of a String object in Java?

24 votes

An empoyee at my company needs to modify data from a SQL Server database through a program I made. The program used Windows authentication at first, and I asked the DBAs to give this specific user write access to said database.

They were not willing to do this, and instead gave write access to my Windows user account.

Since I trust the guy but not enough to let him work 90 minutes with my session open, I'll just add a login prompt to my program, asking for a username and password combination, and log in to SQL Server with it. I'll log in, and trust my application to let him do only what he needs to.

This, however, raises a small security risk. The password fields tutorial over SunOracle's site states that passwords should be kept the minimum amount of time required in memory, and to this end, the getPassword method returns a char[] array that you can zero once you're done with it.

However, Java's DriverManager class only accepts String objects as passwords, so I won't be able to dispose of the password as soon as I'm done with it. And since my application is incidentally pretty low on allocations and memory requirements, who knows how long it'll survive in memory? The program will run for a rather long time, as stated above.

Of course, I can't control whatever the SQL Server JDBC classes do with my password, but I hoped I could control what I do with my password.

Is there a surefire way to destroy/zero out a String object with Java? I know both are kind of against the language (object destruction is non-deterministic, and String objects are immutable), and System.gc() is kind of unpredictable too, but still; any idea?

I can only think of a solution using reflection. You can use reflection to invoke the private constructor that uses a shared character array:

  char[] chars = {'a', 'b', 'c'};
  Constructor<String> con = String.class.getDeclaredConstructor(int.class, int.class, char[].class);
  con.setAccessible(true);
  String password = con.newInstance(0, chars.length, chars);
  System.out.println(password);

  //erase it
  Arrays.fill(chars, '\0');
  System.out.println(password);

Edit

For anyone thinking this is a failproof or even useful precaution, I encourage you to read jtahlborn's answer for at least one caveat.

Why do people still use primitive types in Java?

23 votes

Since Java 5, we've had boxing/unboxing of primitive types so that int is wrapped to be java.lang.Integer, and so and and so forth. I see a lot of new Java projects lately (that definitely require a JRE of at least version 5, if not 6) that are using int rather than java.lang.Integer, though it's much more convenient to use the latter, as it has a few helper methods for converting to long values et al. Why do some still use primitive types in Java? Is there any tangible benefit?

Effective Java, Item 5: "Avoid creating unnecessary objects"

He posts the following code

public static void main(String[] args) {
    Long sum = 0L; // uses Long, not long
    for(long i = 0; i <= Integer.MAX_VALUE; i++) {
        sum += i;
    }
    System.out.println(sum);
}

and it takes 43 seconds to run. Taking the Long into the primitive brings it down to 6.8 seconds... if that's any indication why we use primitives.

The lack of native value equality is also a concern (.equals() is fairly verbose compared to ==)

for biziclop:

class biziclop {

    public static void main(String[] args) {
        System.out.println(new Integer(5) == new Integer(5));
        System.out.println(new Integer(500) == new Integer(500));

        System.out.println(Integer.valueOf(5) == Integer.valueOf(5));
        System.out.println(Integer.valueOf(500) == Integer.valueOf(500));
    }
}

Results in:

C:\Documents and Settings\glow\My Documents>java biziclop
false
false
true
false

C:\Documents and Settings\glow\My Documents>

Why is creating a Thread said to be expensive?

23 votes

The Java tutorials say that creating a Thread is expensive. But why exactly is it expensive? What exactly is happening when a Java Thread is created that makes its creation expensive? I'm taking the statement as true, but I'm just interested in mechanics of Thread creation in JVM.

Thread lifecycle overhead. Thread creation and teardown are not free. The actual overhead varies across platforms, but thread creation takes time, introducing latency into request processing, and requires some processing activity by the JVM and OS. If requests are frequent and lightweight, as in most server applications, creating a new thread for each request can consume significant computing resources.

From Java Concurrency in Practice
By Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea
Print ISBN-10: 0-321-34960-1

Java thread creation is expensive because there is a fair bit of work involved:

  • A large block of memory has to be allocated and initialized for the thread stack.
  • System calls need to be made to create / register the native thread with the host OS.
  • Descriptors needs to be created, initialized and added to JVM internal data structures.

It is also expensive in the sense that the thread ties down resources as long as it is alive; e.g. the thread stack, any objects reachable from the stack, the JVM thread descriptors, the OS native thread descriptors.

All these things are platform specific, but they are not cheap on any Java platform I've ever come across.


(A Google search found me an old benchmark that reports a thread creation rate of ~4000 per second on a Sun Java 1.4.1 on a 2002 vintage dual processor Xeon running 2002 vintage Linux. A more modern platform will give better numbers ... and I can't comment on the methodology ... but at least it gives a ballpark for how expensive thread creation is likely to be.)


(The above assumes "native threads" rather than "green threads", but modern JVMs all use native threads for performance reasons. Green threads are possibly cheaper to create, but you pay for it in other areas.)


I've done a bit of digging to see how a Java thread's stack really gets allocated. In the case of OpenJDK 6 on Linux, the thread stack is allocated by the call to pthread_create that creates the native thread. (The JVM does not pass pthread_create a preallocated stack.)

Then, within pthread_create the stack is allocated by a call to mmap as follows:

mmap(0, attr.__stacksize, 
     PROT_READ|PROT_WRITE|PROT_EXEC, 
     MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)

According to man mmap, the MAP_ANONYMOUS flag causes the memory to be initialized to zero.

Thus, even though it might not be essential that new Java thread stacks are zeroed (per the JVM spec), in practice (at least with OpenJDK 6 on Linux) they are zeroed.

What is the rationale behind this code block in java?

21 votes

What is the rationale behind making this kind of code valid in java? Does it exist for some particular reason or is it just a byproduct of other Java language design decisions? Can't you just use the consructor to achieve the same effect?

class Student
{
    {
        System.out.println("Called when Student class is instantiated.");
    }
}

One point is that it will execute whichever constructor is called. If you have several constructors and they don't call each other (for whatever reason, e.g. each wanting to call a directly-corresponding superclass constructor) this is one way of making sure the same code is executed for all constructors, without putting it in a method which could be called elsewhere.

It's also potentially useful when you're writing an anonymous class - you can't write a constructor, but you can write an initializer block. I've seen this used for JMock tests, for example.

Java to clojure rewrite

21 votes

I have just been asked by my company to rewrite a largish (50,000 single lines of code) Java application (a web app using JSP and servlets) in Clojure. Has anyone else got tips as to what I should watch out for?

Please bear in mind that I know both Java AND Clojure quite well.

The biggest "translational issue" will probably be going from a Java / OOP methodology to a Clojure / functional programming paradigm.

In particular, instead of having mutable state within objects, the "Clojure way" is to clearly separate out mutable state and develop pure (side-effect free) functions. You probably know all this already :-)

Anyway, this philosophy tends to lead towards something of a "bottom up" development style where you focus the initial efforts on building the right set of tools to solve your problem, then finally plug them together at the end. This might look something like this

  1. Identify key data structures and transform them to immutable Clojure map or record definitions. Don't be afraid to nest lots of immutable maps - they are very efficient thanks to Clojure's persistent data structures. Worth watching this video to learn more.

  2. Develop small libraries of pure, business logic oriented functions that operate on these immutable structures (e.g. "add an item to shopping cart"). You don't need to do all of these at once since it is easy to add more later, but it helps to do a few early on to facilitate testing and prove that your data structures are working..... either way at this point you can actually start writing useful stuff interactively at the REPL

  3. Separately develop data access routines that can persist these structures to/from the database or network or legacy Java code as needed. The reason to keep this very separate is that you don't want persistence logic tied up with your "business logic" functions. You might want to look at ClojureQL for this, though it's also pretty easy to wrap any Java persistence code that you like.

  4. Write unit tests (e.g. with clojure.test) that cover all the above. This is especially important in a dynamic language like Clojure since a) you don't have as much of a safety net from static type checking and b) it helps to be sure that your lower level constructs are working well before you build too much on top of them

  5. Decide how you want to use Clojure's reference types (vars, refs, agents and atoms) to manage each part mutable application-level state. They all work in a similar way but have different transactional/concurrency semantics depending on what you are trying to do. Refs are probably going to be your default choice - they allow you to implement "normal" STM transactional behaviour by wrapping any code in a (dosync ...) block.

  6. Select the right overall web framework - Clojure has quite a few already but I'd strongly recommend Ring - see this excellent video "One Ring To Bind Them" plus either Enlive or Hiccup depending on your templating philosophy. Then use this to write your presentation layer (with functions like "translate this shopping cart into an appropriate HTML fragment")

  7. Finally, write your application using the above tools. If you've done the above steps properly, then this will actually be the easy bit because you will be able to build the entire application by appropriate composition of the various components with very little boilerplate.

This is roughly the sequence that I would attack the problem since it broadly represents the order of dependencies in your code, and hence is suitable for a "bottom up" development effort. Though of course in good agile / iterative style you'd probably find yourself pushing forward early to a demonstrable end product and then jumping back to earlier steps quite frequently to extend functionality or refactor as needed.

p.s. If you do follow the above approach, I'd be fascinated to hear how many lines of Clojure it takes to match the functionality of 50,000 lines of Java

Should mutable collections override equals and hashCode?

19 votes

I was just wondering if it was a good idea to override equals and hashCode for mutable collections. This would imply that if I insert such a collection into a HashSet and then modify the collection, the HashSet would no longer be able to find the collection. Does this imply that only immutable collections should override equals and hashCode, or is this a nuisance Java programmers simply live with?

You should override equals and hashCode if your class should act like it were a value type. This usually is not the case for collections.

(I don't really have much Java experience. This answer is based on C#.)

What's the benefit of seeding a random number generator with only prime numbers?

19 votes

While conducting some experiments in Java, my project supervisor reminded me to seed each iteration of the experiment with a different number. He also mentioned that I should use prime numbers for the seed values. This got me thinking — why primes? Why not any other number as the seed? Also, why must the prime number be sufficiently big? Any ideas? I would've asked him this myself, but its 4am here right now, everyone's asleep, I just remembered this question and I'm burning to know the answer (I'm sure you know the feeling).

It would be nice if you could provide some references, I'm very interested in the math/concept behind all this!

EDIT:

I'm using java.util.Random.

FURTHER EDIT:

My professor comes from a C background, but I'm using Java. Don't know if that helps. It appears that using primes is his idiosyncrasy, but I think we've unearthed some interesting answers about generating random numbers. Thanks to everyone for the effort!

Well one blink at the implementation would show you that he CAN'T have any reason for that claim at all. Why? Because that's how the set seed function looks like:

synchronized public void setSeed(long seed) {
    seed = (seed ^ multiplier) & mask;
    this.seed.set(seed);
    haveNextNextGaussian = false;
}

And that's exactly what's called from the constructor. So even if you give it a prime, it won't use it anyhow, so if at all you'd have to use a seed s where (s^ multiplier) & mask results in a prime ;)

Java uses a usual linear congruency method, i.e.:

x_n+1 = (a * x_n + c) mod m with 2 <= a < m; 0 <= c < m.

Since you want to get a maximal periode, c and m have to be relatively prime and a few other quite obscure limitations, plus a few tips how to get a practically useful version. Knuth obviously covers that in detail in part2 ;)

But anyhow, the seed doesn't influence the qualities of the generator at all. Even if the implementation would be using a Lehmer generator, it would obviously make sure that N is prime (otherwise the algorithm is practically useless; and not uniformly distributed if all random values would have to be coprime to a non prime N I wager) which makes the point moot

Play! framework uses a <lot> of statics

17 votes

tl;dr: Waaah, the Play! framework has so many static methods. Where I go to school, we were told never ever to use any statics, yet Play! uses it like there's no tomorrow. Is that somehow okay? If so, why?

We (7 people and I) are planning to use the Play! framework for a project involving a web app. We decided to do it with Play! because it looks quite fun to do, all of us already know Java and the assignment is pretty hard so we wanted to focus on the actual assignment rather than also learning how to program in a different language.

We were always told, however, NEVER EVER to use 'static's in any Java program we developed, but when I look at Play! ... Well... about half the methods are static. </exaggeration>

I suppose, at the very least, we could use singleton objects (by using Scala, for example ^^) in order to program our project, but I'm quite concerned at how many statics there actually are in framework itself.

So, should I be concerned about this? Did the way the Play! developers programmed it make it so that all these statics don't pose a problem?

(For example, this thread has a rant about why static members should be avoided at all costs.)

Play uses static methods only when it makes sense:

  • in the controller layer, because controllers are not object oriented. Controllers act as mapper between the HTTP world (that is stateless, and request/response based) and the Model layer that is fully object oriented.
  • in the model layer for factory methods, like findAll(), count(), create() which of course don't depend of any particular instances
  • in some play.libs.* classes that provides purely utility functions

What are the Common Practices for Java Development on Linux?

14 votes

Hello, I'm trying to migrate from Windows to Linux as a Java development platform, and while the transition has generally been pretty painless, there are a few points of uncertainty that I'd like some feedback on. I'm running openSUSE 11.4, but I'm open to hear what works on other distros.

  1. Where do you install your JDK from? This one is surprisingly not as cut and dry as most people make it out to be. OpenJDk 6 is available in the openSUSE repositories, and was very easy to install. However it's currently update 21, and right now the Oracle release is at update 24. I'm used to a little alert in Windows notifying me that my Java needs updating but that doesn't appear to be the norm in Linux. Do Java developers forgo the JDK in their package manager and install the binary directly? Or is there another way?
  2. Where do you install Eclipse? There seems to be a general agreement online that Eclipse is best installed by simply downloading the binary and extracting it somewhere, but where's the usual place I would extract a program like Eclipse or Ant? I've seen votes for /usr/local and /opt online, but no definitive answer.
  3. Where do you put your Jetty/Tomcat? Similar to the eclipse question, where do most Linux Java developers put their Jetty/Tomcat/other container.
  4. What are some of the differences between the way you setup development versus production At the very least it seems I don't want to run my servlet container as root, that makes sense to me. But what other practices should I watch out for? Is there anything else that could make my development environment easier, but perhaps less secure?

I found this question was similar but ultimately too high level and didn't get into details of how actual developers are setting up their environment. If there's other resources you feel answer these questions, please share them here.

Thanks for your time.

Q> Where do you install your JDK from?
A> I never bother with other JDKs coming from outside Sun/Oracle mainly because our product is only certified to work with Sun/Oracle JRE. On my desktop, I run Kubuntu, but I never use apt-get for this but always download them manually. Reasons:

  • distro maintainers rarely rush to upgrade packages, as their primary concern is to make dependant apps (such as OpenOffice) work. If JDK changes from 1.6.0_20 to 1.6.0_21, they simply don't care. I might do because a newer patch might have an important bugfix or I simply want to try if my app still passes all the unit tests.
  • it might be a nightmare to retain old JDK versions. We still support older versions of our product and if I upgrade to a newer Kubuntu, I don't have guarantees that some ancient JDK will still be available as a package.
  • I am not sure some distros even support multiple existence of JDKs on the same machine.

My preference is to keep all JDKs/JREs in /opt and make a symlink to the newest one or the one I need most. I simply don't see why installing JDK manually is a problem.

I also set the PATH to the newest JDK/JRE.

Same thing (and similar arguments) apply to Ant and Maven.

Q> Where do you install Eclipse?
A> I use IntelliJ but the same applies. I store IDE in my home folder. This allows me to have different versions of it, update them without needing sudo, etc. I could as well install it in /opt but I guess I got this habit when I was downloading and testing newest IntelliJ IDEA EAP every week so I can quickly delete the older versions and do not pollute /opt. Finally, other programs might require Ant/Maven/JDK but it's only me who uses IntelliJ hence the different approach.

Q> Where do you put your Jetty/Tomcat?
A> I have a separate folder tomcats under /home where I have ~10 different Tomcat instances. Each of Tomcats is used for a different version of my app (we bundle Tomcat with our app). This is necessary because one deployment of our app can have different Tomcat settings (or even version) than another.

Q> What are some of the differences between the way you setup development versus production
A> It very much depends on your app. For example, we need some partitions to have lower access latencies but having less space (e.g. gigabytes for Lucene indexes) VS others which can have higher latencies but require more space (e.g. terabytes for content repositories). We, however, design our app so that all these different aspects can reside on different partitions which are configurable. Some partitions need to have special limitations (e.g. file upload) so this doesn't overflow other partitions. There is no simple one-for-all answer to this question, but obviously most of these concerns don't matter that much for a development environment.

Common Errors while writing Android Project

12 votes

Hi,

What are the Common Errors come while creating Android Project.

  • Android Specific Error
  • Java Specific Error

If you know just include known error got by You.

Thanks.

Hi,

I will list some errors i m always getting.

  • NullPointer error

    1. when i use un initialized variable or object we are creating. (Java)
    2. when we use some layout out view that is not in xml what we set in context.(Android)


  • ClassCast Exception
    • when a program attempts to cast a an object to a type with which it is not compatible. (eg: when i try to use a linear layout which is declared as a relative layout in xml layout).


  • StackOverflowError
    • it can also occur in correctly written (but deeply recursive) programs.(java and android)
    • when a program becomes infinitely recursive.
    • we create layout (deep and complex) that exceeds that stack of platform or virtual machine . recursive or too much of layout will create Stack overflow error in Android
    • Too many inner layouts.


  • ActivityNotFoundException: Unable to find explicit activity class exception

    • The activity is not declared in manifest.


  • Android securityException
    • You need to declare all permission in the application Manifest that your application check this link (internet, access to contact,gps,wifi state,write to SDCard, etc).


  • OutofMemoryError
    • when a request for memory is made that can not be satisfied using the available platform resources . mainly using bit map, gallery , etc.


  • Application Not Responding (ANR)

    • Mainly comes when you are making network function,or some long process.

    this will block UI Thread so user can not do any work. to avoid ANR read this

This are thing i mainly get while creating Android Project.

Try to use Try - Catch block in All Place of program.
Use proper Naming conversion for all variable and ID's in Layout.


I read one article from net it contains some error now i am adding that alos if it have redundancy please forgive me.

Issue : My previously nice RelativeLayout is making an ugly heap or some elements aren't visible anymore...What's going on ??? I just moved an element in it... Solution : Never forget that in a RelativeLayout, elements are referenced and placed in relation to their neighbours. Maybe there is something wrong in the hierarchy of relationship between your element. Try opening the outline view in Eclipse and clicking each element to see where there is a rupture.

Issue : Circular dependencies cannot exist in RelativeLayout Solution : You have probably written the same dependency in two different way. For instance an ImageView as the attribute android:layout_toRightOf a TextView and the TextView has android:layout_toLeftOf the ImageView. Only one of them is necessary

Issue : I wrote a style for one of my view/layout, but when I apply it in my xml, I have no display in the layout viewer of Eclipse Solution : Unfortunately, this seems to be a bug of the android ADT, I reported it but no news so far. Anyway, no panic, styles are working well, but they aren't displayed properly in Eclipse. Just build the app and launch it on the emulator or phone and you will see if everything is fine or not.

Issue : Toast is written properly but nothing is displayed Solution : This is a common error of use : just add the .show() method to show the Toast and see if it is working well

issue : I tried to display a String from strings.xml but I just had a number like 0x7f060001 Solution : This is not a bug, just a display due to the way android deals with resources. When you want to retrieve a resource, you have to use a method like getString(R.id.something), getDrawable, …Otherwise, you just display the reference written in the R class

Issue : Some change in code doesn't have any effect in the application Solution : there are 2 options, either you have forgotten something like the .show() of the Toast, or the emulator is not updating properly your application. In that case, you have to check the option “Wipe user Data” in your launch configuration of the emulator in Eclipse.

Issue : How to display borders on my table? Solution : There is no direct way to do that in android, you have to use a trick :http://www.droidnova.com/display-borders-in-tablelayout,112.html

Issue : the emulator is writing in japaneese withtout you having changed any parameter Solution : This happens sometimes, quite easy to fix, just long click in any EditText field, and change the input type to what you want

Issue : I can't get the Context Menu to appear in the emulator Solution : long click on emulator does not seem to register on every kind of view, you have to push the button in the center of the 4 directionnal arrows

issue : I'm following a tutorial about map route but I can't get it work, android does not find a package Solution : You might have been following a tutorial written for 1.5 SDK. At this time, there was a package to display route in android, but it was removed in the next SDK and is not available anymore. It just not possible anymore. There seems to be a trick with KML files but nothing official

Issue : Sending coordinates to the emulator gives wrong position Solution : ensure that you wrote the coordinate like 51.16548 and not 51,16548 nor 5116548

issue : Only the original thread that created a view hierarchy can touch its views. Solution : You must have tried to update a view content from another thread than the UI thread. 2 options to patch this : either create a handler in your UI thread and post your Runnable to this handler OR use the method runOnUIThread to run the lines of code that are doing the update

Issue : accessing localhost 127.0.0.1 doesn't work Solution : it works, you are just not doing it the right way : use 10.0.2.2

Thanks,

Ganapathy.C

Reading multiple NFC tags simultaneously in Android

11 votes

The new 2.3.3 SDK includes improved NFC support, and I'd like to write an app that relies on this. Ultimately, I'd like to be able to read data from multiple tags that enter the field simultaneously, but Android only seems to trigger on the first one that enters the field - subsequent ones are ignored.

From reading the NFC spec (ISO/IEC 14443-3) for the tags I'm using (Mifare Classic), I should be able to send a 'halt' command to the tag, which will cause it to stop responding, and allow me to read the next tag in the field. Android doesn't support the halt command directly on any of the TagTechnology subclasses, so I tried sending it myself directly using transceive(new byte[] {0x50, 0x00}). This throws an IOException, with the message 'transceive failed'.

Admittedly I'm doing all this from the main thread, which I understand is a bad idea, but I just want to test the concept as easily as possible.

Is it possible to communicate with multiple tags in the field at the same time? What am I doing wrong?

What you want is unfortunately not possible at the moment, unless you do some pretty advanced hackery, which is almost never a good idea :)

Probably you could halt the card if you send the right bytes in the transceive(), as you're trying to do now. But since the halt (or rather, the HLTA which you're trying to send) is an ISO14443-3A command, this will not work through the MifareClassic interface - which uses an "encrypted" pipe. Directly transmitting over the NfcA interface unfortunately doesn't work with the current stack either.

Even if you could get the card to halt, this will not automatically cause the NFC chip in the phone to resume polling for new tags - since you are "going around" the stack.

Irrational number representation in any programming language?

9 votes

Does anyone know of an irrational number representation type/object/class/whatever in any programming language?

All suggestions welcome.

Simply put, if I have two irrational objects, both representing the square root of five, and I multiply those objects, I want to get back the integer five, not float 4 point lots o' 9s.

Specifically, I need the representation to be able to collect terms, not just resolve every time to an integer/float. For instance, if I want to add the square root of five to one, I don't want it to return some approximation integer/float, I want it to return an object that I can add/multiply with another irrational object, such that I can tell the object to resolve at the latest time possible to minimize the float approximation error.

Thanks much!

What you are looking for is called symbolic mathematics. You might want to try some computer algebra system like Maxima, Maple or Mathematica. There are also libraries for this purpose, for example the SymPy library for Python.

RESTful MySQL / Terminology / Passing Parameters / Returning Ints & Doubles

8 votes

So, in an attempt to create a RESTful frontend to a MySQL database, I've briefly looked at phprestql (easy & simple, but just too simple) and now I'm attempting to build it onto NetBeans' tutorial. I've got the basic tutorial completed and working with my database just fine. However, I'm trying to figure out how to customize it a bit.

  1. All the results in JSON seem to be strings, even though in the MySQL table properties are Big Ints, Ints, and Doubles. The types also seem to be set correctly within the netbeans sources as well. However, JSON returns everything as strings. Any ideas where to address this? (Again, I'm just working from the tutorial above, albeit with my DB.)

  2. I'm also trying to figure out how I can implement additional parameters in the URI, to further refine the DB results. (http://localhost/the_db/people_table/?gender_property=male&updated_property=2011-01-18) ... would return all people rows that fit those criteria. Part of my problem is I'm not even sure of the proper terminology for this kind of feature, so it's making it a little difficult to find examples and tutorials on it.

  3. This may be related to the previous item, but I'd also like to use the URI to "drill-down" into the table/row/property to return individual values (in JSON) ... (http://localhost/the_db/people_table/42/lastname) ... would return {"Jones"}

Part of the problem is that I barely know Java from Ruby from Python. I'm pretty familiar with breaking things in Objective-C, PHP, and Perl though. However, tutorials for quick and easy Restful MySQL services with those don't seem very popular or prevalent.

[EDIT]

To the extent that this helps answer question #1, I'm attaching some of the java methods to indicate how the numerical properties are set/retrieved ... from what I can tell the actual JSON generation is automated by some library. I don't see it in here:

/** in the MySQL CommitteeObj table, the committeeId is set as follows */
/* `committeeId` bigint(11) NOT NULL auto_increment */

/** in committee.java */
public class committee implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "committeeId")
    private BigInteger committeeId;
//.....
}

public committee(BigInteger committeeId) {
    this.committeeId = committeeId;
}

@Override
    public String toString() {
        return "texlege.committee[committeeId=" + committeeId + "]";
    }

/** in committeeConverter.java */

@XmlElement
public BigInteger getCommitteeId() {
    return (expandLevel > 0) ? entity.getCommitteeId() : null;
}

public void setCommitteeId(BigInteger value) {
    entity.setCommitteeId(value);
}

/** in committeeResource.java */

@GET
    @Produces({"application/json"})
    public committeeConverter get(@QueryParam("expandLevel")
                                  @DefaultValue("1")
    int expandLevel) {
        return new committeeConverter(getEntity(), uriInfo.getAbsolutePath(), expandLevel);
    }

protected committee getEntity() {
        try {
            return (committee) em.createQuery("SELECT e FROM committee e where e.committeeId = :committeeId").setParameter("committeeId", id).getSingleResult();
        } catch (NoResultException ex) {
            throw new WebApplicationException(new Throwable("Resource for " + uriInfo.getAbsolutePath() + " does not exist."), 404);
        }
    }
}

And here's the output from a query of a specific committee. Notice the distinct lack of JSON numbers for the committeeId, committeeType, and parentId properties:

{
    "@uri":"http://localhost:8080/TexLegeRest/rest/committees/2735/",
    "clerk":"Amy Peterson",
    "committeeId":"2735",
    "committeeName":"Appropriations",
    "committeeType":"1",
    "parentId":"-1",
    "updated":"2011-02-20T00:00:00-06:00",
}

In short, this answer may not be what you are looking for at all as it's nothing to do with NetBeans. However it does provide a different way of doing what you want in providing a RESTful interface to a MySQL database.

I have uploaded a zip file with 4 Java files, 2 XML files and 1 text file to support this solution, otherwise the answer would have been very long.

In short this is a Maven/Java/Spring/Hibernate/MySQL solution, the reason being is that I have been using this architecture recently and found it quite simple and powerful to do what is really just converting SQL ↔ JSON!

This solution also uses a few other tools like Maven for compiling/packaging/deploying rather than an IDE, which in my opinion removes a level of complexity, but might put a few IDE-loving people off.

System Configuration

So firstly you will need to download and unzip/install Java and Maven if you don't have those already. I'll also assume Windows, mainly because that is what I am currently using. I have these installed the above applications in the following locations:

c:\apps\java\jdk1.6.0_24
c:\apps\apache-maven-3.0.3

Since there is no IDE in this solution, the application is built and run from the command line. There is a tiny amount of configuration here, so just execute the following to set up some environment variables:

set JAVA_HOME=c:\apps\java\jdk1.6.0_24 Enter

set M2_HOME=c:\apps\apache-maven-3.0.3 Enter

set PATH=%PATH%;%M2_HOME%\bin;%JAVA_HOME%\bin Enter

Typing mvn --version can then be used to verify that Java and Maven are installed and found correctly.

Project Creation

Create a directory for your source, let's use c:\src\project1

On the command line again, navigate to that directory and execute:

mvn archetype:generate -DgroupId=my.group -DartifactId=project1 -DarchetypeArtifactId=maven-archetype-quickstart

Maven will download some standard libraries and eventually prompt you to "Define value for property 'version':" - just Enter to continue. Maven will then ask you to confirm the project settings so just hit Enter again to confirm. You will end up with a directory structure in which you will find a pom.xml file and two Java files. The Project Object Model (POM) file tells Maven how to build/test/package/deploy (and more) your project. You need to add some libraries to that file so that we can use Spring, JSON, Jetty and other functionality. So edit the pom.xml adding the following to the XML structure:

Under <project> element (i.e. as a sibling of the <url> element) add:

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <spring.version>3.0.5.RELEASE</spring.version>
</properties>
<repositories>
  <repository>
    <id>JBoss</id>
    <url>https://repository.jboss.org/nexus/content/groups/public/</url>
    <releases>
      <enabled>true</enabled>
      <updatePolicy>always</updatePolicy>
      <checksumPolicy>warn</checksumPolicy>
    </releases>
  </repository>
</repositories>

Under <dependencies> element add the contents of the dependencies.txt file from the zip file linked above. Those changes will allow Maven to find the latest Hibernate and Spring libraries which are not always present in the default Maven repositories and also other libraries like the HSQLDB - an in-memory database used to test this example and JSON ↔ Java conversion.

Also under the <project> element (I added this just after the </dependencies> element) add the following:

<build>
  <plugins>
    <plugin>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>maven-jetty-plugin</artifactId>
      <version>6.1.26</version>
      <configuration>
        <contextPath>/${project.artifactId}</contextPath>
        <scanIntervalSeconds>10</scanIntervalSeconds>
        <webXml>${project.build.directory}/${project.build.finalName}/WEB-INF/web.xml</webXml>
      </configuration>
    </plugin>
  </plugins>
</build>

This is the embedded web server that we will use to run the .war file you are about to build, which leads us to the final change in the pom.xml… the <packaging> element near the top of the file needs to be changed to war instead of jar.

On the command line again, navigate to the project directory you just created where the pom.xml is (probably cd project1) and type mvn compile. Maven should download all he new libraries we just added in the POM and hopefully compile without error. Now we need to just configure Spring to wire up all the RESTful URLs & beans and configure the web application itself.

Create 2 new directories under /src/main called resources and webapp/WEB-INF. Your directory structure should now look like this:

src
src/main
src/main/java
src/main/resources
src/main/webapp/WEB-INF

In the resources add the file called applicationContext.xml from the zip file. The ApplicationContext is the configuration for the application.

In the WEB-INF directory add the file called web.xml from the zip file. The web.xml describes how a web container (e.g. Tomcat or in our case Jetty) should deploy the application.

Now we need to add in some code! Instead of adding the code here and making this answer longer than it already is, the zip file contains 4 classes. Simply copy those into the src/main/java/my/group directory, overwriting App.java in the process.

Compilation and Execution

This is where you should cross your fingers… as you should be able to use mvn compile to compile the classes and then if successful mvn jetty:run-war to run the web server with the application war file. If there are no errors in starting the application, there should be some logging that looks like INFO: Mapped URL path [/people] onto handler 'app' as the initialization of Jetty finishes.

Testing the REST Interface

Now we can test the RESTful URLs. I recommend using the Poster addon for Firefox (not compatible with Firefox 4 though), so install this and we can use it to do PUT and GET requests on the project1 web-app. Once installed either select Tools → Poster or Ctrl+Alt+P.

Firstly, since we are using Spring content negotiation(scroll down to the Content Negotiation section) you will need to configure Poster to add the correct Content Type. Just add application/json to this field. To add a person to our database, just add

{"firstName" : "foo", "lastName" : "bar"}

to the body (this is the large area in the Poster addon) and use the PUT button. You should get a reponse back from the web-app and see logging on the command window. The response should be:

{"name":"foo bar","id":1,"height":1.8}

This is valid JSON and you can see integers and doubles are appearing just fine. If you have a look at the Person.java class from the zip file, you can see that firstName and lastName are the names of the actual class members which match the names of the JSON keys that were PUT. I have added a @JsonIgnore annotation to those and created a different @JsonProperty to return the full name instead. In practice you probably would not do this otherwise it would be difficult to update just the first or last name but in this example I am just using it to show that you have full control of the JSON entities returned and their names/values. Also note the Person class has a hard-coded Double (the height member) to demonstrate that numbers are serialized correctly to JSON.

You can then retrieve person 1 by changing the URL to http://localhost:8080/project1/people/1 and using the GET button instead, which just returns the same JSON.

Swapping HSQLDB for MySQL

You might have noticed that there is no MySQL database so far. You will need to change some of the configuration to point to a MySQL database instead of the in-memory HSQL database that was used up until now. The "dataSource" bean properties should be updated like so:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
  p:driverClassName="com.mysql.jdbc.Driver"
  p:url="jdbc:mysql://localhost:3306/name_of_your_database_instance"
  p:username="your_database_username"
  p:password="your_database_password"/>

where you need to specify the connection details of your database.

Lastly the hibernate dialect needs updating to be the MySQL one, so replace the org.hibernate.dialect.HSQLDialect with

<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>

in the applicationContext.xml file.

Be warned that when Hibernate is configured with the following property <prop key="hibernate.hbm2ddl.auto">create</prop> it will destroy the database when the web application is started, since Hibernate is scanning the Person class and creating the table from the @Table, @Entity and @Column (and other) annotations.

Please note that unfortunately I've not tested this last part as I don't have MySQL installed but hopefully it should work.

Answering your questions (or not)

1) No idea, sorry. It looks like just a conversion/serialization problem which could probably be solved looking at what beans are used in the tutorial and checking their documentation.

2) and 3) With the solution above you can add as many path variables as you require, e.g.

@RequestMapping(value = "/people/gender/{gender}/updated/{lastUpdated}", method = RequestMethod.GET)
@ResponseBody
public Person findByGenderAndUpdated(@PathVariable String gender, @PathVariable String lastUpdated) {}

It might not be practical to create an API to expose the individual properties. Returning full resources by your URLs is more practical and then just let the downstream component to pick out the lastName from a Person JSON object if that is all it needs. However I could see the need for a cut-down JSON representation of a Person if there are is a lot of data. i.e. it is more efficient from a bandwidth perspective to exlucde certain large data properties.

You would have to implement these methods yourself for each combination. This effectively constitutes your RESTful API. If you need to create a document to describe your API then the Atlassian guidelines are very well written.

Summary

There are many variations on this solution and in practice you should put the classes in a better directory structure (model, view, controller) and create some Java class templates for handling the persistence, since all models will probalby need a "save", "find" method for example.

I hope this solution is useful to someone :-)

What is the life span of an ajax call?

6 votes

let's say I have this code in javascript:

function doAnAjaxCall () {
    var xhr1 = new XMLHttpRequest();
    xhr1.open('GET', '/mylink', true);
    xhr1.onreadystatechange = function() {
        if (this.readyState == 4 && this.status==200) {
            alert("Hey! I got a response!");
        }
    };
    xhr1.send(null);
}

and let the code in the servlet be:

public class RootServlet extends HttpServlet {
    public void doGet (HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.getWriter().write("What's up doc?");
        resp.setStatus(200);
    }
}

Will xhr1 still wait for new changes in readystate? Or it is closed as soon as it gets the first response? If it remains open, will it lead to memory leaks/slower browser after a while and accumulating a few of those? Should I always call resp.getWriter().close() at the end of the servlet code?

And, lastly, for the jQuery fans out there:

does $.ajax() behave as XMLHttpRequest() in that respect?

Will xhr1 still wait for new changes in readystate? Or it is closed as soon as it gets the first response? If it remains open, will it lead to memory leaks/slower browser after a while and accumulating a few of those?

Behind the scenes, it remains open. It (and the memory occupation) is however the responsibility of the webbrowser engine. It maintains a certain amount of connections in a pool which has a maximum limit per domain anyway. MSIE for example has a bug which causes them to leak away when they are still running while the user unloads (closes) the window.

Should I always call resp.getWriter().close() at the end of the servlet code?

Not necessary. The servletcontainer will close it anyway. Closing it yourself only prevents the risk of some (buggy) code further in the response chain from writing to the response body. For more detail, see this answer.

And, lastly, for the jQuery fans out there: does $.ajax() behave as XMLHttpRequest() in that respect?

It uses XMLHttpRequest under the covers (only when supported by the browser; otherwise it's the MSIE ActiveX object). It constructs a new one on every call. Open the unminified source code, Ctrl+F the jQuery.ajaxTransport( function. All the ajax handling code is almost 200 loc and it covers all possible browser specific bug fixes you can think about.

Any news about soap handling in Java?

5 votes

I've worked with Axis and Saaj for a few years, then I've switched to spring-ws, which I prefer because it's more xml-oriented. Then I've switched all my projects to axiom due to a serious performance problem with saaj. I've experienced some problems again, especially related to namespace handling (AXIOM-114 and SWS-502), however I've been working with spring-ws and axiom since two years.

Since I'm not completely satisfied, I'm actually looking for any other libraries. Any suggestions?

I know that google is my best friend, but this is a delicate matter, so I'd like to know your experience. Actually, what is the best library to handle soap messages in Java?

I could write my own library based on my needs, but I think this is not too easy and I don't like to reinvent the wheel. I just would like to rely on a library which correctly handles well formed xml, namespaces declaration, multiple levels of nested xml, and so on.

Thank you

I'm actually looking for any other libraries. Any suggestions?

Well, SAAJ is still SUN's specification to handle SOAP (with attachments).
The reference implementation uses DOM so if you exchange big messages you would see memory issues.
Other frameworks that support soap-based web services besides axis and spring (that you have already worked with) is cxf.
Axis uses axiom for the xml handling and has an implementation of SAAJ that I think it is not DOM-based. So better memory usage for large messages is expected.
CXF uses by default the build-in java saaj implementation but can also support axis2-saaj
CXF FAQ

Last but not least is the JAX-WS (also relies on SAAJ).
Both CXF and JAX-WS allow you to work directly on the xml level.
This is pretty much the most popular frameworks for soap web services in java (If I forgot something I hope someone will come in and inform).

what is the best library to handle soap messages in Java?

Hard to tell since this is pretty broad question.
Generally speaking the most popular frameworks are Axis2 and CXF (this is my personal understanding).
IMHO all the libraries have the problem that they are too "sensitive" on input i.e. namespaces etc.
On occassion that the developer needs a more relaxed parsing, this is not easy to get.

java - split string using regular expression

5 votes

I need to split a string where there's a comma, but it depends where the comma is placed.

As an example

consider the following:

C=75,user_is_active(A,B),user_is_using_app(A,B),D=78

I'd like the String.split() function to separate them like this:

C=75 

user_is_active(A,B) 

user_using_app(A,B)

D=78

I can only think of one thing but I'm not sure how it'd be expressed in regex.

The characters/words within the brackets are always capital. In other words, there won't be a situation where I will have user_is_active(a,b).

Is there's a way to do this?

If you don't have more than one level of parentheses, you could do a split on a comma that isn't followed by a closing ) before an opening (:

String[] splitArray = subjectString.split(
    "(?x),   # Verbose regex: Match a comma\n" +
    "(?!     # unless it's followed by...\n" +
    " [^(]*  # any number of characters except (\n" +
    " \\)    # and a )\n" +
    ")       # end of lookahead assertion");

Your proposed rule would translate as

String[] splitArray = subjectString.split(
    "(?x),        # Verbose regex: Match a comma\n" +
    "(?<!\\p{Lu}) # unless it's preceded by an uppercase letter\n" +
    "(?!\\p{Lu})  # or followed by an uppercase letter");

but then you would miss a split in a text like

Org=NASA,Craft=Shuttle