java

Easy way to store and retrieve objects in Java without using a relational DB?

Do you know of an "easy" way to store and retrieve objects in Java without using a relational DB / ORM like Hibernate? [Note that I am not considering serialization as-is for this purpose, as it won't allow to retrieve arbitrary objects in the middle of an object graph. Neither am I considering DB4O because of its restrictive license. T...

Callback functions in Java

Is there a way to do pass a call back function in a Java method? The bahaviour I'm trying to mimic is a .Net Delegate being passed to a function. I've seem people suggesting creating a separate object but that seems overkill, however I am aware that sometimes overkill is the only way to do things. ...

Java toString() using reflection?

I was writing a toString() for a class in Java the other day by manually writing out each element of the class to a String and it occurred to me that using reflection it might be possible to create a generic toString() method that could work on ALL classes. I.E. it would figure out the field names and values and send them out to a String...

Cons for defining logger non-static

Comments for this answer http://stackoverflow.com/questions/212697/how-do-you-reduce-java-logging-boilerplate-code#212753 strongly suggest not to use loggers as instance member variables. I can think of two negative side-effects: 1) superclass logs with the subclass's logger 2) object cannot be serialized (unless marked transient) But...

Collections.max function for iterable<Integer> in java

The Java Collections.max takes only a collection of a sortable object. However since the collection is not necessarily sorted, I don't see any reason not to implement the same max function for iterable types. Is there a max method for Iterable<T extends Comparable<? super T>> in java's standard library? ...

Why can't enum's constructor access static fields?

Why can't enum's constructor access static fields and methods? This is perfectly valid with a class, but is not allowed with an enum. What I'm trying to do is store my enum instances in a static Map. Consider this example code which allows lookup by abbreivation: public enum Day { Sunday("Sun"), Monday("Mon"), Tuesday("Tue"), Wedne...

How do I encode URI parameter values?

I want to send a URI as the value of a query/matrix parameter. Before I can append it to an existing URI, I need to encode it according to RFC 2396. For example, given the input: http://google.com/resource?key=value1 & value2 I expect the output: http%3a%2f%2fgoogle.com%2fresource%3fkey%3dvalue1%2520%26%2520value2 Neither java.net.UR...

Are lots of inheritied single-plugin profiles in Maven a good idea?

In our infrastructure, we have lots of little Java projects built by Maven2. Each project has its own pom.xml that ultimately inherits from our one company "master" parent pom. We've recently started adding small profiles to our parent pom, disabled by default, that, when enabled, execute a single plugin in a conventional manner. Examp...

using java and usb: Which api? jsr-80, jusb, ...?

Hi all, I would like two java programs to communicate directly over the USB without going through a device or cable. Don't ask me why, I don't know the details or reasoning behind this decision. I have found jusb and jsr-80 to be two java/usb APIs to do this. They work on linux AND windows - I will need capabilities on both. Questio...

Objective-C Delegation Explained to a Java Programmer

I am fairly new to Objective-C, but experienced in Java. Is there the equivalent concept of Objective-C "delegation" in Java, so that I may understand this notion better? Would there be a way to emulate the delegation concept in Java? ...

Regex to block all < in a String

I'm trying to create a Regex to block all < and > in a String except when used with <select>. Can anyone suggest a Regex for that? I'll be using it with javax.util.Pattern. I'm trying to write a solution to block the injection attack and XSS attempts through request and URL. For that, I'll be blocking the special characters and charact...

How do I force a spring container not to return a singleton instance of a bean?

When I call getBean(name) on a BeanFactory, I get back an instance of the bean defined in the application context. However, when I call getBean(name) again (with the same name,) I get the same instance of the bean back. I understand how this would be desirable in some (many?) cases, but how do I tell the BeanFactory to give me a new in...

strange HashMap.put() behaviour

I am attempting to troubleshoot an intermittent failure that appears to be related to removing an object from a HashMap and then putting that same object back using a new key. My HashMap is created as follows: transactions = new HashMap<Short, TransactionBase>(); The code that does the re-assignment is as follows: transactions.remov...

Install of Tomcat 5.5 on Ubuntu (using apt) leaves CATALINA_HOME unset

Hi, I'm setting up my development environment for writing web applications using Java, and I'm in need of some help. What I have done: Installation of Java SE 6 from Sun I installed (sudo apt-get install …) the following packages (one at a time as some of them requires user interaction) to get Java SE 6 from Sun: sun-java6-bin sun-ja...

How to generate Java code from an XSD that includes MSFT Serialization: GUID datatypes?

I have used the Jaxme 2 libraries before to generate Java code from .XSD files without a problem. I'm currently encountering a problem generating Java from an XSD file that contains a http://schemas.microsoft.com/2003/10/Serialization/ namespace. Some sample code from my .XSD is: <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns...

Escaping Special Characters Posted to Servlet

I have a jsp containing a form which posts to a servlet, when the servlet receives the parameters from the form the pound sign (£) is preceded with the following character Â. So £ becomes £. What is causing this and how can I get round it? ...

Can I implement State Transitions for a DFA in Java using java.util.Set

I'm implementing a DFA as close as I can to the formal definition as a learning exercise (and blogging material) I planned on using a java.util.Set where a set is involved in the definition. The definition involves a set of tuples to define the legal state transitions: (state,symbol) -> nextState. I have a Transition class with member...

Java - Modifying serialVersionUID of binary serialized object

A few months back I serialized a java.io.Serializable object into a file. Now I need to read the contents, but since then the serialVersionUID has changed, and now I'm getting a "class incompatible" error. I know for a fact that none of the data members have changed, so the only barrier is the serialVersionUID check. Is there a way to...

Hibernate generates invalid SQL query with MySQL

I have the following JPA entity classes (example case). A House belongs on a single Street. A Street has many Houses. @Entity public class House { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public Integer id; public String name @ManyToOne public Street street; } @Entity public class Street { ...

java.awt.HeadlessException - Applet not displayed

I am using Tomcat 5.5.23, JDK 1.5 on HP Unix. We have an application which when invoked form tomcat starts an applet. It was working fine till JDK 1.4. But now we have moved to JDK 1.5 and the applet does not start. The exception thrown is - java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an opera...